2. Intersection

../_images/10_1.png

2.1. Goal

Make a smart traffic light system which automatically switches between red and green lights among different intersection traffic lights according to the road traffic situation.

2.2. Background

2.2.1. What is a smart traffic light?

Smart traffic light systems are designed to maintain normal operation of the intersection. They automatically switch between red and green lights among different intersection traffic lights according to the road traffic situation.

2.2.2. Smart traffic light operation

../_images/10_2.png

../_images/10_4.png

2.3. Part List

Microbit (1)
Expansion board (1)
Distance sensor (2)
Traffic light (2)
4-pin module wire (2)
Female To Female Dupont Cable Jumper Wire Dupont Line (8)
M2*8mm screw (8)
M3*8mm screw (4)
M2 nut (8)
M3 nut (4)
Screwdriver
Module E (2)
Module I(2)

2.4. Assembly step

2.4.1. Step 1

Attach the Traffic light Module to I1 model using M3*8 screw and nut.

../_images/10_6.png

2.4.2. Step 2

Put the I1 model onto the I2 model.

../_images/10_7.png

2.4.3. Step 3

Assembly completed!

../_images/10_8.png

2.4.4. Step 4

Attach the distance sensor to E1 model using M4*8mm screw.

../_images/10_9.png

2.4.5. Step 5

Put the E2 model on the E1 model.

../_images/10_10.png

2.4.6. Step 6

Assembly completed!

../_images/10_11.png

2.5. Hardware connect

Microbit 1:

Connect the Traffic light LED Module to P0 P1 P2 port
Connect the Distance Sensor to P15 (trig)/ P16 (echo) port


Microbit2:

Connect the Traffic light LED Module to P0 P1 P2 port
Connect the Distance Sensor to P15 (trig)/ P16 (echo) port


Microbit 3:

Car


Microbit4:

Car

2.6. Programming (MakeCode)

2.6.1. Traffic light 1:

2.6.2. Step 1. Set up a new function (TurnRed)

  • Snap pause to wait 1 second

  • Control traffic light yellow on

  • Snap pause to wait 1 second

  • Control traffic light red on

../_images/10_12.png

2.6.3. Step 2. Set up a new function (TurnGreen)

  • Snap pause to wait 1 second

  • Control traffic light yellow on

  • Snap pause to wait 1 second

  • Control traffic light green on

../_images/10_14.png

2.6.4. Step 3. Initialize the program

  • Drag set variable trafficLight to Traffic light pin setting Red P0 Yellow P1 Green P2 to on start

  • Drag radio set group 10 to on start

  • Control traffic light red on

  • Pause for 5s

../_images/10_16.png

2.6.5. Step 4. Keep the green light status for 5 second

  • Call function TurnGreen

  • Pause 1.5s before TurnGreen

  • Pause 3.5s after TurnGreen

../_images/10_17.png

2.6.6. Step 5. Keep the red light status for 5 second

  • Call function TurnRed

  • set variable second to 50

  • While second > 0, snap pause to 0.1 second and change second by -1.

../_images/10_19.png

2.6.7. Step 6. Get distance value

  • Snap if statement into while loop, set get distance unit cm trig P15 echo P16 < 20

../_images/10_21.png

2.6.8. Step 7. Control traffic light 2 and car by sending radio number

  • Drag radio send number 1 into if

  • Drag radio send number 0 after TurnGreen

  • Drag radio send string “R” before TurnGreen

  • Drag radio send string “G” before TurnRed

../_images/10_23.png

Full solution:
https://makecode.microbit.org/_Pm2eWtRwERU8

2.6.9. Traffic light 2:

2.6.10. Step 1. Set up new functions

../_images/10_25.png

2.6.11. Step 2. Initialize the program

  • Drag set variable trafficLight to Traffic light pin setting Red P0 Yellow P1 Green P2 to on start

  • Drag radio set group 10 to on start

  • Control traffic light green on

  • Pause for 5s

../_images/10_27.png

2.6.12. Step 3. Control traffic light and car by receiving different number

  • Snap if statement into on radio received receivedString

  • Set receivedString =“R” and call TurnRed

  • Set receivedString =“G” and call TurnGreen

../_images/10_29.png

2.6.13. Step 4. Keep the red light status for 5 second

  • After TurnRed, set variable second to 50

  • While second > 0, snap pause to 0.1 second and change second by -1.

  • Before TurnGreen, pause 1.5s

  • After TurnGreen, pause 3.5s

../_images/10_31.png

2.6.14. Step 5. Get distance value and control the car

  • Snap if statement into while loop, set get distance unit cm trig P15 echo P16 < 20

  • Drag radio send number 3 into if 將廣播發送數字1拖入 if 句式

../_images/10_33.png

Full solution:
https://makecode.microbit.org/_fudKTM57XTKA

2.6.15. Car 1:

2.6.16. Step 1. Set radio set group at start position

  • Drag radio set group 10 to on start

../_images/10_35.png

2.6.17. Step 2. Control car by receiving different number

  • Snap if statement into on radio received receivedNumber

  • Set receivedNumber =1 and make the car stop

  • Set receivedNumber=0 and make the car move forward 設

../_images/10_37.png

Full solution:
https://makecode.microbit.org/_1o1iRhg3qfRF

2.6.18. Car 2:

2.6.19. Step 1. Set radio set group at start position

  • Drag radio set group 10 to on start

../_images/10_39.png

2.6.20. Step 2. Control car by receiving different number

  • Snap if statement into on radio received receivedNumber

  • Set receivedNumber =3 and make the car stop

  • Set receivedNumber=2 and make the car move forward

../_images/10_41.png

Full solution:
https://makecode.microbit.org/_YmH3PrRA14pC

2.7. Result

Distance sensor is used to detect whether there are cars passing by. If it is yes (i.e., there are cars passing by in the intersection), it sends a signal to the cars in the corresponding direction and alerts them to stop, in order to maintain a normal traffic flow of the intersection.

2.8. Think

Challenge question:

In case that it is a intersecion, there should be four traffic lights for four different directions. Can you write a program in that case?