Loading local progress…
PLC Gym
PLC Gym homeLevel 1, 0 XP · your progress
Mission 58 · PRO (needs Pro)

Average the last four readings: rolling average exercise

Smooth a noisy level. Keep four readings, and let each new one replace the oldest.

Work out SmoothedLevel as the average of the last four Level readings. Store them in Readings, a array with four slots, 0 to 3. While Sample is ON, take one reading every . Put it in the slot WriteIndex points to, then move WriteIndex on one. After slot 3, wrap WriteIndex back to 0, so the newest reading replaces the oldest. Add up the four slots with a FOR loop and divide by 4.

This mission is part of ProCreate a free account: every account gets a free Pro trial until 5 December.

No card. Nothing to pay. Nothing to cancel.

Official release: 5 December. Top the leaderboard on the day and win 3 months of Pro, plus 2 weeks of Pro to give a friend. 2nd place wins 4 weeks. 3rd place wins 2 weeks. Rules

  • Every Coaching lesson, from zero
  • Every mission and lesson, as you level up
  • A practical test for every title, with a certificate
  • Rewind any test, one scan at a time
  • Worked solutions when you're stuck
  • The code converter, as much as you like
The 3D process tank this mission runs onPRO (needs Pro) mission
How the test runs

Four readings fill the window

Test 1 of 4
Set by the testChecked by the testStep 6 of 6 · 120 ms
Level
Applied
Time
About 15 min
Reward
+350 XP
Rig
3D process tank
Siemens editors
SCL
Rockwell editors
Structured Text
Beckhoff editors
Structured Text
Stage
18. Keep the latest readings
Required behaviour

Average the last four readings: what your code must do

A noisy level reading jumps about. Smoothing it stops later logic reacting to every flicker. Trends shows Level and SmoothedLevel side by side, scan by scan. This is the first of two missions on the same filter. Here the window, the number of readings you average, is fixed at four. All four slots start at 0.0, so the first three results read low. The next mission fixes that, and lets the window change size. Your filter only works out a number for later logic. It does not switch the pump or valve.

Sample
Sample ON takes one Level reading every scan. Sample OFF keeps SmoothedLevel as it is.
Store and wrap round
Each reading goes into the next of the four slots. After the fourth, go back to the first, so the newest reading replaces the oldest. How you lay out the buffer is up to you.
Average
After each reading, SmoothedLevel is the four slots added up and divided by 4. Keep the decimal places.
The first three readings
All four slots start at 0.0, and so does SmoothedLevel. An empty slot still counts as 0. So with only 10% and 30% stored, the result is 10%: 40 divided by 4.
Tag table

Average the last four readings: inputs and outputs

Your program reads 2 inputs and sets 1 output. The names are the same in every maker’s editor.

TagData typeYour codeWhat it means on the machine
LevelREALReadsAn input your code needsTank level, %
SampleBOOLReadsAn input your code needsWhile ON, take one Level reading on every scan.
SmoothedLevelREALWritesAn output your code setsAverage of the last four readings, in %.

In Siemens SCL, write a block’s own tags with # in front, as TIA Portal does: #Level. Rockwell and TwinCAT use the name on its own: Level.

Automatic tests

Average the last four readings: what the tests check

  1. After every reading, SmoothedLevel is the four slots added up and divided by 4. Keep the decimal places in readings.
  2. The fifth reading replaces the first, and so on round the four slots, however many readings arrive.
  3. With Sample OFF, the average holds still. With Sample held ON, every scan adds a reading.
  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. Rewind PRO (needs Pro) If one fails, step back through it one scan at a time to see why.

Stuck on this mission? Ask the members in the Locker Room.

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 four readings smooths out the ripples. A reading left in the wrong slot, or never replaced, gives a false number that 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 program.

The sizes, timings and cutaways are teaching choices, not a drawing of any one real installation.

Keep going

Next on the road