When I started programming my Arduino Uno I put my program in the
loop-function. This worked for a while, but when I wanted to run things in parallel it started to get difficult to have the whole program in a single loop.
Specifically when I was working on my countdown timer with a pulsating addressable RGB-LED strip I started to have some timing problems.
In this post I will show you how to use the TimerOne library to create a timer and a function to be called when the timer fires.
Download the TimerOne lib
- Go to https://github.com/PaulStoffregen/TimerOne
- Download the lib by clicking the
- Put the downloaded folder in your
- Rename the folder from
Now you can open your Arduino programming environment and use the library by including it with:
Using the TimerOne lib
This short program counts down for 30 seconds and prints the current count value to the serial monitor.
As you may have noted there is no code at all in the
loop() function. All the logic is done by interupts from the timer.
The timer will fire every second and run the
countDownOneSecond() function. When the counter reaches 0 the timer will stop.