Push button to start an LED flicker for 6 seconds

I am sorry if this is a simple question but I’ve been working on this for days and I can’t get it to work. I know about a thousand ways to get this not to work however. I’ve read multiple posts and have tried the approaches given there but no luck.

What I have is 3 separate LED’s on pins 9,10,11 (analog) and a button on pin 2.

SETUP

Turn pin 9,10,11 on when the Arduino starts for 2 seconds (works good)- tests that everything is working

Turn off pins 10,11 and 9 glows

LOOP

Wait for the button to be pressed and flicker all the LED’s for 6 seconds

Go back to 9 low value and 10, 11 off

I can’t get the button to work. I’ve had it to where it wouldn’t turn on the flicker and now the flicker runs continuously

// LED Fire Effect

unsigned long elapsedTime;
unsigned long onTime;
int helmleft = 10; // left side of helmet 10
int helmright = 11;  //right side of helmet to 11
int chairtop = 9; //insulators on the top of the chair to 2
const int button = 2;  //sets the button to 2

void setup()
{
  onTime=millis();
  onTime=elapsedTime;
  pinMode(button, INPUT);
pinMode(helmleft, OUTPUT);
pinMode(helmright, OUTPUT);
pinMode(chairtop, OUTPUT);
  analogWrite(helmleft, 250);
   analogWrite(helmright, 250);
   analogWrite(chairtop, 250);
  delay(2000);
  digitalWrite(button, HIGH);
  analogWrite (chairtop, 5); // glow insulators
  analogWrite (helmleft, 0); //helmet off
  analogWrite (helmright, 0); //helmet off
}

void loop() {
  if (digitalRead (button)==LOW);
     {
        changelights();
        onTime = millis(); //should only turn on the lights if the button is pressed

     }
if (onTime>0 && millis() - onTime>6000) {
  analogWrite (chairtop, 5);
  analogWrite (helmleft, 0);
  analogWrite (helmright, 0);
  onTime=0; //keeps the helmet off and the insulators glowing
  }
}

void changelights() { //flicker effect of LED's
  analogWrite(helmleft, random (0,200));
  analogWrite(helmright, random (0,200));
  analogWrite(chairtop, random (0,200));
  delay (random (10,100));
}

How is your switch wired?

Using delay() in code is never good!

.

Wire from pin 2 to one side of the button, the other side goes to ground (-).

Maybe there are some ideas here for you:

https://github.com/JChristensen/ledFire_FW/blob/master/ledFire.ino

https://github.com/JChristensen/ledFire_HW/blob/master/schematic.png

|500x386

.

Wire from pin 2 to one side of the button, the other side goes to ground (-).

pinMode(button, INPUT);

Use

pinMode(button, INPUT_PULLUP);

LarryD: Using delay() in code is never good!

.

Why? How would you suggest it be done differently?

Changed, still flickers without the button push

Any time you use delay() it freezes the sketch/program for that amount of time. It looks like you know about BWD so use that instead.

Can you confirm that pin 2 goes from HIGH to LOW when pressed. Use a DVM or a LED with a series 220 resistor.

.

Loaded the button sketch and made sure the output was as expected. Press the button, the LED turns on, no button press and the LED goes off.

I loaded the sketch below and it runs as expected but the button must be continuously pressed to make the lights flicker. I just need to add a way to flicker for 6 seconds before it resets to the rest condition. I have no idea what I am doing wrong with the timer. I don't really understand how the timer is supposed to work.

// LED Fire Effect

unsigned long elapsedTime;
unsigned long onTime;
const long interval = 6000;
int helmleft = 10; // left side of helmet 10
int helmright = 11;  //right side of helmet to 11
int chairtop = 9; //insulators on the top of the chair to 2
const int button = 2;  //sets the button to 2

void setup()
{
  onTime=millis();
  elapsedTime=onTime;
  pinMode(button, INPUT_PULLUP);
  pinMode(helmleft, OUTPUT);
  pinMode(helmright, OUTPUT);
  pinMode(chairtop, OUTPUT);
  analogWrite(helmleft, 250);
  analogWrite(helmright, 250);
  analogWrite(chairtop, 250);
  delay(2000);
  digitalWrite(button, HIGH);
  analogWrite (chairtop, 5); // glow insulators
  analogWrite (helmleft, 0); //helmet off
  analogWrite (helmright, 0); //helmet off
}

void loop() {
  if (digitalRead (button)==HIGH)
     {
        changelights();
        onTime = millis(); //should only turn on the lights if the button is pressed

     }
if (digitalRead (button)==LOW) {
  analogWrite (chairtop, 5);
  analogWrite (helmleft, 0);
  analogWrite (helmright, 0);
  onTime=0; //keeps the helmet off and the insulators glowing
  }
}

void changelights() { //flicker effect of LED's
  analogWrite(helmleft, random (0,200));
  analogWrite(helmright, random (0,200));
  analogWrite(chairtop, random (0,200));
  delay (random (10,100));
}

I have no time right now, but see example #3 and use it as a guide.

//Blink without Delay skeleton 
//4 examples demonstrated
//

//LED wiring options
//=============================================
//Depending which way your LEDs are wired, uncomment the next line.
//#define PlusEqualsON 

#ifdef PlusEqualsON
//wired so +5V turns LED ON
#define ledON  HIGH
#define ledOFF LOW
//=========================
#else
//wired so +5V turns LED OFF
#define ledON  LOW
#define ledOFF HIGH
//=========================
#endif

//switch wiring options
//=============================================
//Depending which way your switches are wired, uncomment the next line.
#define PushEqualsLOW 

