Assignment 2: Hex-Candle

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

This flameless candle takes hexidecimal colors as inputs, and it FLICKERS!!

Source Code

/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is hexadecimal color codes
* ###### (#FFFFFF should be "white" or all 3 LED on full brightness, 255).
*
* Original Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Modifications including Hex Value converter and Candle-like flicker
* by Jessica Voytek, 2009
*/

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 redColorVal;
int greenColorVal;
int blueColorVal;

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);
Serial.println("Enter Hexidecimal Value (eg: #FF0000 is RED) :");   
randomSeed(analogRead(0));    // see the random number generator with noise from analogue input 0
}

void loop () {
// Only if there is new data should you read the serial port and create a string out of what you read
if(Serial.available() && Serial.read() != -1) {
readSerialString(serInString);
}

// Modify each color proportionally to create a candle flicker effect without (hopefully) changing the color of the candle
int randInt = random(50, 100);    // get a random number 50-100
int redVal = redColorVal * (.01 * randInt);    // modify the color values but keep the base values for use on the next loop
int blueVal = blueColorVal * (.01 * randInt);
int greenVal = greenColorVal * (.01 * randInt);

analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);

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;
int numArray[] = {0,0,0,0,0,0,0}; // Create an array that can hold integers
while (Serial.available()) {
if ( i < 7) {  // Only get the characters we're interested in
strArray[i] = Serial.read();
numArray[i] = int(strArray[i]) - 48;
if (numArray[i] > 9) {
numArray[i] = numArray[i] - 7;
}
}
i++; 
}

redColorVal = (numArray[0] * 16) + numArray[1]; // you would think this should start at the 1st character (instead of 0th), but for some reason my script ignores the first character...?!?
greenColorVal = (numArray[2] * 16) + numArray[3];
blueColorVal = (numArray[4] * 16) + numArray[5];

Serial.print("Setting color to: r");
Serial.print(redColorVal);
Serial.print(" g");
Serial.print(greenColorVal);
Serial.print(" b");
Serial.print(blueColorVal);
Serial.print(" (#");
Serial.print(strArray[0]);
Serial.print(strArray[1]);
Serial.print(strArray[2]);
Serial.print(strArray[3]);
Serial.print(strArray[4]);
Serial.print(strArray[5]);
Serial.print(")");
Serial.println();

}

Screen Shots & Video

Wiring