Loading local progress…
Practice missions
Practise in
Level 1, 0 XP · your progress
Mission 32 · Pro rolling average exercise

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.

This mission is part of ProTry the full course, every mission and the expert challenges free for 7 days. No card needed.
The 3D process tank this mission runs on Pro mission
Test replay

Warm-up uses only valid readings

Test 1 of 11
Set by the testChecked by the testStep 18 of 18 · 360 ms
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
Automatic tests

What the tests check

  1. Works for every N from 1 to 8, while filling up and after wrapping round many times.
  2. With Sample OFF, the average and count hold still. With Sample held ON, every scan adds a reading.
  3. Reset empties the window, even with Sample ON. Keep the decimal places in readings.
  1. Try it Use the buttons below the model and watch the rig respond.
  2. Test solution Every check runs on your code in one press.
  3. Replay If one fails, step through it one scan at a time to see why.
Why it matters

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.

Where you'd find thisMaking one batch of drink

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.

  1. Ingredient tanksHold the water and syrup
  2. Mixing tankFill, mix and measure one batchYou are here
  3. Holding tankKeeps the finished drink ready
  4. 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.

Keep going

Next in the course