Lesson 10

Announcing the winner

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.

Crossing the finish line

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.

Step 1: Calculating right crossing value

From our experience, an excellent way to calculate your crossing value is to use the following formula: Maximum sensor value without a car + 50 = Crossing Value


What is the maximum sensor value without a car?

At the end of the last lesson, we created code that prints the value from the sensor (on the track’s right side) every second. Watch the right-side 7-segment display change its value. The highest number you see is the maximum sensor value without a car.
 

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.

Step 2: Calculating left crossing value

Now we will calculate the crossing value of the sensor on the left side of the track.

The sensor on the left is connected to pin 7, while the sensor on the right is connected to pin 6.

To print the left sensor value on the left 7-segment display, let’s make the following changes to our code:

 

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.

Step 3: Waiting for crossing

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:

 In our new “repeat until” loop, let’s first tell Arduino to wait a short amount of time (0.01 second).
 

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.

Step 4: Adding “OR” operator

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:

  • “if our left sensor’s value is higher than its crossing value”

    OR

  • “if our right sensor’s value is higher than its crossing value.”

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!

Step 6: Improvements

When a winner is announced, let’s play a melody! It will be much better than just printing numbers on our 7-segment displays.

Let’s also erase the 7-segments displays at the beginning of each race.

And lastly, let’s close the gate when our Arduino announces the winner!

Follow the video below and make the same changes to your code:
 

 

Now, try playing a few more races! Do you feel the game is much better now?

Step 7: Automatic sensor calibration

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!

 

Finish! (not really)

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?

  • Trigger the race start with a remote control
  • Display the number of the race on the screen
  • Display the time it took to the cars to complete the race

Before starting the bonus lessons, take a break and play! Enjoy the racing game you programmed!