Comparing a sensor reading with a stored value

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Comparing a sensor reading with a stored value

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does the microprocessor compare each digital sensor reading with?
A A stored (pre-set) value, such as a target temperature
B The reading from the previous day
2. Who normally chooses the stored value, such as the target temperature of 22 °C?
A The sensor works it out by itself
B The designer of the system sets it in advance
3. A heating system has a stored value of 22 and the sensor reading is 18. What is the result of the comparison?
A The reading is too low
B The reading is too high
4. A cooling system has a stored value of 22 and the sensor reading is 27. What does the microprocessor find?
A The reading is too high, so it turns the cooling on
B The reading is on target, so it does nothing
5. The stored value is 22 and the sensor reading is 22. What does the microprocessor do?
A It switches the heater on because the readings match
B Nothing changes because the reading is on target

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1IF reading < 22 THEN heater ON A18 is not greater than 22. The reading is too low, so the comparison should use < to match the heater being turned on.
2IF reading > 22 THEN heater OFF BThe microprocessor sees the reading is above the stored value, so it switches the heater off
3The designer sets the target temperature once. The microprocessor keeps it as a stored value. CThe microprocessor sees the reading is below the stored value, so it switches the heater on
4Worked example: a reading of 18 is compared with a stored value of 22. It is too low, so the heater turns on. DIF 18 < 22 THEN heater ON
5IF 18 > 22 THEN heater ON EtargetTemp <- 22
6IF reading = 22 THEN heater ON FA reading equal to the stored value is on target, so the heater should not be switched on. The heater should come on when the reading is too low (< 22).

Definitions

stored value

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
target ← 22 reading ← 18 IF reading _ target THEN heater_on
2.
target ← 22 reading ← 25 IF reading _ target THEN cooler_on
3.
IF reading _ target THEN OUTPUT "On target"
4.
target ← 22 IF reading < ______ THEN heater ← ON
5.
// Designer sets target to 22 target ← __ IF reading < target THEN heater_on

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A target ← reading
B reading ← 18
C IF reading < target THEN
D OUTPUT "Heater on"
E ENDIF
Question 2
A target ← 22
B reading ← 25
C IF reading > target THEN
D OUTPUT "Heater on"
E ENDIF
Question 3
A target ← 22
B reading ← target
C IF reading = target THEN
D OUTPUT "On target"
E ENDIF
Question 4
A IF reading < target
B OUTPUT "Heater on"
C ELSE
D OUTPUT "Heater off"
E ENDIF
Question 5
A REPEAT
B READ sensor ← reading
C IF reading < target THEN
D OUTPUT "Heater on"
E ENDIF
F UNTIL system off

Answer Key

Comparing a sensor reading with a stored value
Section A: Quick Questions
Q1
A — A stored (pre-set) value, such as a target temperature
Q2
B — The designer of the system sets it in advance
Q3
A — The reading is too low
Q4
A — The reading is too high, so it turns the cooling on
Q5
B — Nothing changes because the reading is on target
Section B: Match the Pairs
Q1
IF reading < 22 THEN heater ON → The microprocessor sees the reading is below the stored value, so it switches the heater on
Q2
IF reading > 22 THEN heater OFF → The microprocessor sees the reading is above the stored value, so it switches the heater off
Q3
The designer sets the target temperature once. The microprocessor keeps it as a stored value. → targetTemp <- 22
Q4
Worked example: a reading of 18 is compared with a stored value of 22. It is too low, so the heater turns on. → IF 18 < 22 THEN heater ON
Q5
IF 18 > 22 THEN heater ON → 18 is not greater than 22. The reading is too low, so the comparison should use < to match the heater being turned on.
Q6
IF reading = 22 THEN heater ON → A reading equal to the stored value is on target, so the heater should not be switched on. The heater should come on when the reading is too low (< 22).
Section C: Definitions
Q1
stored value — A pre-set value, such as a target temperature, kept in the microprocessor. It compares each digital sensor reading with it to decide if the reading is too high, too low or on target.
Section D: Fill in the Blanks
Q1
target ← 22 reading ← 18 IF reading < target THEN heater_on
Q2
target ← 22 reading ← 25 IF reading > target THEN cooler_on
Q3
IF reading = target THEN OUTPUT "On target"
Q4
target ← 22 IF reading < target THEN heater ← ON
Q5
// Designer sets target to 22 target ← 22 IF reading < target THEN heater_on
Section E: Spot the Error
Q1
A — The stored value is pre-set by the designer, so it must be a fixed number such as 22. It must not be copied from the sensor reading. The line should be target ← 22.
Q2
D — The reading 25 is higher than the stored value 22, so the room is too hot. The microprocessor should switch the cooler on, not the heater. The line should be OUTPUT "Cooler on".
Q3
B — The reading must come from the sensor. Copying the stored value into reading means the comparison is always 'on target'. The line should be reading ← READ sensor.
Q4
A — The IF statement is missing the keyword THEN. The line should be IF reading < target THEN.
Q5
B — The assignment arrow is the wrong way round. The value from the sensor must be stored in the variable reading. The line should be reading ← READ sensor.