This example shows the minimal code to blink the built-in LED in Arduino Uno board.
- SimpleBlink.cpp Main program
- Install Arduino IDE.
- Download the AsyncBlinker.zip library and import in your Arduino IDE.
- Restart the Arduino IDE and open the example in menu (File > Examples).
- Compile and load the example to your board.
- Install Platformio Core.
- Execute
pio run -t upload
to load the example to your board.
This example shows how to:
-
Write a callback function that switches a LED:
void blink_my_led(bool enable) { digitalWrite(PIN_LED, enable ? HIGH : LOW); }
-
Declare the AsyncBlinker instance to execute the callback function:
AsyncBlinker blinker(blink_my_led);
-
Indicate to the AsyncBlinker to start the blinking:
blinker.start();
-
Execute the non-blocking code in the main loop to blink the led:
blinker.tickUpdate(elapsed_millis);
Yes! But this blink is non-blocking (no delays). You can do more things in the main loop and the blinking continues.
Of course, you mustn't use another code blocking the execution (delaying) or the blinker won't update the state...
The code also shows how to get the system time and calculate the elapsed milliseconds in the main loop of the Arduino framework:
static unsigned long last_millis = 0;
unsigned long now_millis = millis();
unsigned long elapsed_millis = now_millis - last_millis;
last_millis = now_millis;
This example is using the default blinking behaviour:
- 500 ms enable
- 500 ms disable
- start again
Go to next example and learn about blinking intervals.