Created
December 30, 2011 18:41
-
-
Save mikedamage/1540965 to your computer and use it in GitHub Desktop.
LED candle w/ ambient light sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TrueRandom.h> | |
#define LED1 9 | |
#define LED2 10 | |
#define LED3 11 | |
#define BUTTON 13 | |
void setup() { | |
pinMode(LED1, OUTPUT); | |
pinMode(LED2, OUTPUT); | |
pinMode(LED3, OUTPUT); | |
pinMode(BUTTON, INPUT); | |
Serial.begin(115200); | |
} | |
void loop() { | |
int ambientLight = analogRead(0); | |
Serial.print("Ambient Light Level: "); | |
Serial.println(ambientLight); | |
if (ambientLight < 400) { | |
candleFlicker(); | |
} else { | |
allOff(); | |
} | |
} | |
void candleFlicker() { | |
long led1 = TrueRandom.random(100, 256); | |
long led2 = TrueRandom.random(100, 256); | |
long led3 = TrueRandom.random(100, 256); | |
analogWrite(LED1, led1); | |
analogWrite(LED2, led2); | |
analogWrite(LED3, led3); | |
delay(TrueRandom.random(40,180)); | |
} | |
void allOff() { | |
digitalWrite(LED1, LOW); | |
digitalWrite(LED2, LOW); | |
digitalWrite(LED3, LOW); | |
} | |
// vim: set ft=arduino sw=2 ts=2 : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment