Long comments, URLs, and code tend to get flagged for spam moderation. No need to resubmit.

ALL comments submitted with fake or throw-away services are deleted, regardless of content.

Don't be a dweeb.

Leave a comment

33 thoughts on “Blink without delay() explained line-by-line

  1. Thank you for the great explanation..

    What kinda always annoyed me as the noob i am, is that the Millis method works in reverse as presented… It starts with a LED Off state, then it counts up Millis to reach the set interval value and the LED switches On…

    Searched quite a while and even tho i wasn’t the only one complaining about it i still couldn’t find a straight foreward answer… Anyway I figured out myself to fix this issue, it actually is very simple… I changed the “unsigned long previousMillis = 0;” into a equal number as in the interval but then as a minus.

    That would be

    unsigned long previousMillis = -1000;
    unsigned long interval = 1000;

    Then it starts with a LED on and so on…

    Cheers…

    • The most easy way to do this is to assign the opposite value in the variable definition
      the given code has in line 2
      int ledState = LOW;

      and to change this to
      int ledState = HIGH;

      and you are done

      best regards Stefan

      • Thank you for the fast reply…

        I think i failed to mention i was playing with an ESP8662 NodeMCU board and i already tried to change line 2 but it doesn’t work. It simply stays as is, whatever HIGH or LOW is changed..

        Till now the only way to get around it, is as what I described previously.

        Cheers..

  2. Hi I got pointed to this tutorial through a link in the Arduino-Forum. Very well explained. I don’t have an overview about your tutorials yet. Ideas about other beginners (or a bit advanced beyond beginner) might be

    – when and how to use interrupt service-routines or when to use alternatives

    . use selfexplaining names everywhere

    – PString and/or SafeString als alternatives to “String”

    – ESP826/ESP32-stuff (especially ESP-NOW)

    best regards Stefan

  3. Hola.
    Me podría ayudar a hacer un contador con un pulsador de por ejemplo 10 pulsos y en este pulso se me active una salida durante un tiempo predeterminado. Luego de ese tiempo desactive esa salida, ponga a cero el contador y siga contando pulsos sin que afecte a la ejecución del código independientemente de la velocidad de los pulsos

  4. Why not define the ledstate as byte = 0 and remove the if / else loop and replace it with ledstate = (1 – ledstate).. it saves some ram and program space and works just the same.

  5. Hi,
    Could you tell me why this program does’t show anything?

    #include <LiquidCrystal>
    lcd(7, 8, 9, 10, 11, 12);
    int buzzer = 5;
    int x = 1;
    unsigned long past = 0;
    unsigned long s11 = 150;
    unsigned long s2 = 200;
    unsigned long s22 = 250;
    unsigned long s3 = 300;
    unsigned long s4 = 400;
    unsigned long s5 = 500;
    unsigned long s7 = 700;
    
    
    
    void setup() {
    
      lcd.begin(16, 2);
      pinMode(buzzer, OUTPUT);
    
    
    
    }
    
    void loop() {
      unsigned long c = millis();
      if (c == 1000)
      {
        lcd.print("Happy birthday");
        lcd.setCursor(0, 1);
        lcd.print("   to you!");
      }
    
      if (c == 3000);
      lcd.clear();
      if (c == 3500)
      {
        lcd.print("Happy birthday");
        lcd.setCursor(0, 1);
        lcd.print("   to you!");
      }
      if (c == 6500)
        lcd.clear();
      if (c == 7000)
      {
        lcd.print("Happy birthday");
        lcd.setCursor(0, 1);
        lcd.print("dear...");
      }
      if (c == 11000)
        lcd.clear();
      if (c == 11500)
      {
        lcd.print("Happy birthday");
        lcd.setCursor(0, 1);
        lcd.print("   to you!");
      }
    
    
    }
    
    • The problem is that you are looking for an exact value of millis(). You will probably never get so lucky to be checking for a value of “1000” at the EXACT moment millis() is that value.

      The right way is to use a ranged comparison like “less than” or “greater than.” However, if you just change the code to be: if (millis() >= 1000) then once millis() is more than 1000, it’ll always print that statement. That means you need to add some flags.

      In a case like this where you are trying to print a sequential message, you could just use a variable to sequence.

      int statement = 0;
      if ((millis() >= 1000) && (statement == 0)) {
      	statement = statement + 1;  // increment the statement variable
      	// print whatever
      }
      

      Then on your next check you would do:

      if ((millis() >= 3000) && (statement == 1)) {
      	statement = statement + 1;  // increment the statement variable
      	// print whatever
      }
      

      and so on.

  6. Hi There, great line by line, really helped me understand better.

    Do you think a logical AND (&&) can be added to your 1st IF statement to check for time but also a button state ? I need to turn “blink” a remote relay via Xbee but check that the user pushed a button to activate it 1st. Basically the relay will blink a light attached to it, but the user needs to have switched on the light to have it blink, otherwise it should just be off.

    Thanks