A
A
ANVARD2016-12-08 07:47:47
Arduino
ANVARD, 2016-12-08 07:47:47

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);
  }
}

Thank you for attention!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Gusev, 2016-12-08
@Sanchogus

if ((millis() % 1000) > 500) digitalWrite(Pin, HIGH);
else digitalWrite(Pin, LOW);

We take the remainder of dividing the current time (since the start of the MK, in ms) by 1000.
If it comes out more than 500, we turn on the diode, otherwise we cut it down.
Let's get a switch every 500 ms.
We paste it where it will be often checked and enjoy.
For your case, here is such a function and call it more often in the main loop.
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();
}

R
Rou1997, 2016-12-08
@Rou1997

delaycan be rewritten like this:

long previousMillis = millis();
while (true) {
  if (millis() - previousMillis >= 120) {
    break;
  }
  previousMillis = millis();
}

Or like this:
Or like this:
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

Enough or more to come up with?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question