Description: I played around with the fader code, and then modified the serial RGB code to increase brightness by 25 every time someone enters the r, g, or b keys. When it gets to 255, the values go back to 0. By entering z, you can zero all the values, blanking the origami balloon box.
I did the optional section as well, adding RGB values for entering y (yellow), o (orange), and p (purple).
I could not use r, g, or b again-- so I coded 1 = 100% red, 2 = 100% green, and 3 = 100% blue. r, g, and b are used to increase brightness by 25 (leading up to 255).
Components:
3 LEDs - Red, Green, Blue
3 220 Ohm Resistors
1 Arduino Uno
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 18 September 2013
* Assignment 2 - Peter Nguyen
*/
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 colorValR = 255;
int colorValG = 255;
int colorValB = 255;
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
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, colorValR); // set them all to highest brightness
analogWrite(greenPin, colorValG); // set them all to highest brightness
analogWrite(bluePin, colorValB); // set them all to highest brightness
Serial.println("enter color command (e.g. r, g, b, 1, 2, 3, o, y, p) :");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' || colorCode == 'o' || colorCode == 'y' || colorCode == 'p' || colorCode == 'z' || colorCode == '1' || colorCode == '2' || colorCode == '3' ) {
//colorVal = atoi(serInString+1);
//colorVal = colorVal + 25.5;
//if (colorVal > 255) {
// colorVal = -25.5;
//}
if (colorValR >= 255) {
colorValR = 0;
}
if (colorValG >= 255) {
colorValG = 0;
}
if (colorValB >= 255) {
colorValB = 0;
}
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r') {
Serial.print("setting RED to ");
Serial.print(colorValR);
analogWrite(redPin, colorValR);
colorValR = colorValR + 25;
}
else if(colorCode == 'g') {
Serial.print("setting GREEN to ");
Serial.print(colorValG);
analogWrite(greenPin, colorValG);
colorValG = colorValG + 25;
}
else if(colorCode == 'b') {
Serial.print("setting BLUE to ");
Serial.print(colorValB);
analogWrite(bluePin, colorValB);
colorValB = colorValB + 25;
}
else if(colorCode == 'o') {
analogWrite(redPin, 255);
analogWrite(greenPin, 127);
analogWrite(bluePin, 0);
Serial.print("ORANGE");
}
else if(colorCode == 'y') {
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
Serial.print("YELLOW");
}
else if(colorCode == 'p') {
analogWrite(redPin, 143);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
Serial.print("PURPLE");
}
else if(colorCode == 'z') {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
Serial.print("Clear All");
}
else if(colorCode == '1') {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
Serial.print("RED");
}
else if(colorCode == '2') {
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
Serial.print("GREEN");
}
else if(colorCode == '3') {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
Serial.print("BLUE");
}
}
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) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
- Login to post comments