Algorithms and programming
An algorithm is a list of clear steps that solves a problem. A program is an algorithm written so that a computer can run it.
Good steps are in order, one idea each, and they end. You can draw them as a flowchart: a box for an action, a diamond for a yes/no question, and arrows between them.
There are only three building blocks. Sequence: do steps one after another. Selection: choose with if / else. Repetition: repeat with a loop. Every industrial program is made from these three.
Example algorithm: average of readings. 1) Set sum to 0. 2) For each reading, add it to the sum. 3) Divide the sum by the number of readings. 4) Show the answer.
Data input and output
Input is data going into the program. It can come from a keyboard, a file, or a sensor (temperature, pressure, speed). Output is what comes out: text on a screen, a printed report, a saved file, or a signal that switches a motor or a lamp.
Always keep the unit with a number (23 °C, not just 23). Always check the input: a temperature of 900 °C in a room is surely a sensor fault. Reject or flag it.
Data is stored in variables. A group of similar readings is stored in a list (also called an array): readings = [20, 26, 22, 28, 19].
Mathematical processing
Processing means doing maths on data to get something useful. Common jobs: sum, average (mean), maximum and minimum, unit conversion and scaling.
Average = sum ÷ number of readings. Unit conversion: °F = °C × 9/5 + 32. Scaling: a sensor gives 0 to 5 V for 0 to 100 °C, so temperature = voltage × 20.
Machines count in tiny steps, so answers may carry small rounding errors. Round only at the end, and show a sensible number of digits.
Control programming
A control program keeps a machine in the state we want. It runs a loop: read the sensor, compare with the set value, act on a device, then repeat. This is feedback: the result of the action is read again.
ON/OFF control: if temperature < set value then heater ON, else heater OFF. Simple and cheap, used in irons, geysers and fridges.
Real systems add safety: a maximum limit that always switches the heater OFF, and a small gap (hysteresis) so the relay does not click ON and OFF every second.
Try it: a thermostat on paper
Write the geyser rule on paper: set value 45 °C. Pick a temperature, say 38 °C, 45 °C and 52 °C. For each, write heater ON or OFF. Then use the slider in the 3D and check your three answers.
Key formulas and definitions
- average = sum of readings ÷ number of readings
- °F = °C × 9/5 + 32
- scaled value = reading × (full range ÷ full signal)
- ON/OFF rule: if temp < set then heater ON else OFF
Worked examples
1. Readings are 12, 15, 18 and 15. Find the average.
Sum = 12 + 15 + 18 + 15 = 60. Count = 4. Average = 60 ÷ 4 = 15.
2. A sensor gives 0 to 5 V for 0 to 100 °C. The reading is 2.5 V. Find the temperature.
Each volt is 100 ÷ 5 = 20 °C. Temperature = 2.5 × 20 = 50 °C.
3. Convert 30 °C to °F.
°F = 30 × 9/5 + 32 = 54 + 32 = 86 °F.
4. A furnace has set value 800 °C. It now reads 780 °C. What does the program do? And at 820 °C?
At 780 °C: 780 < 800, so heater ON. At 820 °C: not less than 800, so heater OFF.
5. Write the algorithm to find the highest of 4 readings.
Take the first reading as "max". For each other reading: if it is bigger than max, make it the new max. At the end show max.
Common mistakes
- Writing a number without its unit.
- Dividing by the wrong count when finding the average.
- Using the heater rule the wrong way round (heater ON when it is already too hot).
- Trusting every sensor value without checking for faults.