- Traffic Lights Kit
- Move Motor Sensor Car
Traffic Lights and Car Communication
In this project we program a set of traffic lights and an autonomous car to communicate with each other so that the car knows when to stop and when to go!
The aim of this project is to code a set of traffic lights to run through a sequence and broadcast what light is been displayed. Then a robot car will receive the broadcast and decide whether to stop or go.
You will need the following for this project:
- 2 Microbits (1 that goes in the car and 1 for the traffic lights).
- A Move Motor car.
- A Traffic Lights kit.
Put one of the Microbits into the car and attach the other Microbit to the traffic lights using the screws.
We will need to create 2 code projects:
- for the Microbit that will be connected to the traffic lights.
- and another for the Microbit that will be inside the car.
Let's create the code for the traffic lights first. Go to the https://makecode.microbit.org website and create a new project. Call it "Traffic Lights" or something that let's you know that it's the project for the traffic lights.
Once you've created the new project, add the following code to set the radio group to 1. We will do the same later on the for the car project.
There are custom code blocks that we use to program our traffic lights kit. To add them to our toolbox we need to add the "stopbit" extension.
Search for the "stopbit" extension and then click on the traffic lights kit picture.
This will add the "Kitronik STOP:bit" category of code blocks to your toolbox.
To add an extension follow these steps:
- Click on 'Advanced' in the toolbox
- Click 'Extensions'
- Type the name of the extension in the search box
- Click on the extension in the search results
- The extension and all it's blocks will be added to your toolbox
We're going to program the traffic lights to go through the following sequence:
- "Go" state for 10 seconds (green light on).
- "Ready to Stop" state for 3 seconds (yellow light on).
- "Stop" state for 5 seconds (red light on).
Add the following code to make the traffic lights forever go through the sequence.
Now let's also broadcast what current state the traffic lights is in. Add the following new code to:
- send the message "go" when the light is green.
- send the message "ready to stop" when the light is yellow.
- send the message "stop" when the light is red.
Once you have added the code, download it onto the Microbit that is attached to your traffic lights.
Give your project a name and send it to your Microbit by following these steps:
- Type "your project name" into the Project Name box.
- Connect your Microbit to your computer using the USB cable.
- Click the 'Download' button (a file called 'microbit-project-name.hex' will be downloaded to your computer).
- Open the folder on your computer where the file downloaded to.
- Right click on the file.
- Click Send To.
- Click Microbit.
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.