ASSIGNMENT
Connect 3 LEDs (red, green, and blue) and resistors to the Arduino breadboard and create a custom way to control the brightness of each LED using serial input from the keyboard.
In addition, create a unique diffuser for your light.
MATERIALS USED
3 LEDs (red, green, blue), 3 220 ohm resistors, Halloween Ghost Decoration
ARDUINO CODE
/*
* Serial RGB LED - Halloween Edition
* ---------------
* Serial commands control the brightness of R,G,B LEDs
* including a special halloween easter egg.
*
* Command structure is entering either the 'r', 'g', or 'b' character
* from 1 - 10 times in a row, with each character increasing that color
* by aproximately 10% brightness.
*
* E.g. "rrrr" turns the red LED to approximately 40% brightness.
* "ggggggggg" turns the green LED to approximately 90% brightness
*
* In addition the user can enter the word 'halloween' to see a special
* halloween themed light show.
*
* Created 13 September 2011 by Kari McGlynn
* Based on modification of Serial RGB LED
* 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
int lengthString = 0;
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 valueR = 25; //10% brightness
int valueG = 25; //10% brightness
int valueB = 25; //10% brightness
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, valueR); // set them all to 10% brightness
analogWrite(greenPin, valueG); // set them all to 10% brightness
analogWrite(bluePin, valueB); // set them all to 10% brightness
Serial.print("Current led brightness values are... Red: ");
Serial.print(valueR);
Serial.print(" Green: ");
Serial.print(valueG);
Serial.print(" Blue: ");
Serial.println(valueB);
Serial.println("Enter color code (e.g. 'r') from 1-10 times in a row to change the brightness (e.g. rrrrr = red at 50% brightness");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
if(serInString[0] == 'h' && serInString[1] == 'a' && serInString[2] == 'l' && serInString[3] == 'l' && serInString[4] == 'o' && serInString[5] == 'w' && serInString[6] == 'e' && serInString[7] == 'e' && serInString[8] == 'n')
{
int count = 0;
while (count <10){
analogWrite(redPin, 254); // set red to 254
analogWrite(greenPin, 20); // set green to 20
analogWrite(bluePin, 0); // set blue to 0
delay(300); // wait for 300 milliseconds
analogWrite(redPin, 0); // set them all to 0
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(300); // wait for 300 milliseconds
count++;
}
Serial.println("Happy Halloween!");
delay(100); // wait a bit, for serial data
}
else{
for (int b = 0; b < lengthString; b++)
{
colorCode = serInString[b];
if(colorCode == 'r' )
{
valueR = valueR + 25;
if (valueR > 255)
valueR = valueR - 255;
analogWrite(redPin, valueR);
Serial.print(" Setting Red to ");
Serial.println(valueR);
}
else if(colorCode == 'g' )
{
valueG = valueG + 25;
if (valueG > 255)
valueG = valueG - 255;
analogWrite(greenPin, valueG);
Serial.print(" Setting Green to ");
Serial.println(valueG);
}
else if(colorCode == 'b' )
{
valueB = valueB + 25;
if (valueB > 255)
valueB = valueB - 255;
analogWrite(bluePin, valueB);
Serial.print(" Setting Blue to ");
Serial.println(valueB);
}
}
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;
lengthString = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
lengthString++;
}
}