LED Candle Flicker Effect WITH Button

Hello people,

I'm totally new to Arduino and been doing some tinkering. Basically I want to make the LED's flicker like a candle light with the ability to switch them on and off simultaneously with a button. I've got all the connections on the Arduino Uno board sorted (I've just got the starter kit). My problem is I can make the LEDs flicker like a candle and I can also make the switch turn them on and off BUT I can't get seem to get them to work together.

Here is the code I'm using to make the lights flicker:

const int ledPin = 11; const int ledPin2 = 10; const int ledPin3 = 9;

void setup() { pinMode(ledPin, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); }

void loop() {

analogWrite(ledPin, random(120)+135); analogWrite(ledPin2, random(120)+135); analogWrite(ledPin3, random(120)+135); delay(random(100)); }

And here's the code I'm using to make the button work:

const int buttonPin = 7; const int ledPin = 11; const int ledPin2 = 10; const int ledPin3 = 9;

int ledState = HIGH; int buttonState; int lastButtonState = LOW;

long lastDebounceTime = 0; long debounceDelay = 50;

void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT);

analogWrite(ledPin, random(120)+135); analogWrite(ledPin2, random(120)+135); analogWrite(ledPin3, random(120)+135); delay(random(100)); }

void loop() { int reading = digitalRead(buttonPin);

if (reading != lastButtonState) { lastDebounceTime = millis(); }

if ((millis() - lastDebounceTime) > debounceDelay) {

if (reading != buttonState) { buttonState = reading;

if (buttonState == HIGH) { ledState = !ledState; } } }

digitalWrite(ledPin, ledState); digitalWrite(ledPin2, ledState); digitalWrite(ledPin3, ledState);

lastButtonState = reading; }

But I have no idea how to sort the coding out so that the LEDs flicker when they're switched on by the button. I've looked everywhere for a solution but no luck... Is it even possible? HELP!!! :(

have a look at this old post of mine - http://forum.arduino.cc/index.php?topic=105030.msg787859#msg787859 - it tries to simulates lightning. Might give an idea how to code a flame.

this?? - http://forum.arduino.cc/index.php?topic=96600.0 -

Please select "modify" and highlight each code section in your previous post and mark it as code with the [ # ] button above the submission window.

Char587: But I have no idea how to sort the coding out so that the LEDs flicker when they're switched on by the button. I've looked everywhere for a solution but no luck... Is it even possible? HELP!!! :(

const int buttonPin = 7;    
const int ledPin = 11;      
const int ledPin2 = 10;      
const int ledPin3 = 9;     


int ledState = HIGH;        
int buttonState;             
int lastButtonState = HIGH;   

long lastDebounceTime = 0;  
long debounceDelay = 10;

void setup() {
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // sets pull-up
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

}

void loop() {
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == LOW) {
        ledState = !ledState;
      }
    }
  }

  if (ledState) {  
    analogWrite(ledPin, random(120)+135);
    analogWrite(ledPin2, random(120)+135);
    analogWrite(ledPin3, random(120)+135);
    delay(random(100));
  } 
  else {   
    analogWrite(ledPin, 0);
    analogWrite(ledPin2, 0);
    analogWrite(ledPin3, 0);
  }

  lastButtonState = reading;
}

Note that I have reformed it to use a button connected to ground and the internal pull-up.