Average the last N readings
Smooth a noisy level. Average only the readings you have, and drop the oldest.
Work out SmoothedLevel as the average of the last WindowSize Level readings. Store them in a array, a numbered list of values. Add them up with a FOR loop that stays inside the array. While Sample is ON, take one reading every . Until the window is full, divide by ValidCount, not N. Reset wins and sets the result to 0.
Pro missionWarm-up uses only valid readings
Test 1 of 11LevelMeasured level99 %SampleTaking readingsOFFSmoothedLevelSmoothed level43.75 %ValidCountReadings stored4 readings- Level
- Applied
- Time
- About 25 min
- Reward
- +300 XP
- Rig
- 3D process tank
- Siemens editors
- SCL
- Rockwell editors
- Structured Text
- Course stage
- 18. Keep the latest readings
What the tests check
- Works for every N from 1 to 8, while filling up and after wrapping round many times.
- With Sample OFF, the average and count hold still. With Sample held ON, every scan adds a reading.
- Reset empties the window, even with Sample ON. Keep the decimal places in readings.
- Try it Use the buttons below the model and watch the rig respond.
- Test solution Every check runs on your code in one press.
- Replay If one fails, step through it one scan at a time to see why.
Where you’d see this on site
Ripples on the surface make a factory tank's level reading jump about. The operator wants a steadier number on the display.
Averaging the last few readings smooths out the ripples. Counting empty slots gives a false low reading. Keeping old readings too long hides a real change.
At a drinks factory, valves let measured amounts of water and syrup into a mixing tank. An electric mixer blends them into one batch. An outlet valve then sends the drink to a holding tank. Filling machines bottle it from there.
- Ingredient tanksHold the water and syrup
- Mixing tankFill, mix and measure one batchYou are here
- Holding tankKeeps the finished drink ready
- Bottle fillerPuts the drink into bottles
On a real site: places, parts and safety
- Drinks factory — blending water and syrup
- Paint factory — mixing ingredients into a consistent colour
- Water-treatment works — preparing a measured chemical mixture
- Brewery — heating water for washing equipment
A full tank does not close its own inlet. Water keeps arriving until the valve closes, so an open inlet can flood the room.
A heater that is not covered by liquid can overheat. Real tanks have separate safety devices as well as the PLC programme.
The sizes, timings and cutaways are teaching choices, not a drawing of any one real installation.
Next in the course
- Describe one pump with a structureKeep a pump’s data together under one tag name.Applied · 12 min · +150 XP
- Call one block for two pumpsOne block, two copies, each with its own memory.Advanced · 20 min · +300 XP
- Read a 4–20 mA transmitterTurn raw input counts into a level the operator can trust.Applied · 15 min · +250 XP
- Empty until a fifth fullThe level reads 0% while the tank fills. Find out why.Applied · 12 min · +200 XP