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

Day 279 of 365 · Expert · Month 10: Bits and data

Project: flag register

Four buttons toggle four lights – stored in a single register, with edge detection using bit instructions.

Project 35 min Week 40

What you learn today

  • Many on/off states in one register
  • Toggle bits with a button
  • Show the state

Bit week ends with a project you can use in every base: a flag register. Four buttons toggle four lights – one press on, the next one off. Instead of wasting four registers on four states, everything lives in one.

The setup

DevicePrefabNames
four buttonsStructureLogicButton“Taste1” to “Taste4”
four lightsStructureWallLight“Lampe1” to “Lampe4”
LED displayStructureConsoleLED5“Schalter”

A button reports 1 on Activate while it is pressed.

The idea

Bit 0 belongs to button 1 and light 1, bit 1 to button 2 and light 2 and so on. Three things happen every tick:

  1. Collect the buttons: all four Activate values go into new as bits with ins.
  2. Find rising edges: pressed is only what is 1 now and was 0 before: new and (not old). With xor you toggle exactly those bits in the state.
  3. Output: ext takes every bit out again and switches the matching light, the display gets the whole state as a number.
not tmp old
and edge new tmp
xor state state edge
move old new

Without edge detection a button held down would toggle the light every tick – it would flicker at 2 Hz.

Milestones

  1. Button 1 toggles light 1: press on, press again off.
  2. The buttons are independent: buttons 2 and 4 only switch their own lights.
  3. The display shows the state as a number: lights 2 and 4 on gives 2 + 8 = 10.
  4. Holding a button toggles only once.
Tip

ins new tmp 1 1 sets one bit at position 1 in new and leaves all other bits alone. That is why you read the first button straight into new – that clears the old bits on its own.

Diagram

The register bank of the chip r0 42 r1 31 r2 10 r3 -2.5 r4 0 r5 0 r6 0 r7 0 r8 0 r9 0 r10 0 r11 0 r12 0 r13 0 r14 0 r15 0 sp = r16 ra = r17 16 free registers, one number each sp: stack pointer · ra: return address
Every bit of the register is a switch. The display shows all four together as one number.

Project task

Task

Build the flag register: four buttons “Taste1”–“Taste4” toggle the lights “Lampe1”–“Lampe4” on every press. The state lives in one register, the display “Schalter” shows it as a number. Holding a button toggles only once.

Milestones

  1. Milestone 1: button 1 toggles light 1
  2. Milestone 2: buttons are independent
  3. Milestone 3: display shows the state
  4. Milestone 4: holding toggles only once

Hints

Hint 1

Read button 1 straight into new, buttons 2 to 4 with ins new tmp 1 1, ins new tmp 2 1, ins new tmp 3 1.

Hint 2

Edge: not tmp old, and edge new tmp, xor state state edge, then move old new.

Hint 3

Output: ext tmp state 0 1 for light 1 and so on.

Quiz

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

Complete the day

  • Passed all milestones of the project
  • 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