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

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

Remembering states

Registers forget nothing. With the value from the last tick you detect edges: the one moment in which something changes.

Lesson 12 min Week 12

What you learn today

  • Registers as memory
  • Detect edges
  • Compare with the last value

A sensor tells you how things are now. Often, though, you want to know what has changed. Has the door just opened? Has the pressure risen since the last tick? For that the chip needs a memory, and that memory is its registers.

Registers forget nothing

A device value is read anew every tick. A register keeps its value until you overwrite it. If you copy the current value into a second register at the end of a pass, that register holds the last value during the next tick:

lbn open DOOR HASH("Lagertor") Open Average
# ... compare here ...
move last open        # remember for the next tick

Detecting edges

When a signal jumps from 0 to 1, that is a rising edge. From 1 to 0 it is a falling edge. For values that are only 0 or 1, one comparison is enough:

EventInstruction
rising edgesgt edge new last
falling edgeslt edge new last
any changesne edge new last

The edge is 1 for exactly one tick. If the door stays open for a minute, it still counts only once. That is the difference between a state and an event.

sgt edge open last        # just opened?
add counter counter edge
move last open
Warning

The order decides. If you copy open into last before comparing, both are equal and there is never an edge. Compare first, then remember.

Comparing with the last value

The same principle works with readings. Is the tank pressure rising or falling?

sub change pressure lastPressure   # positive = rising
move lastPressure pressure

That way you spot a leak before the pressure reaches an alarm limit.

Tip

If the sensor fails, NaN ends up in last. Every comparison with NaN is false, so there is no false edge. As soon as the sensor is back, last holds a real number again.

Note

In the game registers keep their value even when you upload new code. So set counters and flags to 0 with move before the start label.

Exercise

Task

Count how often the door “Lagertor” (StructureCompositeDoor) opens and show the number on the LED display “Oeffnungen”.

  • Every opening counts exactly once, no matter how long the door stays open.
  • If the door is removed, the number stays and the chip keeps running without an error.

Hints

Hint 1

sgt edge open last is 1 if the door is open now and was closed in the last tick.

Hint 2

Add the edge to the counter.

Hint 3

Only then move last open.

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