Now we know how to use the 7-segments displays and the infrared sensors! But, they are not part of our racing game yet.
In this lesson, we will use our new knowledge to display which car is the race’s winner.
We know that Arduino is reading values from our infrared sensors. When a car is crossing the finish line, the sensor’s value will be higher. But we need to decide which value is high enough to make sure that a car has crossed the finish line. Let’s call this value the “crossing value. “
Because the cars cross the finish line at a relatively high speed, the sensor will see only some of the light produced by the infrared LED. In other words, the faster the car, the less light the sensor sees.
If we set a crossing value that is too high, we might miss fast cars. And if we set a crossing value that is too low, Arduino may think a car crosses the finish line when the sensor detects movement or light in the room.
In the video, 77 is the highest number we wee, so this will be our maximum sensor value without a car.
Your value will probably be different, depending on how much light the sensor can detect in your room.
According to the formula our correct crossing value is 127 (77 + 50 = 127).
Calculate your sensor’s crossing value and write it down.
This is the result you should get:
In our test, the highest value we see is 70. So the left crossing value is 120 (70+50=120).
Calculate your left-side crossing value and write it down.
Now that we have our crossing values, we need Arduino to wait until our sensor’s values are higher than our crossing values.
Let’s change our code to tell Arduino to check the value of both sensors consistently. If the sensors’ values are not higher than the crossing values, Arduino will wait and check again.
We will use the “repeat until” block (this block creates a loop. Loops repeats code until something happens). For our block, we will repeat the code that tells Arduino to wait, then do something when a car crosses the finish line.
Change your code as shown in the video:
After this, we’ll also make Arduino check the value of the sensors.
As long as the values are not higher than our crossing values, the loop will wait and repeat itself forever.
When one of our sensors’ values is higher than the crossing value, we want the loop to stop (and more code to continue).
To make this loop work for both sensors (left and right), let’s set the loop’s condition to be:
The OR operator allows us to create a more customized condition that can include both sensors at the same time!
Add the OR operator and update the condition according to the video below:
In the video, we used 120 and 127 as our crossing values. Make sure you use the values that you calculated earlier in this lesson!
Quick reminder: A7 is the left sensor, A6 is the right sensor!
Great! After the race begins, our loop will wait until one of the cars cross the finish line.
What shall we do after a car is crossing the finish line? We need to announce the winner!
Step 5: Who won?
We know that Arduino will wait until one of the cars will cross the line, but we also need to know which car was the one that crossed the finish line, we will add another set of blocks in order to check which sensor was above the crossing value.
We already used the if -> then block, now we will use a more advanced block if -> then -> else
Make the changes to the code according to the video below (we will explain the code soon):
What does the if -> then -> else block do?
The block we added is checking our condition for our sensor crossing values.
If the condition is met (true), Arduino will execute a specific code.
If not, Arduino will run a different specific code:
Arduino will only run the else code if the condition is not met.
If the waiting loop stops, we know that one of the cars has crossed the finish line.
How do we know which one crossed the finish line?
If the sensor on the right does not have a value higher than the crossing value, the sensor on the left must be the winner!
Now you can finally test the code and play!
Make sure to come back to this lesson after a few races. We are going to add some improvements to our code!
It is important to note that the lighting in your room affects the sensors. You may need to update the crossing value when the lighting changes. (The lighting may change from morning to evening, from turning on more lights and taking the track to a different room)
When you adjust the sensors’ crossing values, what you are doing is called “calibration.”
Calibrating the race track every time you want to play is not fun. Luckily, we can make a few changes to the code to add automatic calibration!
Download the code with the automatic calibration below. The code is using variables, which we have not learned about yet. We won’t explore variables right now, but you can download it and use it!
Well done! You have now completed the base code of the PlayRobotics smart-racing workshop!
All the electronic components are working great. But there are still some more advanced things we can do…
We have added several bonus lessons that improve and even make your racing game more fun!
The bonus lessons are a little bit more advanced, but we know you can handle the challenge!
What will we do in the bonus lessons?
Before starting the bonus lessons, take a break and play! Enjoy the racing game you programmed!