What you learn today
- Record values
- Compute statistics
- Show the result
In the lab the temperature over the last minutes should be recorded: how warm was it on average, how cold at its coldest, how hot at its hottest? A chip with a ring buffer and statistics is your data logger.
The setup
| Device | Prefab | Name |
|---|---|---|
| Gas sensor | StructureGasSensor | “Labor” |
| Wall light | StructureWallLight | “Sensor” |
| LED displays | StructureConsoleLED5 | “Anzahl”, “Mittel”, “Minimum”, “Maximum” |
Recording values
Not every tick is stored, only every INTERVAL-th one. A tick counter decides that. The value goes in Celsius to the write position pos, then pos moves on and jumps back to 0 with mod after the last slot – a ring buffer (day 125). count keeps track of how many slots are already filled, at most 8.
Computing statistics
The evaluation runs every tick over the filled slots 0 to count − 1: sum, min, max. As long as the buffer is not full, the values sit in the first slots. Once it is full, all eight are valid – wherever pos happens to be.
Milestones
- The display “Anzahl” counts the readings, at most 8.
- “Mittel” shows the average of the stored values.
- “Minimum” and “Maximum” show the extremes.
- The buffer forgets old values: after eight new readings only these count.
- If the sensor fails, the light “Sensor” is on and nothing is recorded. The statistics of the old values stay.
In the game you use a larger interval, for example define INTERVAL 120 for one reading per minute. Eight slots are then eight minutes of history. With 64 slots that becomes an hourly log – but then take the instruction budget from next week into account.
NaN does not belong in the buffer. A single NaN value turns sum and mean into NaN until it drops out again after eight readings. That is why bnan temp evaluate jumps over the storing.
Diagram
Project task
Task
Build the data logger following the milestones above. Every INTERVAL-th tick one reading in Celsius into the ring buffer (8 slots), then show count, mean, minimum and maximum every tick.
Milestones
- Milestone 1: counting readings
- Milestone 2: mean
- Milestone 3: minimum and maximum
- Milestone 4: ring buffer forgets
- Milestone 5: sensor fails
Hints
Hint 1
Tick: add tick tick 1, blt tick INTERVAL evaluate, then move tick 0.
Hint 2
Store: poke pos temp, add pos pos 1, mod pos pos SLOTS, limit count to 8 with min.
Hint 3
Only evaluate when count is not 0 – otherwise you divide by 0.
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