Microbit Robotics
Beginner
60 mins
Teacher/Student led
125 points
What you need:
Microbit
Microbit
Traffic Lights Kit
Phillips Screwdriver

Traffic Light Reaction Game

In this step-by-step lesson, you'll create a fun traffic light reaction game using your micro:bit and STOP:bit Traffic Lights. You'll learn to program the lights, create variables, and measure reaction times. By the end, you'll have a game that challenges you and your friends to react as quickly as possible when the light turns green.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Create a New Project

    In this game, you'll create a traffic light reaction game using your micro:bit and STOP:bit Traffic Lights.

    The game works like this: when the STOP:bit traffic light turns green, you have to press a button on your micro:bit as quickly as possible. The micro:bit will measure your reaction time and display it on its LED matrix. You can challenge your friends to see who has the fastest reaction time!

    Make sure to attach the STOP:bit traffic lights to your micro:bit before starting the project.

    Go to MakeCode for micro:bit and create a new project. Name it 'Traffic Light Reaction Game'.


    2 - 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.



    3 - Create the Variables

    In this step, we will create three variables that will help us measure the reaction time.

    • The 'startTime' variable will store the time when the green light turns on.
    • The 'endTime' variable will store the time when the button is pressed.
    • The 'reactionTime' variable will store the difference between the two, which is the actual reaction time.

    4 - Button A Pressed

    In this step, we'll set up the code for when button A is pressed. This will turn the traffic light to red, wait for a random amount of time, and then turn it green. We'll also record the start time. Add the following code:

    Here's a breakdown of what the code does:

    • startTime = 0, endTime = 0, and reactionTime = 0 reset the start time, end time, and reaction time variables to 0.
    • Make Traffic Light state to [Stop] turns the traffic light to red (stop).
    • pause(pick random(1, 10) * 1000) waits for a random amount of time between 1 and 10 seconds.
    • Make Traffic Light state to [Go] turns the traffic light to green (go).
    • startTime = runningTime records the start time when the light turns green.

    5 - Button B Pressed

    In this step, we'll set up the code for when button B is pressed. This will record the end time of your reaction and calculate the reaction time by subtracting the start time from the end time. The reaction time will be the time it took you to press button B after the traffic light turned green.

    Add the following code:

    input.onButtonPressed(Button.B, function () {
        endTime = input.runningTime()
        reactionTime = endTime - startTime
    })

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. 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