Description
This project started off with the Fade program to ensure that the basic arduino setup was correct. After confirming that the circuit was properly setup with Fade, I added LEDs to the breadboard. Wires from ports 9, 10, and 11 connect to the breadboard and then each connects to a resistor and then to a LED. Each LED outputs to the breadboard bus that connects to ground. The
Materials
1 - breadboard
1 - Arduino
7 - wires
3 - LEDs (red, green, and blue)
3 - 220-ohm resistors
1 - sheet of white paper (folded into a cube lamp)
Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorVal>", where "colorCode" is
* one of "r","g",or "b" and "colorVal" is a number 0 to 255.
* E.g. "r0" turns the red LED off.
* "g127" turns the green LED to half brightness
* "b64" turns the blue LED to 1/4 brightness
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
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 = 10; // Red LED, connected to digital pin 9
int greenPin = 9; // 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, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("enter color command (e.g. 'r43') :");
}
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' ) {
//Custom code - by morgan - for every 'r' 'g' or 'b' it adds 25 to its colorVal. (i.e. enter 'rrr' for 30% r brightness)
if(serInString[1] == 'r' || serInString[1] == 'g' || serInString[1] == 'b'){
int colorCoefR = 0;
int colorCoefB = 0;
int colorCoefG = 0;
for (int i=0; i<100;i++){
if (serInString[i]=='r'){
colorCoefR++;
}
else if (serInString[i]=='g'){
colorCoefG++;
}
else if (serInString[i]=='b'){
colorCoefB++;
}
}
if (colorCoefR>0){
analogWrite(redPin, colorCoefR*25);
}
if (colorCoefG>0){
analogWrite(greenPin, colorCoefG*25);
}
if (colorCoefB>0){
analogWrite(bluePin, colorCoefB*25);
}
}
else{
colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}
}
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