Lesson 32: Data logger
Keep the readings
- A data logger measures something again and again and keeps the readings, for example the temperature in a greenhouse or a freezer.
- The readings go into an array: one name, many values, each with its own index (0, 1, 2 …).
- Then a loop works out the total, the average, the highest and the lowest.
Words for this lesson
| Word | Meaning |
|---|---|
| data logger | a machine that records readings over time |
| array | many values under one name: temps[0], temps[1] … |
| total | all the values added together |
| average | the total divided by how many values there are |
The standard methods
| Job | Start with | In the loop |
|---|---|---|
| total | total = 0 |
total = total + temps[i] |
| average | (after the loop) | total / 10 |
| highest | highest = temps[0] |
if bigger, keep it |
| lowest | lowest = temps[0] |
if smaller, keep it |
An array in C++
void setup() {
Serial.begin(9600);
int marks[4] = {7, 3, 9, 5};
int total = 0;
for (int i = 0; i < 4; i++) {
total = total + marks[i];
}
Serial.println(total);
}
void loop() {
}
- The loop adds 7 + 3 + 9 + 5 and prints 24. The index goes from 0 to 3: four values.
In the exam
IGCSE Computer Science (0478) Paper 2 calls these the standard methods: totalling, counting, finding the maximum, the minimum and the average.
- 0478/22/O/N/23 Q3(b): an array
Temperatures[]holds values from −20 to 100. Write pseudocode with a single loop to find the lowest value. StartLowestat the first value (or at 100, the biggest possible value), never at 0. - 0478/23/O/N/23 Q10: a weather station stores hourly temperatures in a 2D array; output the maximum, minimum and average for each day and for the week. It is this lesson's code, once for each day.
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
A data logger takes a temperature reading every 200 ms and keeps the last 10 in an array. The sketch already fills the array temps. Add up all 10 values with a for loop (a running total), then print Average: and the total divided by 10.
点击“运行”查看此处输出。
难度 5/5 · 今日首次完成可获 400 金币及少量额外奖励
Now the logger is in a freezer. Find the highest and the lowest temperature. Start both highest and lowest at the first value, temps[0]. Then look at every value: if it is bigger than highest, it becomes the new highest; if it is smaller than lowest, it becomes the new lowest. Print Max: and Min: with the values.
点击“运行”查看此处输出。