Blink without delay with push button switch

I have a project where i have to blink several LEDs simultaneously using a push button, i got the blink without delay code that is available in the library modified a bit to make it suitable for my project, but my problem is when i press the push button the code is being executed once since the switch changes state whenever i remove my finger. my question is how can i execute the code(Blink without delay) for a duration of time specified by me whenever i press the push button once?

but my problem is when i press the push button the code

which you didn't post.

Remember to use code tags.

Hope this helps!

const int trigger = 7; const int ledPin = 13;

boolean ledState = LOW; boolean previousButtonState = LOW; boolean buttonState = LOW;

long duration = 0;

unsigned long previousMillis = 0;

// constants won't change : const long interval = 1000;

void setup() { // set the digital pin as output: pinMode(ledPin, OUTPUT); pinMode(trigger,OUTPUT); }

void loop() { unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval && buttonState == HIGH) { previousMillis = currentMillis;

if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; }

if (millis() >= duration) { previousButtonState = buttonState; buttonState = LOW; } // set the LED with the ledState of the variable: digitalWrite(ledPin, ledState); }

if(digitalRead(trigger) == HIGH) { if(previousButtonState == HIGH) { buttonState = HIGH; } else { buttonState = LOW; } delay(5); //debouncing duration = millis() + 10000; } }

The demo several things at a time flashes 3 LEDs. It is an extended example of BWoD.

...R

Instead of setting the LED only when the button is pressed (&& buttonState == HIGH) you should set it as long as millis() is below your threshold.

I’m guessing you only need to know if buttonState == HIGH after you checked the duration and are setting the buttonState to LOW.

unsigned long keepBlinkingUntil = millis();

void loop()
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) 
  { 
    previousMillis = currentMillis;   

    if (currentMillis < keepBlinkingUntil){
      if (ledState == LOW)
      {
        ledState = HIGH;
      }
      else
      {
        ledState = LOW;
      }


      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
    
    if (millis() >= duration && buttonState == HIGH)
    {
      previousButtonState = buttonState;
      buttonState = LOW;
    }
  }
  
  if(digitalRead(trigger) == HIGH)
  {
    if(previousButtonState == HIGH)
    {
      buttonState = HIGH;
    }
    else
    {
      buttonState = LOW;
    }
    delay(5); //debouncing
    duration = millis() + 10000;
    keepBlinkingUntil = duration;
  }
}

Sorry about my previous program which had a few errors.

I've fixed the errors in the following program. Hope it helps.

Thnx Fexduino for notifying me of the errors.

const int trigger = 2;
const int ledPin =  13;      

boolean ledState = LOW;
boolean previousButtonState = HIGH;
boolean buttonState = LOW;
boolean serialCheck = LOW;

long duration = 0;

unsigned long previousMillis = 0;        

// constants won't change :
const long interval = 1000;

void setup() 
{
  Serial.begin(9600);
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(trigger,INPUT);
}

void loop()
{
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis >= interval && buttonState == HIGH ) 
  { 
    previousMillis = currentMillis;   
  //  Serial.println(buttonState);
 //   if(buttonState == HIGH)
 //   {
       if (ledState == LOW)
       {
         ledState = HIGH;
         Serial.println("led set high");
       }
       else
       {
         ledState = LOW;
       }
   // }
    
    if (millis() >= duration)
    {
      Serial.println("duration completed");
      previousButtonState = buttonState;
      buttonState = LOW;
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
  
  if(digitalRead(trigger) == HIGH)
  {
    if(previousButtonState == HIGH)
    {
       Serial.println("pressed");
     //  Serial.println(currentMillis);
      buttonState = HIGH;
    }
    else
    {
      buttonState = LOW;
    }
    delay(150); //debouncing
    duration = millis() + 9000;
  }
}

this is my code: when i push my button the two leds turn on and after 10 seconds Pin 10 to turn off and after 8 seconds pin 9 to turn off. when i run the code without button the logic works but it keeps on running over and over again, in my project i need to run the above logic just once when i push my button.

long sequenceDelay = 8000; long flashDelay = 10000; const int buttonPin =2 ; int buttonState = 0; boolean LED13state = false; boolean LED12state = false; long waitUntil13 = 0; long waitUntil12 = 0; void VODKA() { digitalWrite(10, LED13state); digitalWrite(9, LED12state);

if (millis() >= waitUntil13) { LED13state = !(LED13state); waitUntil13 += flashDelay;

}

if (millis() >= waitUntil12) { LED12state = !(LED12state); waitUntil12 += sequenceDelay; } } void setup() { pinMode(10, OUTPUT); pinMode(9, OUTPUT); pinMode(buttonPin, INPUT); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == LOW) //im using the switch input initially high and when i press it gets low { VODKA(); } }

if (millis() >= waitUntil13) {

That is not how you use millis().

Have a look at several things at a time

And please use the code button “</>” for your code

…R

Also, try changing this pinMode(buttonPin, INPUT); to pinMode(buttonPin, INPUT_PULLUP); // enable internal pullup

so that buttonState definitely goes HIGH when the button is released, assuming you don't have an external pullup resistor.

I'm working on a similar project, and am also very new to this. I've succeeded at the "button" program and "blink without delay" but haven't been able to merge the two. I, too, would like to have the led flash for a specified amount of time (3000 milliseconds) when triggered before stopping. After I get the code side straightened out, I plan to increase the led quantity that is manipulated by this command.

sirjorge:
I’m working on a similar project, and am also very new to this. I’ve succeeded at the “button” program and “blink without delay” but haven’t been able to merge the two. I, too, would like to have the led flash for a specified amount of time (3000 milliseconds) when triggered before stopping. After I get the code side straightened out, I plan to increase the led quantity that is manipulated by this command.

It is best to start your own post to ask your question.
To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.