This lesson is for those who have the Classroom Pack.
The unique Classroom pack includes multiple smart racing tracks that can each communicate with each other.
In this lesson, we will explain how the communication between the tracks works and use it to create an impressive synchronized start.
By synchronized start, we mean that cars on multiple tracks will start at the same time!
How do the tracks communicate?
It is hard to notice at first , but the black board which holds the Arduino has a hidden component under it (this component is only included in the classroom pack kits) – It is called NRF24L01.
What is NRF21L01?
The NRF21L01 is an electronic component that can send and receive messages using radio waves.
Let’s break down this sentence into understandable details:
First, for simplicity, let’s call this component “NRF” instead of NRF21L01.
NRF can send and receive messages, meaning it can communicate with other NRFs. In our case, each Arduino has an attached NRF that it can ask to send and receive messages.
In other words, our Arduinos cannot communicate with each other unless they have a little help from the NRF device.
NRFs communicate by using radio waves. Since this is not a physics lesson, we will not be able to explain how waves work, but in general, radio waves are invisible and can penetrate through walls!
We are already familiar with another type of communication – Infra-Red light (remember we have used a remote control to implement a remote start in lesson 11?).
Radio communication is much more advanced compared to IR communication because it doesn’t require a line of sight; our tracks will be able to communicate even if they are in separate rooms!
Our smart race track has two lanes, but what if we want to race with four or even six cars?
We can use multiple race tracks in such a case, but we need to make sure each race starts at exactly the same time. Also, to make the race fair, we need to make sure that all the tracks are the same length.
How will we implement a synchronized start?
Let’s plan the general idea before starting to code:
1) We will have a “master” track; this will be the track that will decide when to trigger the start of the race. The other tracks will be called “slaves.”
2) When the start button is pressed on the master track, he will send a message to the slave tracks and tell them to start the race.
Master-slave is a very popular concept in robotics because different robots or devices need to be synchronized, and this concept simplifies things significantly.
For example, in our case, only one track needs to be “smart.” The other tracks are just getting messages and doing as instructed.
The slave tracks won’t even know how the race was started; whether with a button or a remote. All they need to do is listen to messages and start the race when they are told.
Since we will be using the master-slave concept, we will need to code the master track differently than the slave tracks.
Let’s start with the master track code first, so choose which track will be the master.
The track you choose will trigger the race when its start button is pressed and will also be the track that plays the MP3 files.
We will make some changes to the code from Lesson 18. If it isn’t already open in your mBlock, download the code from lesson 18 and open it.
Step 5: Setup Master as a sender
We need to make some changes to the code to send messages to the slave tracks.
First, we need to let Arduino know that we are going to be using the NRF (it is physically connected to pins 7 & 8 of the Arduino).
Add the NRF as a SENDER block as shown in the image below (the NRF menu is located at the very bottom of the blocks menu, so you might need to scroll down in order to see it).
The SENDER block will tell Arduino to setup the NRF component and get ready to send messages.
Now we need to tell our master Arduino to use the NRF component to send a START message once the race is started (when the start button is pressed).
Change your code as shown in the image below:
That’s it! Our master code is ready! UPLOAD the code to the master track. Then, disconnect the master track and connect the slave track instead.
You can use as many slave tracks as you want for the following steps, and they will each use the same slave code.
Let’s first start with one slave track. Later you can upload the final slave code to multiple tracks, and they will all work in the same way.
Step 8: Clean the slave code
We need to remove some blocks that are only relevant to the master code because the salve code should not have them. We will be removing the MP3 blocks from the slave code as it doesn’t make sense to have multiple MP3 players playing at the same time.
Make the changes as shown in the images below (remove the blocks marked in red):
We will need to tell the slave Arduino to setup the NRF and get ready to receive messages.
Change your code as shown below:
Step 10: Listen to messages
Now, let’s tell that slave Arduino that instead of listening to the start button we want him to listen to NRF message and start the race once a “START” message is received.
Change your code as shown below:
Step 11: Upload the slave code
Upload the slave code.
Step 12: Add batteries and test!
The process of adding batteries is described in lesson 4.
This is the result you should see:
If everything worked out you can upload the slave code to more slave tracks.
There is nothing you should change with the master track, he will just continue to send messages to any active slave out there.
Download the final code
Have any issue with the code changes? We have prepared the final code for you. Make sure to upload the master code to the master track and the slave code to the slave tracks.
Thank you for being with us on this exciting journey and we hope that you had fun during these lessons!