#ifdef PushEqualsLOW 
//pushing the switch makes pin LOW
#define Pushed   LOW
#define Released HIGH
//=========================
#else
//pushing the switch makes pin HIGH
#define Pushed   HIGH
#define Released LOW
//=========================
#endif

//=============================================
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long pin12Millis;
unsigned long pin11Millis;
unsigned long SwitchMillis;

//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 100UL;     //100ms
unsigned long ledOnTime      = 5*1000UL;  //5 seconds

byte laststartSwitchState    = HIGH;
byte buttonState             = HIGH;
byte counter                 = 0;

//the following are enable/disable flags
//some of these might not be used in this sketch
boolean flag13 = true;
boolean flag12 = true;
boolean flag11 = true;
boolean flag10 = true;

const byte startSwitch = 2; //pushed = LOW
const byte testSwitch  = 3; //pushed = LOW

//**********************************************************************

void setup()
{
  Serial.begin(9600);
  
  digitalWrite(13,ledOFF);
  pinMode(13, OUTPUT); 

  digitalWrite(12,ledOFF);
  pinMode(12, OUTPUT);

  digitalWrite(11,ledOFF);
  pinMode(11, OUTPUT);
  
  digitalWrite(10,ledOFF);
  pinMode(10, OUTPUT);

  pinMode(startSwitch, INPUT_PULLUP); //pushed = LOW
  pinMode(testSwitch,  INPUT_PULLUP); //pushed = LOW

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<

void loop()
{
  //save the current time
  currentMillis = millis();

  //************************************* E x a m p l e  1
  //toggle pin 13 every 200mS
  //has 200ms or more gone by?
  if (currentMillis - pin13Millis >= 200UL)
  {
    //code here runs every 200ms
    //get ready for next iteration
    pin13Millis = pin13Millis + 200UL;
    //toggle pin 13
    digitalWrite(13,!digitalRead(13));
  }

  //************************************* E x a m p l e  2
  //at power up, pin 12 LED goes ON, after 3 seconds goes OFF and stays OFF
  //could be used as a powerup reset signal
  if (flag12 == true && currentMillis - pin12Millis <= 3000UL)
  {
    //code here runs for 3 seconds after power up, then stops
    digitalWrite(12,ledON);
  }
  else
  {
    digitalWrite(12,ledOFF);
    //disable further pin 12 control
    flag12 = false;
  }

  //************************************* E x a m p l e  3
  //if testSwitch is pushed and released
  //pin 11 LED goes ON for 5 seconds, then goes OFF 
  buttonState = digitalRead(testSwitch);
  
  //are we are allowed to check the switch and is it pressed?
  if(flag11 == true && buttonState == Pushed)
  {    
    //enable timing of LED on pin 11
    flag11 = false; //false --> timing is enabled
    //turn LED ON
    digitalWrite(11,ledON);
    //record the time LED turned ON
    pin11Millis = currentMillis;
  }
    
  //are we allowed and is it time to control pin 11
  if (flag11 == false && currentMillis - pin11Millis >= ledOnTime)
  {
    //if enabled, code here runs after ledOnTime ms goes by
    digitalWrite(11,ledOFF);
    //allow switch press detection again
    flag11 = true; //true --> switch monitoring is enabled
  }

  //************************************* E x a m p l e  4
  //is it time to check the switches?
  //in particular, pushing startSwitch will turn ON/OFF (toggle) an output pin 10
  //is it time to check the switches
  if (currentMillis - SwitchMillis >= debounceMillis)
  {
    //code here runs every debounceMillis ms
    //get ready for the next iteration
    SwitchMillis += debounceMillis; 
    //go and check the switches
    checkSwitches();    
  } 

  //*********************************
  //put other non-blocking stuff here
  //*********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                      F U N C T I O N S
//======================================================================


//****************** c h e c k S w i t c h e s ( ) *********************
//switches are checked every debounceValue milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()  
{
  //re-usable for all the switches  
  boolean thisState;    

  //************************************* E x a m p l e  Push ON push OFF (toggle)   
  //check if this switch has changed state
  thisState = digitalRead(startSwitch);
  if (thisState != laststartSwitchState)
  {  
    //update the switch state
    laststartSwitchState = thisState;  

    //this switch position has changed so do some stuff

    //"HIGH condition code"
    //switch went from LOW to HIGH
    if(thisState == HIGH)        
    {
      //Do some HIGH switch stuff here
    }

    //"LOW condition code"
    //switch went from HIGH to LOW
    else                          
    {
      //Do some LOW switch stuff here  
      digitalWrite(10, !digitalRead(10));
      //print number of pushes
      counter++;
      Serial.println(counter);
    }

  } //END of startSwitch code

  //*****************************************  
  //similar code for other switches goes here 
  //*****************************************  

} //END of checkSwitches()

//**********************************************************************

//======================================================================
//                      E N D  O F  C O D E
//======================================================================

First, you have a straightforward bug: a semicolon after your if statement. This will get parsed as a do-nothing statement in its own right, and so the block after the if will get executed unconditionally.

With this fixed, this is what happens when the button is pressed:

For as long as the button is held down, the lights will be flickered and the code will make a note of the time.

When the button is released, the code will leave the LEDs alone for six seconds from the last noted time. Then after six seconds from the time the button was released, things will be put back into their resting state.

Now, that probably isn't what you want to happen :) . So you'll need to re-think your logic a bit. One line will fix it. Normally you'd keep a boolean to hold the information "I am flickering/I am not flickering", but you are using onTime==0 to do that job, so you don't need any variables that you don't already have.