41. Case 37: Traffic Lights#

41.1. Introduction#

In our daily life, many traffic accidents should have been avoided. These traffic accidents are often caused by people not observing the traffic rules, thus, we must understand the traffic rules. For example, the common traffic lights in our lives, red light means not passing, yellow light means warning, and green light means permission to pass. Let’s make a traffic light controlled by the micro:bit.

../../_images/case_01_01.png

41.2. Quick Start#

Materials Required#

Nezha expansion board × 1

micro:bit × 1

LED-red × 1

LED-green × 1

LED-yellow × 1

RJ11 wires × 3

Tips: You may need to purchase Nezha Inventor’s Kit if you want all the above compoents.

Connection Diagram#

Connect the red LED to J1, yellow LED to J2 and green LED to J3 on the Nezha expansion board as the picture shows.

../../_images/case_01_03.png

Assembly Video#

Video reference:https://youtu.be/TmsDDWLcgcs

Assembly Steps#

../../_images/case_step_01_01.png

../../_images/case_step_01_02.png

../../_images/case_step_01_03.png

../../_images/case_step_01_04.png

../../_images/case_step_01_05.png

../../_images/case_step_01_06.png

../../_images/case_step_01_07.png

../../_images/case_step_01_08.png

../../_images/case_step_01_09.png

../../_images/case_step_01_10.png

41.3. Makecode Programming#

Step 1#

Click “Advanced” in the MakeCode to see more choices.

../../_images/case_01_10.png

For programming, we need to add a package: click “Extensions” at the bottom of the MakeCode drawer and search with “PlanetX” in the dialogue box to download it.

../../_images/case_01_11.png

Notice: If you met a tip indicating that some codebases would be deleted due to incompatibility, you may continue as the tips say or create a new project in the menu.

Step 2#

Code as below:#

../../_images/case_01_12.png

Reference#

Link:https://makecode.microbit.org/_M6RiKA642WPP

You may also download it directly below:

Result#

The red LED lights on for 5 seconds and then lights off; the yellow LED lights on for 2 seconds and off then; the green LED lights on for 5 seconds and off then.

../../_images/case-gif-01.gif