In the last lesson, we created a round counter and displayed it on the LCD screen. Now, let’s add another cool feature to our race.
We will show the time it took for the winner to complete the race on the 7-segment display!
How will we do this?
To show the time, we need a clock or stopwatch that we can turn on at the beginning of the race. Then, we will stop it when the winner crosses the finish line.
Sadly, Arduino doesn’t keep time like a regular clock, but it does have a feature that will help us achieve our goal.
Arduino has an internal clock that counts the number of milliseconds that have passed since turning it on.
Each second is 1000 milliseconds, meaning that one second after turning on our race track, Arduino will count to 1000. Then, 2000 after two seconds, and so on.
To use this feature, we will need to use the “Milliseconds since ON” block, located in the Time menu:
How will we use this block to calculate the winning time?
The Milliseconds since ON block will give us the “millisecond count,” which is the number of milliseconds that Arduino has counted since turned on.
1) At the start of each race round, we will store the millisecond count in a variable called “round start milliseconds” ,giving us the time since turned on until the start of the race round.
2) At the end of each race round, we will store the millisecond count in a variable called “round end milliseconds“, giving us the time since turned on until the end of the race round.
3) Then, we can easily calculate the race round time by subtracting the variables:
round time = round end milliseconds – round start milliseconds
Step 1: Creating the variables
Create the variables as shown in the video below:
We will update the round start milliseconds variable at the beginning of each round.
Follow the video below:
We will update the round end milliseconds variable at the end of each round.
Follow the video below:
Now let’s print the time it took the winner to complete the race on the 7-segment display located on the winner’s track.
Instead of displaying the number “1” (as it is now), we will replace this number with the winner’s time.
The losing track will still show the number “2,” so we will not change this.
Make the changes according to the video:
This is the result you should get:
Well done! You have just finished the third bonus lesson!
In the next lesson we will make more advanced improvements to our race.