Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A freezer must sound an alarm whenever its temperature rises above . A student writes this pseudocode for the microprocessor.
CONSTANT MaxTemp ← -15
INPUT Temperature
IF Temperature < MaxTemp
THEN
OUTPUT "Alarm on"
ENDIFThere are two errors. Find them, explain why each one is an error, and write a corrected version.
A control loop reads the sensor, compares the reading with the stored values, acts, and repeats for as long as the system runs. Feedback means that what the actuator does changes the next sensor reading. With two stored values, the actuator comes on below the low value and goes off above the high value; between them nothing changes.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Error 1. The comparison is the wrong way round. Temperature < MaxTemp is true for a cold reading such as , so the alarm would sound when the freezer is working well. It must be Temperature > MaxTemp.
Error 2. There is no loop. The program reads the sensor once and stops. A control system must keep reading the sensor for as long as it runs.
A corrected version.
CONSTANT MaxTemp ← -15
WHILE TRUE DO
INPUT Temperature
IF Temperature > MaxTemp
THEN
OUTPUT "Alarm on"
ELSE
OUTPUT "Alarm off"
ENDIF
ENDWHILECheck. For , the test is true, so the alarm sounds. For it is false, so the alarm is off. A REPEAT ... UNTIL loop that never ends would also be accepted.
Log in to join the discussion.
Loading…