Random ON OFF generator project...for LED tea candle

Hi everyone!!!

I am working on creating an LED tea candle effect. I wrote this code so far, but it doesn't fade smoothly and look realistic...Its really cool on an LED, but I need help with making it fade and look like a candle. Could someone re-write this code so that it looks like a candle LED? Thanks!!! Here is the code so far:

//(I made sure that you know what each line of the functions and code is for by adding a note beside each one.)

int ledPin = 13; // LED connected to (digital) pin 13 long randOn = 0; // Initialize a variable for the ON time long randOff = 0; // Initialize a variable for the OFF time int led = 13; //ignore int brightness = 0; //ignore int fadeAmount = 10; //ignore

void setup() // run once, when the sketch starts { randomSeed (analogRead (0)); // randomize pinMode(ledPin, OUTPUT); // sets the digital pin as output }

void loop() // run over and over again { randOn = random (20, 600); // generate ON time between x seconds (fiddle with these values to get the length of time ON) randOff = random (20, 600); // generate OFF time between y seconds (fiddle with these values to get the length of time OFF) digitalWrite(ledPin, HIGH); // sets the LED on delay(randOn); // waits for a random time while ON digitalWrite(ledPin, LOW); // sets the LED off delay(randOff); // waits for a random time while OFF analogWrite(led, brightness); //ignore

brightness = brightness + fadeAmount; //ignore

if (brightness == 0 || brightness == 255) //ignore fadeAmount = -fadeAmount ; //ignore } //blinks the LED randomly...can I make it look like a candle???

So, minus the code we are supposed to ignore, you have this:

int ledPin = 13;                  // LED connected to (digital) pin 13
long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time

void setup()                      // run once, when the sketch starts
{
  randomSeed (analogRead (0));    // randomize
  pinMode(ledPin, OUTPUT);        // sets the digital pin as output
}

void loop()
{
  randOn = random (20, 600);
  randOff = random (20, 600); 
    digitalWrite(ledPin, HIGH);
    delay(randOn); 
    digitalWrite(ledPin, LOW);    // sets the LED off
    delay(randOff);
}

You are simply turning an LED on and off for differing amounts of time. A candle, on the other hand, has differing colors AND differing intensities. You won't be able to reproduce differing colors using an monochromatic LED and you won't be able to reproduce the differing brightness levels using a non-PWM pin.

A red and a yellow LED illuminating the same target, and each PWM randomly modulated , as Paul suggests , looks good as a candle..

Thank you, both. I will try to have a multi-colored LED, and write a code for an LED in a PWM pin with random differing brightness levels.