stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 79

Day 79 of 365 · Beginner · Month 3: First systems

Button as a toggle

A button only reports a short pulse. With an edge and xor it becomes a switch, and a lockout time catches double presses.

Lesson 14 min Week 12

What you learn today

  • One press toggles
  • Edge instead of a steady signal
  • Debouncing

A lever stays where you put it. A button (StructureLogicButton) springs back. Still, you often want a button to switch the light on and, with the next press, off again, like a light switch in a corridor.

What the button reports

When you press the button, Activate jumps to 1. After a little more than half a second the game resets it to 0 by itself. So a press is a short pulse, and the chip sees it for one or two ticks. While the pulse runs, the button accepts no new press.

The naive attempt

lbn button BUTTON HASH("Lichttaster") Activate Average
xor light light button     # toggle on 1

This toggles in every tick in which Activate is 1. If the chip sees the pulse for two ticks, it switches on and straight off again. The light seems to react at random.

Edge instead of a steady signal

Only the rising edge should toggle, that is the one tick in which the button goes from 0 to 1:

sgt edge button last       # just pressed?
move last button
xor light light edge       # toggle on the edge only
sbn LAMP HASH("Flurlicht") On light

xor with 1 flips the state, xor with 0 leaves it alone. That is exactly what a toggle needs.

Debouncing with a lockout

Real push buttons bounce: the contact rattles and delivers several edges in a row. The button in the game does not bounce, but players like to press twice in quick succession. A short lockout after every toggle catches that:

slez ready lock            # lockout over?
and edge edge ready
xor light light edge
sub lock lock 1
select lock edge LOCKOUT lock
max lock lock 0

After a toggle lock holds LOCKOUT and counts down by 1 every tick. Only at 0 does the next edge count again.

Warning

Without an edge the behaviour depends on the tick: sometimes the chip sees the pulse once, sometimes twice. Such bugs easily slip through testing and then show up in the game.

Tip

If the button is missing, Average returns NaN. sgt with NaN is 0, there is no edge, and the light stays as it is. For a light that is fine.

Exercise

Task

The button “Lichttaster” (StructureLogicButton) toggles the wall lights “Flurlicht”: one press on, the next off.

  • Only the rising edge of Activate counts.
  • After every toggle no new edge counts for LOCKOUT ticks.
  • If the button is missing, the light stays as it is – without an error.

Hints

Hint 1

Edge: sgt edge button last, then move last button.

Hint 2

Lockout: slez ready lock and and edge edge ready – the edge only counts without a running lockout.

Hint 3

Toggle with xor light light edge, then count lock down and set it again with select on an edge.

Quiz

4 questions. You pass from 60 % – the server does the grading.

Complete the day

  • Passed the simulator exercise
  • Passed the quiz with at least 60 %

Without an account progress is saved in this browser only.

Report a mistake in this lesson

Please sign in to report. Sign in

Esc
↑ ↓ selectEnter open/ opens search anywhere