Description:
The goal was to allow input from the serial terminal to adjust the output of three LEDs. With limited resources, I created a diffuser from a piece of paper towel and sealed the top of the paper to form a cone. It was very successful in replicating many different colors like purple and turquoise. The program takes occurrences of the color-code characters like ‘r’, ‘g’, and ‘b’ to increase of 20% to each LED, until a maximum of 100%. Going beyond 100% resets the individual LED to zero, where the new adjustments can be made.
Components:
1 Breadboard
1 Arduino Uno
3 LEDs (Red, Blue, Green)
1 USB Cable
3 220 Ohm Resistors
10 wires
Code:
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs .. AND MORE!
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
* Modified 24 September 2013
* Anthony Suen
*
* Typing the 'R', 'G', and 'B' keys will cycle the brighness of the corresponding LEDs upwards by 20%.
*/
char* colorCode;
int redPin = 11; // Red LED, connected to digital pin 11
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 9; // Blue LED, connected to digital pin 9
int red = 0;
int green = 0;
int blue = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
Serial.println("Enter color command 'r', 'g', or 'b' :");
}
void loop () {
// clear the string
memset(colorCode, 0, 1);
//read the serial port and create a string out of what you read
readSerialString(colorCode);
char color = *colorCode;
if( color == 'r' || color == 'g' || color == 'b' ) {
Serial.print("setting color ");
Serial.print(color);
Serial.print(" to ");
if(color == 'r') {
analogWrite(redPin, increment(&red));
Serial.print(red);
}
else if(color == 'g') {
analogWrite(greenPin, increment(&green));
Serial.print(green);
}
else if(color == 'b') {
analogWrite(bluePin, increment(&blue));
Serial.print(blue);
}
Serial.println("%");
colorCode = 0; // indicates we've used this string
}
delay(100); // wait a bit, for serial data
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
if(!Serial.available()) {
return;
}
*strArray = Serial.read();
}
int increment(int* color) {
*color += 20;
if(*color > 100) {
*color = 0;
}
int value = (((double) *color)/100)*255;
return value;
- Login to post comments