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
| Device | Prefab | Names |
|---|---|---|
| four buttons | StructureLogicButton | “Taste1” to “Taste4” |
| four lights | StructureWallLight | “Lampe1” to “Lampe4” |
| LED display | StructureConsoleLED5 | “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:
- Collect the buttons: all four
Activatevalues go intonewas bits withins. - Find rising edges: pressed is only what is 1 now and was 0 before:
new and (not old). Withxoryou toggle exactly those bits in the state. - Output:
exttakes 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
- Button 1 toggles light 1: press on, press again off.
- The buttons are independent: buttons 2 and 4 only switch their own lights.
- The display shows the state as a number: lights 2 and 4 on gives 2 + 8 = 10.
- Holding a button toggles only once.
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
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
- Milestone 1: button 1 toggles light 1
- Milestone 2: buttons are independent
- Milestone 3: display shows the state
- 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