What you learn today
- Show states as text
- Alternating texts
- Combine number and text
A single small display in the corridor should show two things: whether the temperature is fine and exactly how warm it is. Alternating, as text. For that you need everything from this week: STR(), fields with ins, bits with ext and the display's string mode.
The setup
| Device | Prefab | Name |
|---|---|---|
| Gas sensor | StructureGasSensor | “Raum” |
| LED display | StructureConsoleLED5 | “Info” |
Combining number and text
STR() cannot read registers. But every character is an 8-bit field, and digits are ASCII codes: “0” is 48, “1” is 49 and so on. So you take a template and insert the digits at run time:
| Character | T | = | tens | units | C |
|---|---|---|---|---|---|
| Bits | 32–39 | 24–31 | 16–23 | 8–15 | 0–7 |
STR("T=00C") is the template. You work out tens and units with div, floor and mod, add 48 and put them into place with ins.
Alternating texts
A tick counter counts up. Bit 2 of the counter switches between 0 and 1 every four ticks – exactly the rhythm for the alternation. ext tmp tick 2 1 pulls it out without you needing a second counter.
Milestones
- The display is in string mode and shows “OK” at 22 °C.
- Below 18 °C it shows “LOW”, above 26 °C “HIGH”.
- In the number phase it shows “T=22C”, with two digits even at 5 °C (“T=05C”).
- Every four ticks the display switches between state and number.
- Without the sensor it only shows “ERROR”.
The temperature has to be between 0 and 99 before splitting it. clamp temp temp 0 99 makes sure that in heat “T=99C” appears instead of a broken character.
“T=00C” has five characters. More than six do not fit into one number – “T=22.5C” is therefore impossible. Always plan display texts with a character count.
Project task
Task
Build the text display following the milestones above. The defines and aliases are already there.
Milestones
- Milestone 1: string mode and OK
- Milestone 2: LOW and HIGH
- Milestone 3: number as text
- Milestone 4: switch every 4 ticks
- Milestone 5: sensor missing
Hints
Hint 1
Tens: div digit temp 10, floor, add digit digit ASCII0, ins numtext digit 16 8.
Hint 2
Units: mod digit temp 10, plus 48, ins numtext digit 8 8.
Hint 3
Alternation: add tick tick 1, ext tmp tick 2 1, select text tmp numtext text.
Quiz
2 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