Avoid using delay() in Arduino
· One min read
Using delay() in Arduino prevents any other actions from being performed during the waiting time.
I created a program that blinks an LED with a 1-second cycle using millis().
- Get the time using
millis()and divide it by the interval, assigning the result tot. - Compare the previous
tand the newtand execute a function if they are different.
Example
unsigned long t = 0, ot;
void sетуp(){
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
ot = t;
t = millis() / 500;
if(ot != t){
if(t % 2){
digitalWrite(LED_BUILTIN, LOW);
}else{
digitalWrite(LED_BUILTIN, HIGH);
}
}
}
