A more random Candleflame

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)

Hi!

Sorry, but are you sure your code is correct? I think that only two LEDs are flickering, because of your random-code.

Your code "int thisPin = random(9, 11);" only generates numbers from 9 to 10 and pin 11 is never flickering because of the special arduino random-function.

cu, robert

Interesting .. I may have read that wrong then. It's based on the array example. It looks like it's flickering but I'll investigate. Thanks :slight_smile:

flyingangel:
Your code "int thisPin = random(9, 11);" only generates numbers from 9 to 10 and pin 11 is never flickering because of the special arduino random-function.

You're right. Pin 11 goes high and low for a while then stabilizing. There are more errors in the code. Pins 1 through 13 seem to run as well. I think it's the setup doing it.

I cooked up a similar circuit with an ATtiny84A that I used for Halloween.
Had custom PC boards made so it's ready to go again for next year.
Check it out here.

karlboll:

analogWrite(ledPins[thisPin], random(2500)+30);

Hi again!

The highest value you can "analogWrite" is 255. You send a maximum value of 2499+30. So try following code:

analogWrite(ledPins[thisPin], random(226)+30);

Hope I can help you,
Robert