Description
Components
1x Arduino
1x Breadboard
3x LED lights (1 each of red, green and blue)
7x Wires
3x 220 ohm resistors
1x Macbook Pro
1x Lampshade, lined with foil
Diffuser
I tried different materials for creating a diffuser - cotton lined inside a bottle opener, foil inside a shot glass before finally settling on using a lamp shade lined with foil, to reflect light. I felt that this gave me the best effect of light mixing.
Code
For the code, I modified the Serial Input code from class to allow for increasing the red, green and blue by 10% based on user keystrokes. I ensured that once the color value reached 255 (which is the point of maximum brightness), the user would see a message informing them that this limit had been touched.
As an optional question, we were asked to modify the code to do something additional. I allowed two things - 1. Using the command 'off' to turn the lights off, and 2. A setting called 'Surprise' which basically auto-mixes colors (in intervals of 0.5 seconds) to give an impression of a Dancing Light.
/*
* 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 I changed the order of the pins because that's how I ended up connecting them on the board
int greenPin = 11; // Green LED, connected to digital pin 10
int bluePin = 9; // Blue LED, connected to digital pin 11
// Initialize Colors
int redVal = 0;
int greenVal = 0;
int blueVal = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 255); // set them all to full brightness
analogWrite(greenPin, 255); // set them all to full brightness
analogWrite(bluePin, 255); // set them all to full brightness
Serial.println("Enter 'off' to turn off the light");
Serial.println("Enter 'surprise' to see a random light combination");
Serial.println("enter color command (e.g. 'Enter a combination of rgb to illuminate the lights, e.g. rrgg or rgbb etc') :");
}
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 (strcmp(serInString, "off") == 0) // input is off
{
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin,0);
memset(serInString, 0,100);
}
if (strcmp(serInString, "surprise") == 0) // input is surprise mode
{
for (int i =0; i<10; i++)
{
digitalWrite(redPin, HIGH);
delay(500);
digitalWrite(greenPin, HIGH);
delay(500);
digitalWrite(bluePin, LOW);
delay(500);
digitalWrite(redPin, LOW);
delay(500);
digitalWrite(greenPin, HIGH);
delay(500);
digitalWrite(bluePin, HIGH);
delay(500);
digitalWrite(redPin, HIGH);
delay(500);
digitalWrite(greenPin, LOW);
delay(500);
digitalWrite(bluePin,HIGH);
delay(500);
Serial.print(i);
}
memset(serInString, 0,100);
}
for(int i =0; i <= sizeof(serInString); i++)
{
colorCode = serInString[i];
if(colorCode =='r')
{
if(redVal+25.5 >= 255)
{
Serial.print("Red is at its brightest");
Serial.println();
redVal = 255;
}
else
{
redVal = redVal + 25.5;
}
analogWrite(redPin, redVal);
Serial.println("Red is now");
Serial.println(redVal);
Serial.println();
}
if(colorCode =='g')
{
if(greenVal+25.5 >= 255)
{
Serial.print("Green is at its brightest");
Serial.println();
greenVal = 255;
}
else
{
greenVal = greenVal + 25.5;
}
analogWrite(greenPin, greenVal);
Serial.print("Green is now");
Serial.print(greenVal);
Serial.println();
}
if(colorCode =='b')
{
if(blueVal+25.5 >= 255)
{
Serial.print("Blue is at its brightest");
Serial.println();
blueVal = 255;
}
else
{
blueVal = blueVal + 25.5;
}
analogWrite(bluePin, blueVal);
Serial.print("Blue is now");
Serial.println(blueVal);
Serial.println();
}
}
delay(500);
}
//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