Completing the model
Here is our description:
Comment | Red light | Yellow light | Green light | Wait light | Red man | Green man | Buzzer |
Cars going normally along road. | Off | Off | On | Off | On | Off | Off |
The button is pressed and you wait. | Off | Off | On | On | On | Off | Off |
Eventually, the lights start to change. | Off | On (2 secs) | Off | On | On | Off | Off |
Now you can start to cross. | On | Off | Off | Off | Off | On (flashes) | Beeps |
After 5 seconds, the lights start to change. | Off | On (flashes) | Off | Off | Off | On (flashes) | Off |
Cars going normally along road. | Off | Off | On | Off | On | Off | Off |
And here is the code for the complete cycle (without flashing lights or the buzzer):
Q1. The next job is to add a buzzer at the right time. Replace this line in the code for crossing the road:
time.sleep(5) # 5s to cross the road.
with this block of code:
#add a buzzer
for seconds in range(10): # do the following code 10 times
winsound.Beep(600,250) #beep for a quarter of a second
time.sleep(0.25) #wait for a quarter of a second
In Windows, winsound.Beep(x, y) creates a beep. x is the frequency and y is the time in milliseconds. Try changing x and y to different values. You may also need to change the value in time.sleep(c) so that your beeping is regular. Winsound and time are inside a 'for loop'. The code inside the for loop executes 10 times. Try changing the 10 to a bigger number for more beeps or a smaller number for less beeps.
Q2. Now change your buzzer code so that the green man flashes at the same time.
#add a buzzer and make the green man flash
for seconds in range(10): # do the following code 10 times
light(30,"green",150,-235) # green man on
winsound.Beep(600,250) #beep for a quarter of a second
time.sleep(0.25) #wait for a quarter of a second
light(30,"black",150,-235) # green man off
time.sleep(0.25) #wait for a quarter of a second
Q3. Now, using some of the instructions from above and the same technique, see if you can work out how to make the yellow light flash to stop people crossing. Make sure you use the for loop correctly; there must be a colon at the end of the 'for line', and the lines you want in the loop must all be indented perfectly, just like in the above example! HINT TO DO THIS TASK: replace the 'yellow light on' code in the 'stop people crossing' block with this:
for counter in range(10):
your lines of code here
Remember, the green man must also flash as well, at the same time the yellow light is flashing! Run the final code and check it very carefully to see if the yellow light and green man flashes correctly - it is easy to make a mistake here!
When you have done tha above task, you have completed a model of the Pelican crossing!