Arduino Time Keeping

Learning Goals

Students will practice using variable and learn about long and unsigned long variable types

Students will write a short sketch form sctatch.

Students will learn to send information to the Serial Monitor using the keywords Serial.begin, Serial.print and Serial.println

Students will use millis() to explore how the Arduino keeps time.

Most of us learn to tell time when we are quite quite young then we take hours minutes and seconds for granted and don't really think about how we use time. Truthfully our system of hours minutes and seconds is kind of a complicated system of keeping time, we use a base 10 number system but our time is based on 60 seconds in a minute and 60 minutes in an hour but 24 hour is a day in 2 sets of 12 that is a lot of different unit that don't fit easily with each other mathematically.

The Arduino is different, it keeps track of time in one unit the millisecond. Most Arduino programs don't really care what time of day it is so there is no relation to our time and the Arduino's time. If we wanted to connect our time and Arduino we would need to write a program to synchronize them you may do that in a future activity.

In this activity we are just going to investigate how the Arduino tracks and uses time to do this we need to see what the computer is doing Arduino give us a set of commands that we can use for this. they use the connection to the computer to send information from the Arduino to the desktop.

Procedure :

To start this activity connect your Arduino to the computer open the Arduino program (IDE) Open a new program window it should look like this:

All Arduino sketches (programs) have two functions a setup() and a loop(). in the setup function between the {} braces type the line:

Serial.begin(9600); // this command tells the Arduino to set up communication between the Arduino and the computer

The number tells what speed they will send the information at. 9600 is quite slow for computers to exchange information but much faster than we can read or act on the information so it is just fine for us and not demanding on the Arduino.

1. In the next line enter the text

Serial.print (" Hello ");

Upload the sketch to the Arduino When the upload is done you will not see any change, click the magnifying glass icon on the top right corner of the screen to open the serial monitor , a window will open and should display the message hello in the top left.

2. Move the Serial.print line form the setup function into the loop function upload the program again open the serial monitor again

What has changed?

3. Add the letters ln to the end of the serial. print command making it Serial.println, upload your changes, open the serial monitor again

What has changed this time?

4.At the top of your sketch before the setup function add the line

unsigned long timePast=millis();

in the loop function change the word Hello to timePast

Upload and try your sketch, what has changed this time?

5. Add the line timePast=millis(); to the top of the loop function

Remove the " " from the println line to read println(timepast);

Upload and try your sketch, what has changed this time?

6. You should have a stream of number on the screen the numbers are the time since the sketch started in milliseconds. if you want the numbers to count in seconds simply divide the time by 1000.

Change the timePast line to timePast=millis()/1000;

Upload and try your sketch, what has changed this time?

7. The Variable type unsigned long is an integer so it does not display the decimal portion of the division so all you see is the seconds use what you have learned about serial.print and serial.println to create a sketch that serial outputs hours minutes and seconds.