Arduino Blink without Delay

preview_player
Показать описание
Sometimes you need to do two things at once. For example you might want to blink an LED while reading a button press. In this case, you can't use delay(), because Arduino pauses your program during the delay().

If the button is pressed while Arduino is paused waiting for the delay() to pass, your program will miss the button press.

This video demonstrates how to blink an LED without using delay(). It turns on the LED on and then makes note of the time.

Then, each time through loop(), it checks to see if the desired blink time has passed. If it has, it toggles the LED on or off and makes note of the new time. In this way the LED blinks continuously while the sketch execution never lags on a single instruction

Don't forget to subscribe!
Рекомендации по теме
Комментарии
Автор

The code is available online, a video running through how it works would have been useful.

cms
Автор

This is the code I was looking for.
I like the video.
Thank you.
감사합니다.

healingday
Автор

Don't know about the dislikes, this works

BoneAppleTea
Автор

Thanks
I like the bang toggle
ledState = !ledState
not as controllable or elegant

patrickmclaughlin
Автор

Thank a lot for your nice video, I want to use this code for my program while trying to connect to wifi

masouddayaghi
Автор

hi i need a 2 second delay with below code, delay location is also written inside code, plz help in this regard- --

int input = 2;
int output1 = 3;
int output2 = 4;
int output3 = 8;
int output4 = 9;
int output5 = 10;
int output6 = 11;
int state = 0;
int old =0;
int buttonpoll=0;

void setup() {
pinMode(input, INPUT_PULLUP);
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
pinMode(output3, OUTPUT);
pinMode(output4, OUTPUT);
pinMode(output5, OUTPUT);
pinMode(output6, OUTPUT);

digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
digitalWrite(output4, LOW);
digitalWrite(output5, LOW);
digitalWrite(output6, LOW);

}

void loop() {



buttonpoll = digitalRead (input);
if (buttonpoll ==1){
delay (80);
buttonpoll = digitalRead (input);
if (buttonpoll==0){
state = old + 1;
}}

else
{
delay(100);}
switch(state){
case 1:
digitalWrite(output3, LOW);
digitalWrite(output4, HIGH);
digitalWrite(output5, LOW);
digitalWrite(output6, HIGH);
digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);
old = state;
break;
case 2:
digitalWrite(output3, HIGH);
digitalWrite(output4, LOW);
digitalWrite(output5, HIGH);
digitalWrite(output6, LOW);
digitalWrite(output1, LOW);
digitalWrite(output2, LOW);

//i need here a break of 2 seconds and continue the output1 and 2 High, also continue to next switch case.

digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);

old = state;
break;
default:

digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
digitalWrite(output4, LOW);
digitalWrite(output5, LOW);
digitalWrite(output6, LOW);
old = 0;
break;


}

}

Quantum_innovations
Автор

is it possible to apply this feature in stepper motor control ?

sagivcohen
Автор

Hi
I want to blinking led without delay and with buttons
Button1 blinkig for 250 ms on 250 ms off
Button2 100ms on 100 off
Button3 50ms on 50ms off

mohamedeln
Автор

i find that when i integrate this into some more code it gets really unstable, the led sometimes skips one or multiple blinks

maxxiang
Автор

not accurate by the way time is not stable if you do another program

ehabramzy
Автор

HOW DID U LEARN TO CODE IN ARDUINO
I NEED SOME GUIDE FOR ME TO CODE

nileshgarala
Автор

im always using delay in my codes, because making code 2 times larger does not make any sense :DDD

strssko