We’re now going to look at a very useful
Arduino function that helps us keep track
of time.
Microcontrollers require a clock pulse for
correct operation and this pulse helps synchronize
all the events and different modules. The
Arduino UNO and NANO contain a 16MHZ clock
source which generates a clock pulse that
is simply a steady and precise square wave.
The cycle time of this wave is 62.5ns, and
we can keep track of time by counting the
number of such clock pulses.
Microcontrollers contain timer modules that
can do this along with many other interesting
functions that we will see in future videos.
For now, let’s try to look at the importance
of timers by analysing this sketch. It simply
blinks the red LED every second while only
switching ON the green LED when the switch
is pressed. If you press and release the switch
multiple times, you will notice that it doesn’t
always respond quickly enough and that is
because we have used the delay function which
is blocking in nature. What this means is
that when the microcontroller is executing
the delay function, it cannot execute any
other commands and is thus stuck waiting for
the delay to lapse. This is noticeable as
we’ve added a delay of 1 second.
To eliminate this and make the microcontroller
responsive to our inputs, we would need to
write non-blocking code and using the millis
function is one way to do this. The millis
function returns the number of milliseconds
that have passed since the microcontroller
was switched ON. It uses a hardware timer
under the hood but the Arduino libraries set
this up for us and we don’t have to worry
about it. The function returns an unsigned
long value and like any variable, this too
has a limit. The returned value will roll
back to 0 after approximately 50 days of continuous
running. Make sure your sketch can handle
this roll over if you plan on running it continuously
for that long.
Now that we have a method of obtaining the
current time, we can compare this with a known
reference to determine how much time has elapsed
between events. This can then be used to keep
track of time and execute statements.
Here’s what the updated sketch would look
like once we eliminate the blocking delay
function.
We use three variables to keep track of time
– previousMillis, currentMillis and interval.
The interval is defined as a pre-processor
directive. currentMillis holds the current
time in milliseconds, while previousMillis
is the reference that is used to store the
previous time (in milliseconds) since the
event was triggered last.
If the difference between the current and
previous millis is greater than the interval,
then we know that sufficient time has passed
and we can then blink the LED. We also need
to store the current time as the reference
and we can do this by assigning the currentMillis
value to previousMillis.
Let’s compile and upload the sketch – the
green LED is much more responsive now
We’ve now seen how to write two different
sketches that give us a similar end result
– one is responsive while the other isn’t.
It is always a good habit to get into reviewing
your logic to make sure that the code you
write will work in all cases. It’s easy
to write code that uses blocking functions
but many times, it’s worth taking the additional
effort to write non-blocking code, particularly
when we are also using communication modules
like the serial port.
Now that we have a basic understanding of
writing Arduino sketches, let us move on to
interfacing modules and working on projects
as that’s the best way to learn electronics.
Don’t forget to like, share and comment
if you liked this video.
I will see you in the next one.
