What you learn today
- Bring an oversized script below the limits
- Behaviour unchanged
- Measure before and after
A colleague wrote a script for four rooms: each room has a heater with hysteresis and an alarm light, plus a display with the number of alarms. It is well commented, it works in the simulator – and it cannot be loaded. It has more than 128 lines.
The setup
| Device | Prefab | Names |
|---|---|---|
| Gas sensors | StructureGasSensor | “Raum1” to “Raum4” |
| Wall heaters | StructureWallHeater | “Raum1” to “Raum4” |
| Alarm lights | StructureWallLight | “Raum1” to “Raum4” |
| LED display | StructureConsoleLED5 | “Alarme” |
The rules
- Each room's heater on below 20 °C, off above 24 °C. The chip reads the previous state back from the heater.
- The room's alarm light on below 15 °C, above 35 °C or without a sensor.
- The display shows how many rooms have an alarm.
Measuring before and after
Before you optimise, you measure. Afterwards you measure again. For the starter code and a solution with a name table it looks like this:
| before | after | |
|---|---|---|
| lines | 133 | 45 |
| characters | 3,789 | 1,041 |
| steps per tick | 118 | 78 |
“Steps per tick” counts every line the chip runs through between two yield – comments in the loop included.
The lines shrink to a third, the steps per tick only to two thirds. In the starter code the many comment lines cost steps every tick – they disappear. In return the loop adds label, counter and jump back, four times per tick. Both are below 128, for this plant that is enough. With twelve rooms the loop would need around 220 steps and would have to run in portions as on Thursday.
Milestones
- Each room heats with hysteresis, independently of the others.
- Each room reports cold and heat on its light.
- The display counts the rooms with an alarm.
- A missing sensor switches off its room's heater and reports an alarm.
Work in small steps: first the name table, then the loop with only the heater part, test, then the alarm. Whoever rebuilds everything at once then searches for the bug in 40 lines instead of five.
“Behaviour unchanged” is the most important part of the task. A shorter script that leaves the heater on with a missing sensor is worse than one that is too long.
Diagram
Project task
Task
The starter code has 133 lines and cannot be loaded. Bring it below 128 lines with a name table and a loop. All four rules above must still hold.
Milestones
- Milestone 1: heater per room
- Milestone 2: alarm per room
- Milestone 3: counting alarms
- Milestone 4: sensor missing
Hints
Hint 1
Before start: four poke with HASH("Raum1") to HASH("Raum4").
Hint 2
One room's block becomes the loop body: replace HASH("RaumN") with the register name.
Hint 3
The comments per room go – one comment above the loop is enough.
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