Auto Candle

Assignment: Input / Output Coincidence Lab Assignment

Collaborators:

Assignment: Input / Output Coincidence Lab Assignment
Collaborators:

This candle turns on automatically when the lights in the room go off.  A photo diode embedded in the candle detects changes in the ambient light in the room, and then the Arduino turns on/off the LEDs.

Code

/*
*  Auto-Candle
*  Turns on when the lights go off
*  Turns off when the lights go on
*  Jessica Voytek, October 2009
*/

// Input
int pin0 = 0;
int val = 0;
int lastValue = 0;

// Output
int redPin = 9;
int greenPin = 10;
int bluePin  = 11;

int redColorVal;
int greenColorVal;
int blueColorVal;

int redVal = 0;
int greenVal = 0;
int blueVal = 0;

int i = 0;
int wait = 50;
int DEBUG = 1;

void setup() {               
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);  
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
randomSeed(analogRead(0));
}

void loop() {
val = analogRead(pin0);   // read the value from pot 1, between 0 - 1024, for dimming
if (val < 10 && (lastValue - val) > 30) {
Serial.println("light");
light();
delay(1000);
}
if (val > 10 && (lastValue - val) < 30) {
Serial.println("dark");
dark();
delay(1000);
}    
lastValue = val;

// 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

if (DEBUG) { // If we want to read the output
Serial.println(val);
}

}

void light() {
redColorVal = 255;
greenColorVal = 0;
blueColorVal = 255;
}

void dark() {
redColorVal = 0;
greenColorVal = 0;
blueColorVal = 0;
}

Photos