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.
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
PRO (needs Pro) missionFour readings fill the window
Test 1 of 4LevelMeasured level80 %SampleTaking readingsONSmoothedLevelSmoothed level35 %- 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
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.
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.
Average the last four readings: what the tests check
- After every reading, SmoothedLevel is the four slots added up and divided by 4. Keep the decimal places in readings.
- The fifth reading replaces the first, and so on round the four slots, however many readings arrive.
- With Sample OFF, the average holds still. With Sample held ON, every scan adds a reading.
- Try it Use the buttons below the model and watch the rig respond.
- Test solution Every check runs on your code in one press.
- 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.
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.
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 program.
The sizes, timings and cutaways are teaching choices, not a drawing of any one real installation.
Next on the road
- Green figure, on timeRun a crossing's six stages with their timers.Advanced · 30 min · +450 XP
- Down, grip, up, acrossStep a pick and place through its sequence, every move confirmed.Advanced · 35 min · +450 XP
- The cylinder that never arrivesGive every move three seconds, and stop safely if one runs out.Advanced · 35 min · +500 XP
- Emergency stop: air out, then back on purposeDump the air on an emergency stop, and recover without a surprise.Advanced · 40 min · +500 XP