For the lab, I used two soldered potentiometers – used one for controlling brightness of LEDs and other for controlling the blink frequency.
Materials:
- Jumper cables
- Potentiometers (2)
- Arduino
- 3 LED’s
- 3 Resistors
- Breadboard
Code:
/*
* Serial RGB LED
* —————
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is “<colorCode(1)><colorCode(n)>”, where “colorCode” is
* one of “r”,”g”,or “b”.
* E.g. “rr” sets the red LED to 20% brightness.
* “gggg” sets the green LED to 40% brightness
* “bbb” sets the blue LED to 30% brightness
*
* Created 13 Sep 2016
*
*/
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won’t work properly
char colorCode;
int colorVal;
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int sensorPin_blink = A0; // Input pin for the potentiometer
int sensorPin_bright = A1;
int sensorValue_blink = 0;
int sensorValue_bright = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 25);
analogWrite(greenPin, 25);
analogWrite(bluePin, 25);
//Serial.println(“enter color command (e.g. ‘r43’) :”);
}
void loop () {
//Serial.println(“in the loop”);
sensorValue_blink = analogRead(sensorPin_blink);
sensorValue_bright = analogRead(sensorPin_bright);
analogWrite(bluePin, sensorValue_bright/4);
analogWrite(redPin, sensorValue_bright/4);
analogWrite(greenPin, sensorValue_bright/4);
delay(sensorValue_blink);
analogWrite(bluePin, 0);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
delay(sensorValue_blink);
}
Video – https://drive.google.com/a/berkeley.edu/file/d/0ByHw8c_nutT2QVhqN0tOQ0piYms/view?usp=sharing