The challenge we have set for you is to create a computer controlled traffic light using a micro:bit and three LEDs.
The picture below shows a traffic light made from three LEDs connected to the micro:bit's edge strip.
Each red wire is connected to one of the numbered pins using coloured micro:pegs.
Red wire from the red light to pin 0
Red wire from the yellow light to pin 1
Red wire from the green light to pin 2
Black wires from the three LEDs all share the GND pin.
The picture on the right shows the traffic light algotithm.
This sequence from 1 to 4 needs to loop or repeat forever.
Lights 2 and 4 in the sequence will need to last just a few seconds.
Lights 1 and 3 in the sequence will need to stay on much longer. The red light will stop cars in one direction and to allow the cars to pass in the other direction on a green light.
In the MakeCode editor, create a new project called Traffic.
Controlling the pins on the micro:bit edge strip is advanced work so you will need to find the Pins blocks in the Advanced tab on the MakeCode editor.
The red digital write pin block is used to turn on an LED.
You must set the pin to P0, P1 or P2, depending on which LED you want to turn on and set the number to 1 for ON or 0 for OFF.
E.g. The block digital write pin P0 to 0 will tun the red LED off.
The code on the right will turn an LED connected to pin2 on for
2 secounds and then turn it of.
The traffic light at a pelican crossing is controlled by the pedestrian.
When the pedestrian presses the button, the light changes to stop the traffic.
The crossing beeps so that the pedestrian knows that it is safe to cross the road.
Use button A on the micro:bit as the pedestrian button.
When button A is pressed
green light changes to yellow light for 2 seconds
yellow light changes to red light long enough for the pedestrians to cross
yellow light comes on with red light for 2 seconds
yellow and red lights change to green
The starting point is an on button A pressed input block.
The rest of the code consists of digital write pin blocks and pause blocks to control the lights to come on in the correct sequence with the correct timings.
It must follow the algorithms shown above.
The crossing needs to beep when the green light comes on so that the pedestrian knows that it is safe to cross the road.
Add some blocks to play a number of beeps while the green light is on.
How many beeps do you think are needed to give people time to cross?
Will 5 beeps be enough?