Lesson 5 of 5Robotics with fischertechnik
Robotics with fischertechnik · Lesson 5 · 45 minutes
Project: Traffic light control
What you'll learn
- Program a traffic light as a sequence of states
- Keep the code readable with small helper functions
- React to a pedestrian request with a button
The code in this lesson comes from the book “Python for Robots”, Chapter 3.4, and runs unchanged in the simulator. German edition on Amazon ↗
A traffic light is a state machine
At any moment a traffic light is in exactly one state: red, red-yellow, green or yellow. Fixed rules say when it moves on to the next state. That’s exactly how you build it in code.
Build: three lamps (red, yellow, green) on O1 to O3 and a button on I1 for pedestrians.
The complete code
This is the project from the book. Press Run and watch the control panel. Hold down the button on I1 during the red phase – then the light stays green for only 3 instead of 6 seconds afterwards.
# ============================================================
# PROJECT: Traffic Light Control
# Runs through the classic German traffic-light cycle and reacts
# to a pedestrian request via a button.
# ============================================================
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
red_lamp = txt.output(1) # Lamp on O1
yellow_lamp = txt.output(2) # Lamp on O2
green_lamp = txt.output(3) # Lamp on O3
button = txt.input(1) # Button on I1
def all_off():
# Helper function: turns all three lamps off (see Functions, Part 1)
red_lamp.setLevel(0)
yellow_lamp.setLevel(0)
green_lamp.setLevel(0)
def show_red():
all_off()
red_lamp.setLevel(512)
def show_red_yellow():
all_off()
red_lamp.setLevel(512)
yellow_lamp.setLevel(512)
def show_green():
all_off()
green_lamp.setLevel(512)
def show_yellow():
all_off()
yellow_lamp.setLevel(512)
print("Traffic light started.")
while True:
# Check the pedestrian request: in ftrobopy, state() == 0 means
# the button is pressed (contact closed) - not 1!
request_pending = button.state() == 0
show_red()
sleep(2)
show_red_yellow()
sleep(1)
show_green()
if request_pending:
# If requested, the light stays green for a shorter time
sleep(3)
else:
sleep(6)
show_yellow()
sleep(1)The four small functions show_red(), show_red_yellow(), show_green() and show_yellow() make the main part much easier to read. Every function does exactly one job – and each one calls all_off() first. That way red and green can never shine at the same time by accident.
Once more: reversed logic.
button.state() == 0means “pressed”. That’s why the code saysrequest_pending = button.state() == 0. The button is read once per round – right at the start, while red is coming up.
More projects in the book
In the book “Python for Robots” (part 3) there are bigger projects still to come: a line follower with two sensors, a sorting machine, a parking barrier and a conveyor system with two belts. You now know everything you need for them – motors, lamps, buttons and sensors.
Your task
Your turn
Exercise 3.4 from the book (simplified): write the function night_mode(times). At night a traffic light only blinks yellow: first switch all lamps off. Then yellow goes on for one second and off for one second, times times. Red and green stay off the whole time.
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
red_lamp = txt.output(1) # Lamp on O1
yellow_lamp = txt.output(2) # Lamp on O2
green_lamp = txt.output(3) # Lamp on O3
def all_off():
red_lamp.setLevel(0)
yellow_lamp.setLevel(0)
green_lamp.setLevel(0)
def night_mode(times):
pass
night_mode(3)- Something to think about
In a
forloop:yellow_lamp.setLevel(512),sleep(1),yellow_lamp.setLevel(0),sleep(1). - Which command helps?
Use the parameter times in
range(times)– no fixed number.Call
all_off()at the start ofnight_mode(). - Gap code – fill in the ___
import ftrobopy from time import sleep txt = ftrobopy.ftrobopy("localhost", 65000) red_lamp = txt.output(1) # Lamp on O1 yellow_lamp = txt.output(2) # Lamp on O2 green_lamp = txt.output(3) # Lamp on O3 def all_off(): red_lamp.setLevel(0) yellow_lamp.setLevel(0) green_lamp.setLevel(0) def night_mode(times): all_off() for _ in ___: yellow_lamp.setLevel(___) sleep(___) yellow_lamp.setLevel(0) sleep(___) night_mode(3)
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
red_lamp = txt.output(1) # Lamp on O1
yellow_lamp = txt.output(2) # Lamp on O2
green_lamp = txt.output(3) # Lamp on O3
def all_off():
red_lamp.setLevel(0)
yellow_lamp.setLevel(0)
green_lamp.setLevel(0)
def night_mode(times):
all_off()
for _ in range(times):
yellow_lamp.setLevel(512)
sleep(1)
yellow_lamp.setLevel(0)
sleep(1)
night_mode(3)