Coding

One can code a traffic light using Scratch Junior. One can also add a car that stops and moves according to the red or green traffic lights. We can use Scratch messages for this purpose. Messages define states. When using messages, one must indicate what happens to all Scratch sprites when each message is issued so that one covers all possible states. One must also indicate what happens when the program starts.

In the traffic light case, the program starts by issuing a green message. When a green message is issued, the car moves, the green light appears for a while, then disappears and sends a red message, and the red light disappears. When a red message is issued, the car stops, the green light disappears, and the red light appears for a while, then disappears and sends a green message. Here is the final result and the code used, planed in advance using Scratch icon post-its.

Planning the code using post-its