Blink Code Version using On Board LED using millis

I often revise my code several times also have several NodeMCU boards with different programs installed. I finally figured out how to blink the on board LED with the version of code installed on that board.

Using millis and % (remainder operator) the on board LED will now blink ‘version blinks’ pause ‘code update blinks’. So 1 fast and 4 long blinks in approximately 500 ms at a 50% duty cycle.

Simple changes will change the duty cycle and blink duration. I used 4800 ms with 2400 half cycle because 2400 is easily divisible by 1, 2, 3, 4, 6, 8, 12.

For version 0-1 the numbers would be: %2400<2400 (no fast blinks) and %2400<1200 (1 blink on for 1.2 ms).

Use unsigned long variables as shown to minimize millis rollover

Millis.ino (1.12 KB)


Now, it would make sense to:

  1. Learn to format the code so that it human-readable. What you have currently is a mess.

  2. Stop overloading the code with massive amounts of pointless braces like {led1state=1;} or even {(led1state=0);} (Wow! Why?!). The problem is even worse because of poor formatting.

  3. The % based technique is a step in the right direction. Another step in the right direction would be to learn not to do things that are unnecessary. For example, your loop performs digitalWrite(2, led1state); on each iteration (!). This is completely unnecessary. There's no need to make a digitalWrite call if you know that the pin is already in the proper state. Do digitalWriteonly when you really need to change the pin's state.

This is not a big deal actually in a program that is so simple (i.e. a program that has nothing else to do). But it is a much bigger deal in more serious code.