Chapter 7. Managing the Time Domain

We have come a long way, and at this point we have seen almost everything regarding digital and analog inputs and outputs. But Arduino still has some unseen characteristics that come in very handy when developing our projects. Controlling the time is one of them and a very important feature for a good number of projects.

In this chapter, we will learn about the functions in the Arduino library to control the time and will also discover sound generation and the use of speakers and buzzers. Every timer has an alarm, doesn't it?

Time control functions

The Arduino library has four functions that allow us to manage the time. We have already seen one of them, the delay() function, which we have used from our first sketch to stop the execution of the code for a short period of time.

As we saw in Chapter 3, Interacting with the Environment the Digital Way, the delay() function accepts only one parameter: the desired number of milliseconds to pause. For most projects, this resolution will be fine, but there can be situations where a millisecond is too much.

For these kinds of problems, the Arduino library also offers the delayMicroseconds() function that, as you can imagine, pauses the code execution for just the number of microseconds that you set as its only parameter.

A microsecond is a millionth of a second, or to put it another way, there are a thousand microseconds in a millisecond and a million microseconds in a second.

Due to restrictions in the Arduino architecture, the maximum delay the delayMicroseconds() function can produce is around 16,000 ms. In case you should need a larger delay, you can always use your old friend, the delay() function.

Stopping versus accounting

Both functions that we already know, delay() and delayMicroseconds(), stop the code execution for a desired amount of time, but we don't always want to stop. In some cases, we just want to know what time is it, or if a certain amount of time has passed by.

For accounting purposes, the Arduino library offers two other functions that don't stop the code, but simply return a value representing a time. We can use this to store in a variable to operate with it by making calculations or by taking decisions based on this time.

These two functions are as follows:

  • millis(): It returns us the number of milliseconds since the sketch execution began
  • micros(): It returns the number of microseconds elapsed from the beginning of the sketch execution

Both return a value of type unsigned long, which means that they use 4 bytes (32 bits) to store it and that the maximum value they can store is 4,294,967,295 or 2^32 - 1.

Due to this maximum value, if we keep our sketch running for a very long time, the value returned by these functions may overflow, that is, may return to 0 and start counting again.

In the case of the millis() function, the overflow will happen after approximately 50 days and in the case of the micros() function around 70 minutes after the beginning of the sketch execution, so take this into consideration when using these functions in long runner projects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset