I’ve been attending a series of Arduino workshops at my local hackerspace, Arch Reactor. The last workshop focused on blinking LEDs but that was a little bit too simple for me so I took it a little further with some ideas spread across the internet.
My project for this week was to imitate a candle flicker with LEDs. I’ve added a photocell to initiate the flicker when the lights get dim.
Sketch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
int ledPin1 = 9; // LED 1 int ledPin2 = 10; // LED 2 int ledPin3 = 11; // LED 3 int photocellPin = 0; // the cell and 10K pulldown are connected to a0 int photocellReading; // the analog reading from the analog resistor divider void setup(void) { // We'll send debugging information via the Serial monitor Serial.begin(9600); pinMode(ledPin1, OUTPUT); // Set LED 1 to be Output pinMode(ledPin2, OUTPUT); // Set LED 2 to be Output pinMode(ledPin3, OUTPUT); // Set LED 3 to be Output } void loop(void) { photocellReading = analogRead(photocellPin); Serial.print("Analog reading = "); Serial.print(photocellReading); // the raw analog reading // We'll have a few threshholds, qualitatively determined if (photocellReading < 10) { Serial.println(" - Dark - Flicker"); ledFlicker(); } else if (photocellReading < 200) { Serial.println(" - Dim - Flicker"); ledFlicker(); } else if (photocellReading < 500) { Serial.println(" - Light"); ledOff(); } else if (photocellReading < 800) { Serial.println(" - Bright"); ledOff(); } else { Serial.println(" - Very bright"); ledOff(); } //delay(1000); } void ledFlicker(){ // LED flicker effect analogWrite(ledPin1, random(120)+135); analogWrite(ledPin2, random(120)+135); analogWrite(ledPin3, random(120)+135); delay(random(100)); } void ledOff(){ // Turn off LEDs analogWrite(ledPin1, 0); analogWrite(ledPin2, 0); analogWrite(ledPin3, 0); } |