Assignment: Sensing: Potentiometers
Collaborators:
Continuing with my electronics-in-the-kitchen tinkering, I've made an onion that changes color and blinks at different rates via separate controls from two potentiometers.
The onion was hollowed out to leave the outer two or three layers.
To change colors, turn the knob of the color controlling potentiometer. I've used a linear mapping for rgb values to range from red (1,0,0) to blue (0,0,1).
To change the blinking rate, turn the knob of the other potentiometer. That code has not been modified.
Attached are photos of 1) the onion and circuit not lit up, and 2) the onion glowing teal. Also, here's a movie of my color-changing flash-onion
Below is my code:
/*
* one pot changes color of LEDs, the other pot changes the blinking rate
* modification of the following
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int potColorPin = 0; // select the input pin for the potentiometer 1
int potBlinkPin = 1; // select the input pin for the potentiometer 2
long int potColorVal = 0; // variable to store the value coming from pot 1
int potBlinkVal = 0; // variable to store the value coming from pot 2
int redPin = 9; // select the pin for the red LED
int greenPin = 10; // select the pin for the green LED
int bluePin = 11; // select the pin for the blue LED
void setup() {
pinMode(redPin, OUTPUT); // declare the led1Pin as an OUTPUT
pinMode(greenPin, OUTPUT); // declare the led2Pin as an OUTPUT
pinMode(bluePin, OUTPUT); // declare the led2Pin as an OUTPUT
Serial.begin(9600);
}
void loop() {
long int val;
potColorVal = analogRead(potColorPin); // read the value from pot Color, between 0 - 1024, for color control
potBlinkVal = analogRead(potBlinkPin); // read the value from pot Blink, between 0 - 1024, for blinking
potColorVal = potColorVal + 1;
Serial.print(potColorVal);
Serial.print(" ");
// change LEDs to corresponding color value from potColor
if(potColorVal == 1)
{
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 200);
Serial.print("blue\n");
}
else if(potColorVal <= 256)
{
analogWrite(redPin, 0);
analogWrite(greenPin, (float)255/(float)256*(float)potColorVal);
analogWrite(bluePin, 200);
Serial.print("blue, some green\n");
}
else if(potColorVal <= 512)
{
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, (float)-255/(float)256*(float)potColorVal + 510);
Serial.print("green, some blue\n");
}
else if(potColorVal <= 768)
{
analogWrite(redPin, (float)-255/(float)256*(float)potColorVal - 510);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
Serial.print("green, some red\n");
}
else
{
analogWrite(redPin, 255);
analogWrite(greenPin, ((float)-255/(float)256*(float)potColorVal + 1020));
analogWrite(bluePin, 0);
Serial.print("red, some green\n");
}
delay(potBlinkVal); // stop the program for some time, meaning, LED is on for this time
analogWrite(redPin, 0); // dim red LED to completely dark (zero)
analogWrite(greenPin, 0); // dim green LED to completely dark (zero)
analogWrite(bluePin, 0); // dim blue LED to completely dark (zero)
delay(potBlinkVal); // stop the program for some time, meaning, LED is OFF for this time
}