Blink a LED every 500 miliseconds during 3 seconds when a switch is pressed

Hi everyone, I looked for this question in the forums but i didnt find anything...

I've been working on blinking 2 LEDs with 2 switches, but they only blink while I press the switch. Is there any way to blink them during 3 seconds just pressing one time the switch and stop pressing it? Anyone can show me a code example please? :/ thank you!

Just to clarify... your current way is to hold the switch down while the blinking goes on, but you would prefer just a single press to start the blinking process?

If that's the case, I would use the single press to set a state variable (say called "blinking") to true. Then while blinking is true, do the blinking stuff (and look at blink without delay in the examples), setting blinking to false at the end of the process. Off the top of my head.....

Show us the sketch you are working on.

JimboZA: Just to clarify... your current way is to hold the switch down while the blinking goes on, but you would prefer just a single press to start the blinking process?

If that's the case, I would use the single press to set a state variable (say called "blinking") to true. Then while blinking is true, do the blinking stuff (and look at blink without delay in the examples), setting blinking to false at the end of the process. Off the top of my head.....

Yea that's what I wish, but I still read your reply and can't figure out what you say :/

LarryD: Show us the sketch you are working on.

Here you got :]

int switchleft=3; 
int ledleft=13;      
int switchright=2;  
int ledright=12;  


void setup() 
{
 pinMode(switchleft, INPUT); 
 pinMode(ledleft,OUTPUT);   
 pinMode(switchright, INPUT);
 pinMode(ledright,OUTPUT);  
}


void loop() 
{         


   if (digitalRead(switchleft)==HIGH) 
   {

     digitalWrite(ledleft,HIGH); 
     delay(500); 
     digitalWrite(ledleft,LOW); 
     delay(500); 
   }
   else 
   {

     digitalWrite(ledleft,LOW); 
   }
   
   
      if (digitalRead(switchright)==HIGH) 
   
   {

     digitalWrite(ledright,HIGH); 
     delay(500);
     digitalWrite(ledright,LOW); 
     delay(500);
   }
   else
   {

     digitalWrite(ledright,LOW); 
   }
}

Definitely, I would make that with a single pressing a button It would blink a LED for 3 seconds... I have been trying with millis() but unsuccesfully I deleted it :/

Just did the same thing you want a couple of days ago:
In my case i wired a button to GND and D2 and enabled the pullup on D2. This way D2 goes low when the button is pressed. Also i needed by button to trigger the button after it had been hold down for at least 2 seconds. The code is easy to change to your own needs.

long lastDebounceTime = 0;
int debounceDelay = 2000;
int buttonState;  // the current reading from the input pin
int lastButtonState = LOW;  
int ledState = LOW;      
long previousMillis = 0;
int LEDFlashInterval=0;
int LEDFlashNum=0;
void setup(){
  pinMode(2,INPUT); // button pin
  digitalWrite(2,HIGH); // button goes low when presded
}
void loop()
{
  ButtonStatus();
  LEDFlash();
}
void ButtonStatus(){
  int reading = digitalRead(2);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > debounceDelay && LEDFlashNum==0) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == LOW) {
          LEDFlashInterval=500;
          LEDFlashNum=6; //3x ON, 3x OFF

      }
    }
  }
  lastButtonState = reading;
}
void LEDFlash(){
  if (LEDFlashNum>0){
    unsigned long currentMillis=millis();
     if(currentMillis - previousMillis > LEDFlashInterval) {
       previousMillis = currentMillis;   
       if (ledState == LOW)
         ledState = HIGH;
       else
         ledState = LOW;
       digitalWrite(13, ledState);
       LEDFlashNum--;
     }
  }
}

racquemis:
Just did the same thing you want a couple of days ago:
In my case i wired a button to GND and D2 and enabled the pullup on D2. This way D2 goes low when the button is pressed. Also i needed by button to trigger the button after it had been hold down for at least 2 seconds. The code is easy to change to your own needs.

long lastDebounceTime = 0;

int debounceDelay = 2000;
int buttonState;  // the current reading from the input pin
int lastButtonState = LOW;  
int ledState = LOW;      
long previousMillis = 0;
int LEDFlashInterval=0;
int LEDFlashNum=0;
void setup(){
 pinMode(2,INPUT); // button pin
 digitalWrite(2,HIGH); // button goes low when presded
}
void loop()
{
 ButtonStatus();
 LEDFlash();
}
void ButtonStatus(){
 int reading = digitalRead(2);
 if (reading != lastButtonState) {
   lastDebounceTime = millis();
 }
 if ((millis() - lastDebounceTime) > debounceDelay && LEDFlashNum==0) {
   if (reading != buttonState) {
     buttonState = reading;
     if (buttonState == LOW) {
         LEDFlashInterval=500;
         LEDFlashNum=6; //3x ON, 3x OFF

}
   }
 }
 lastButtonState = reading;
}
void LEDFlash(){
 if (LEDFlashNum>0){
   unsigned long currentMillis=millis();
    if(currentMillis - previousMillis > LEDFlashInterval) {
      previousMillis = currentMillis;  
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
      digitalWrite(13, ledState);
      LEDFlashNum–;
    }
 }
}

