Skip to content
BetaDE
Lesson 3 of 5Robotics with fischertechnik

Robotics with fischertechnik · Lesson 3 · 30 minutes

Lamps and buttons

What you'll learn

  • Switch lamps on O1 to O8 on and off and dim them
  • Read a button on an I input
  • Understand the reversed logic of state() (0 = pressed)

The code in this lesson comes from the book “Python for Robots”, Chapters 3.2 and 3.4, and runs unchanged in the simulator. German edition on Amazon ↗

Lights on!

Lamps and LEDs don’t need a direction of rotation. They go on the simple outputs O1 to O8. With setLevel(...) you set the brightness: from 0 (off) to 512 (full brightness).

import ftrobopy
from time import sleep

txt = ftrobopy.ftrobopy("localhost", 65000)

red_lamp = txt.output(1)     # Lamp on O1
green_lamp = txt.output(2)   # Lamp on O2

# Turn lamps on and off individually
red_lamp.setLevel(512)     # full brightness
sleep(1)
red_lamp.setLevel(0)       # off

# Gradually brighten a lamp (dimming)
for brightness in range(0, 513, 64):
    green_lamp.setLevel(brightness)
    sleep(0.1)

You see the lamps in the control panel. The green one gets brighter step by step – that’s called dimming.

Remember: Motors and lamps follow the same pattern as with Pybricks: first create the object for the right port, then control it through that object. This pattern works on almost every robotics platform.

Reading a button

A button goes on an input I1 to I8. You get it with txt.input(...) and read it with state(). This is how the traffic light project in the book does it (chapter 3.4):

button = txt.input(1)   # Button on I1
    # Check the pedestrian request: in ftrobopy, state() == 0 means
    # the button is pressed (contact closed) - not 1!
    request_pending = button.state() == 0

Watch out, reversed logic! In ftrobopy, state() == 0 means: the contact is closed, the button is pressed. state() == 1 means: released. That’s exactly the other way round from what you’d expect. The simulator does it just like the real TXT.

In the control panel you can hold the button down with the mouse, your finger or the space bar. You build the complete traffic light program in lesson 5.

Your task

Your turn

Exercise 3.2 from the book: build a light organ with three lamps on O1, O2 and O3. They light up one after the other: first O1, then O2, then O3 – each for 0.2 seconds (setLevel(512), sleep(0.2), setLevel(0)). Repeat the whole pattern ten times with a for loop.

import ftrobopy
from time import sleep

txt = ftrobopy.ftrobopy("localhost", 65000)

lamps = [txt.output(1), txt.output(2), txt.output(3)]

# Ten times: let each lamp light up briefly

Solve it without hints to earn a ⭐ on your progress page.

Show solution

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)

lamps = [txt.output(1), txt.output(2), txt.output(3)]

for round_number in range(10):
    for lamp in lamps:
        lamp.setLevel(512)
        sleep(0.2)
        lamp.setLevel(0)