Approach
For this assignment, we use 3 single color LEDS which is red, green and blue. They can be used to combine different secondary colors and with the correct code, the bulb intensity for each color can be adjusted depending on the user. The user can type in 'r', 'g' or and 'b' multiple times and each count will increase the light intensity by 30 till it reaches 255, which will return and start from 0 again. I also added more color options like cyan, purple and yellow. Or the user can type in '0' to turn the lights off. I used an egg shell to scatter the light from beneath and through the cracks of the egg.
Components Used
- 1. Arduino UNO
- 2. Solderless breadboard,Digikey #23273-ND
- 3. Blue LED,Jameco #183222
- 4. Green LED, Jameco #334473
- 5. Red LED, Jameco #33481
- 6. 220 ohm resistor, Jameco #107941 x 3
- 7. USB cable, Jameco #222607
Code
/*
Coding template based on previous student's work
Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Adapted 5 September 2007
* copylefter 2007 Ryan Aipperspach <ryanaip@alumni.rice.edu>
*/
char serInString[100];
char colorCod;
int colorValue;
int redPin = 9; //Red LED in pin 9
int greenPin = 10; //Green LED in pin 10
int bluePin = 11; //Blue LED in pin 11
int redIntens = 0; //Initial Red brightness set to 0
int greenIntens = 0; //Initial Green brightness set to 0
int blueIntens = 0; //Initial Blue brightness set to 0
void setup (){
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redIntens); // set them all to mid brightness
analogWrite(greenPin, greenIntens);
analogWrite(bluePin, blueIntens);
Serial.println("Please Enter Color Command (rrrrbbbbgg')");
Serial.println("Otherwise, Please Enter Color Command 'C' for cyan max brightness");
Serial.println("Otherwise, Please Enter Color Command 'P' for purple max brightness");
Serial.println("Otherwise, Please Enter Color Command 'Y' for yellow max brightness");
Serial.println("If not, Please Enter Color Command '0' for default-OFF");
}
void loop () {
readSerialString(serInString, 100);
processRepeatKeyCommands(serInString, 100);
resetSerialString(serInString, 100);
delay(100);
}
void resetSerialString (char *strArray, int length) {
for (int i = 0; i < length; i++) {
strArray[i] = '\0';
}
}
void readSerialString (char *strArray, int maxLength) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available() && i < maxLength) {
strArray[i] = Serial.read();
i++;
}
}
void processRepeatKeyCommands(char *strArray, int maxLength) {
int i = 0;
while (i < maxLength && strArray[i] != '\0') {
colorCod = serInString[i];
if (colorCod == 'r') {
redIntens = (redIntens + 30) % 256;
analogWrite(redPin, redIntens);
Serial.print("setting color r to ");
Serial.println(redIntens);
} else if (colorCod == 'g') {
greenIntens = (greenIntens + 30) % 256;
analogWrite(greenPin, greenIntens);
Serial.print("setting color g to ");
Serial.println(greenIntens);
} else if (colorCod == 'b') {
blueIntens = (blueIntens + 30) % 256;
analogWrite(bluePin, blueIntens);
Serial.print("setting color b to ");
Serial.println(blueIntens);
} else if (colorCod == 'P') {
blueIntens = 255;
greenIntens = 0;
redIntens = 255;
analogWrite(bluePin, blueIntens);
analogWrite(greenPin, greenIntens);
analogWrite(redPin, redIntens);
Serial.println("setting color to purple ");
} else if (colorCod == 'C') {
blueIntens = 255;
greenIntens = 255;
redIntens = 0;
analogWrite(bluePin, blueIntens);
analogWrite(greenPin, greenIntens);
analogWrite(redPin, redIntens);
Serial.println("setting color to cyan ");
} else if (colorCod == 'Y') {
blueIntens = 0;
greenIntens = 150;
redIntens = 255;
analogWrite(bluePin, blueIntens);
analogWrite(greenPin, greenIntens);
analogWrite(redPin, redIntens);
Serial.println("setting color to yellow ");
} else if (colorCod == '0') {
blueIntens = 0;
greenIntens = 0;
redIntens = 0;
analogWrite(bluePin, blueIntens);
analogWrite(greenPin, greenIntens);
analogWrite(redPin, redIntens);
Serial.println("OFF");
}
i++;
}
}
- Login to post comments