I loaded your code in my Arduino UNO and does not work for me… I dunno what’s the issue… :~ I am trying to figure out how to write that function in my sketch.

EDIT: I want to make it with delays, and I have found the way to press it one time to work just adding a do & while with a millis, but it only runs one time, the next time I press the button it only blinks 1 time, I think it happens because millis is bigger than 3000… How can I reset the value of millis? I add here the sketch:

int switchleft=3; 
int ledleft=13;      
int switchright=2;  
int ledright=12;  
int interval=3000;
 

void setup() 
{
 pinMode(switchleft, INPUT); 
 pinMode(ledleft,OUTPUT);   
 pinMode(switchright, INPUT);
 pinMode(ledright,OUTPUT);  
}
 

void loop() 
{         
 

   if (digitalRead(switchleft)==HIGH) 
   {
    do {
     digitalWrite(ledleft,HIGH); 
     delay(500); 
     digitalWrite(ledleft,LOW); 
     delay(500); 
   }
   while (millis()<interval);
   else 
   {

     digitalWrite(ledleft,LOW); 
   }
   
   
      if (digitalRead(switchright)==HIGH) 
   
   {
 do {
     digitalWrite(ledright,HIGH); 
     delay(500);
     digitalWrite(ledright,LOW); 
     delay(500);
   }
   while (millis()<interval);
   else
   {

     digitalWrite(ledright,LOW); 
   }
}

I really don’t recommend using delays since it blocks basically everything else going on. If it’s just for learning purposes it okay but If you intent to add additional functionality to your sketch i would want to freely cycle through the main loop (for example if your taking measurements from sensors or when your controlling something) without being delayed.

My example should work if the right changes are made:
change debounceDelay to a value of 40. (holding the button for 40ms triggers the flashing)
and change ‘if (buttonState == LOW)’ to ‘if (buttonState == HIGH)’
Pressing the button attached to D2 should flash the led on D13

If you really want to use your own sketch, you need to store the time you pressed the button and compare that to the current time. if the current time is 3000 ms higher then the time you stored then it needs to stop.
I’ve made some changes to your sketch:

int switchleft=3; 
int ledleft=13;      
int switchright=2;  
int ledright=12;  
int interval=3000;
unsigned long PreviousMillisLeft,PreviousMillisRight; 

void setup() 
{
  pinMode(switchleft, INPUT); 
  pinMode(ledleft,OUTPUT);   
  pinMode(switchright, INPUT);
  pinMode(ledright,OUTPUT);  
}


void loop() 
{         
  if (digitalRead(switchleft)==HIGH) 
  {
    PreviousMillisLeft=millis();
    do {
      digitalWrite(ledleft,HIGH); 
      delay(500); 
      digitalWrite(ledleft,LOW); 
      delay(500); 
    }
    while (millis()-PreviousMillisLeft<interval);
  }
  else 
  {
    digitalWrite(ledleft,LOW); 
  }
  if (digitalRead(switchright)==HIGH) {
    PreviousMillisRight=millis();
    do {
      digitalWrite(ledright,HIGH); 
      delay(500);
      digitalWrite(ledright,LOW); 
      delay(500);
    }
    while (millis()-PreviousMillisRight<interval);
  }
  else
  {
    digitalWrite(ledright,LOW); 
  }
}

racquemis:
I really don’t recommend using delays since it blocks basically everything else going on. If it’s just for learning purposes it okay but If you intent to add additional functionality to your sketch i would want to freely cycle through the main loop (for example if your taking measurements from sensors or when your controlling something) without being delayed.

My example should work if the right changes are made:
change debounceDelay to a value of 40. (holding the button for 40ms triggers the flashing)
and change ‘if (buttonState == LOW)’ to ‘if (buttonState == HIGH)’
Pressing the button attached to D2 should flash the led on D13

If you really want to use your own sketch, you need to store the time you pressed the button and compare that to the current time. if the current time is 3000 ms higher then the time you stored then it needs to stop.
I’ve made some changes to your sketch:

int switchleft=3; 

int ledleft=13;      
int switchright=2;  
int ledright=12;  
int interval=3000;
unsigned long PreviousMillisLeft,PreviousMillisRight;

void setup()
{
 pinMode(switchleft, INPUT);
 pinMode(ledleft,OUTPUT);  
 pinMode(switchright, INPUT);
 pinMode(ledright,OUTPUT);  
}

void loop()
{        
 if (digitalRead(switchleft)==HIGH)
 {
   PreviousMillisLeft=millis();
   do {
     digitalWrite(ledleft,HIGH);
     delay(500);
     digitalWrite(ledleft,LOW);
     delay(500);
   }
   while (millis()-PreviousMillisLeft<interval);
 }
 else
 {
   digitalWrite(ledleft,LOW);
 }
 if (digitalRead(switchright)==HIGH) {
   PreviousMillisRight=millis();
   do {
     digitalWrite(ledright,HIGH);
     delay(500);
     digitalWrite(ledright,LOW);
     delay(500);
   }
   while (millis()-PreviousMillisRight<interval);
 }
 else
 {
   digitalWrite(ledright,LOW);
 }
}

Thank you! Now I can see what I was failing, that was what I really wanted :]