Writes a random PWM to a random pin (9, 10 or 11) at random
intervals for a more random flickering flame effect.
Uses an array to initialize ledPins rather than one by one.
Uses randomSeed from floating A0 for a more elegant randomness.
Added an instructable A-more-random-candleLED
/*
Based on
http://www.instructables.com/id/Realistic-Fire-Effect-with-Arduino-and-LEDs/
http://arduino.cc/en/Tutorial/Array
Writes a random PWM to a random pin (9, 10 or 11) at random
intervals for a more random flickering flame effect.
Uses an array to initialize ledPins rather than one by one.
Uses randomSeed from floating A0 for a more elegant randomness.
The circuit:
* LEDs from pins 9 through 11 to ground + complimentory 220ohm resistor
created 2014
by Karl Rosenqvist
http://www.instructables.com/member/omnibot/
This code is in the public domain.
*/
int timer = 10; // The higher the number, the slower the timing.
int ledPins[] = { 9, 10, 11 }; // an array of pin numbers to which LEDs are attached
int pinCount = 3; // the number of pins (i.e. the length of the array)
void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
randomSeed(analogRead(A0));
}
}
void loop() {
int thisPin = random(9, 11);
// turn the pin on .. or off:
analogWrite(ledPins[thisPin], random(2500)+30);
delay(random(timer));
}
LED_Fire_effect3.ino (1.23 KB)