Microbit Inventor Project Robotics
Hard
60 mins
195 points
What you need:
  • Computer/laptop
  • Microbit
  • 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!

1 - Introduction

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:

  1. 2 Microbits (1 that goes in the car and 1 for the traffic lights).
  2. A Move Motor car.
  3. A Traffic Lights kit.

Put one of the Microbits into the car and attach the other Microbit to the traffic lights using the screws.


2 - Program the traffic lights

We will need to create 2 code projects:

  1. for the Microbit that will be connected to the traffic lights.
  2. 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.

radio.setGroup(1)
If this project is being done in a classroom with multiple cars then make sure each traffic lights and car set uses a different radio group. Otherwise the signals will get crossed!

3 - Add the Stopbit extension

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.


4 - Program the sequence

We're going to program the traffic lights to go through the following sequence:

  1. "Go" state for 10 seconds (green light on).
  2. "Ready to Stop" state for 3 seconds (yellow light on).
  3. "Stop" state for 5 seconds (red light on).

Add the following code to make the traffic lights forever go through the sequence.

5 - Broadcast the state

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.

Join our club 😃

To view the remaining 3 steps and access hundreds of other coding projects please login or create an account.

Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more