Answer the question
In order to leave comments, you need to log in
How to flash LEDs without delay?
Hello,
Two LEDs, flashing with a strobe effect. Please help with an example of how to rewrite the code to get rid of delay() ?
long previousMillis = 0;
void loop () {
flash();
}
void flash () {
if (millis() - previousMillis > 840) {
previousMillis = millis();
digitalWrite(NoSoundLed, LOW);
digitalWrite(NotifyLed, HIGH);
delay(120);
digitalWrite(NotifyLed, LOW);
delay(120);
digitalWrite(NotifyLed, HIGH);
delay(120);
digitalWrite(NotifyLed, LOW);
delay(120);
digitalWrite(NoSoundLed, HIGH);
delay(120);
digitalWrite(NoSoundLed, LOW);
delay(120);
digitalWrite(NoSoundLed, HIGH);
delay(120);
digitalWrite(NoSoundLed, LOW);
}
}
Answer the question
In order to leave comments, you need to log in
if ((millis() % 1000) > 500) digitalWrite(Pin, HIGH);
else digitalWrite(Pin, LOW);
void setup() {
pinMode(13, OUTPUT);
}
unsigned int millisCounter = 0;
void migalka(void)
{
millisCounter = millis() % 8400;
if(millisCounter < 1200 )
{digitalWrite(13,1);}
else
if(millisCounter < 2400)
{digitalWrite(13,1);}
else
if(millisCounter < 3600)
{digitalWrite(13,1);}
else
if(millisCounter < 4800)
{digitalWrite(13,0);}
else
if(millisCounter < 5200)
{digitalWrite(13,0);}
else
if(millisCounter < 6400)
{digitalWrite(13,1);}
else
if(millisCounter < 7200)
{digitalWrite(13,1);}
else
{digitalWrite(13,0);}
}
/*основной цикл*/
void loop() {
migalka();
}
delay
can be rewritten like this:
long previousMillis = millis();
while (true) {
if (millis() - previousMillis >= 120) {
break;
}
previousMillis = millis();
}
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
digitalWrite(NotifyLed, HIGH);
//TODO If delay is too low, use Ctrl+C Ctrl+V
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question