📖 Scenario: You work in a factory where machines have sensors measuring temperature and pressure. You want to create a simple trend chart that records these values over time to see how they change.
🎯 Goal: Build a program that stores temperature and pressure readings in arrays, updates them over time, and then outputs the recorded trend data.
📋 What You'll Learn
Create arrays to hold temperature and pressure values
Set a maximum number of readings to store
Write logic to add new readings to the arrays
Output the stored trend data
💡 Why This Matters
🌍 Real World
Factories and plants monitor machine conditions by recording sensor data over time to detect problems early.
💼 Career
PLC programmers often write code to log and display process variables for operators and maintenance teams.
Progress0 / 4 steps
1
Create arrays for temperature and pressure readings
Create two arrays called TemperatureReadings and PressureReadings each with 10 elements of type REAL initialized to 0.0.
PLC Programming
Hint
Use ARRAY[1..10] OF REAL to create arrays with 10 elements.
2
Add a variable for the current index
Create a variable called CurrentIndex of type INT and initialize it to 1. This will track where to store the next reading.
PLC Programming
Hint
Use CurrentIndex : INT := 1; inside the VAR block.
3
Add logic to store new readings and update the index
Write code to store new temperature and pressure readings into TemperatureReadings[CurrentIndex] and PressureReadings[CurrentIndex]. Then increase CurrentIndex by 1. If CurrentIndex becomes greater than 10, reset it to 1.
PLC Programming
Hint
Assign new readings to arrays at CurrentIndex, then increment and reset CurrentIndex if needed.
4
Output the stored trend data
Write code to output the TemperatureReadings and PressureReadings arrays. Use a loop with variable i from 1 to 10 to print each reading in the format: "Reading i: Temp = X, Pressure = Y".
PLC Programming
Hint
Use a FOR loop from 1 to 10 and print each reading with the index and values.
Practice
(1/5)
1. What is the main purpose of a trend chart in PLC programming for process variables?
easy
A. To show how process variables change over time
B. To control the speed of the PLC processor
C. To reset the PLC memory automatically
D. To display error messages only
Solution
Step 1: Understand the role of trend charts
Trend charts are used to visualize data changes over time, especially for process variables like temperature or pressure.
Step 2: Eliminate unrelated options
Options about controlling processor speed, resetting memory, or showing errors do not relate to trend charts.
Final Answer:
To show how process variables change over time -> Option A
Quick Check:
Trend charts = show variable changes over time [OK]
Hint: Trend charts visualize data changes over time [OK]
Common Mistakes:
Confusing trend charts with control functions
Thinking trend charts reset PLC memory
Assuming trend charts only show errors
2. Which of the following is the correct way to declare an array to store 10 samples of a process variable in a PLC program?
easy
A. ARRAY samples[10] REAL;
B. samples = [10] REAL;
C. VAR samples: REAL[10]; END_VAR
D. VAR samples: ARRAY[1..10] OF REAL; END_VAR
Solution
Step 1: Recognize PLC array declaration syntax
In many PLC languages, arrays are declared with VAR, specifying index range and type, e.g., ARRAY[1..10] OF REAL.
Step 2: Check each option
VAR samples: ARRAY[1..10] OF REAL; END_VAR matches correct syntax. Options A, B, and D use incorrect or invalid syntax for PLC arrays.
Final Answer:
VAR samples: ARRAY[1..10] OF REAL; END_VAR -> Option D
Quick Check:
Correct PLC array syntax = VAR samples: ARRAY[1..10] OF REAL; END_VAR [OK]
Hint: Use VAR and ARRAY with index range for PLC arrays [OK]
Common Mistakes:
Using incorrect brackets or missing VAR keyword
Confusing array syntax with other languages
Not specifying index range properly
3. Given the following PLC code snippet that samples a temperature every cycle and stores it in an array of size 5, what will be the content of the array after 7 cycles?
VAR samples: ARRAY[1..5] OF REAL; index: INT := 1; temp: REAL; END_VAR
// On each cycle:
temp := ReadTemperature();
samples[index] := temp;
index := index + 1;
IF index > 5 THEN
index := 1;
END_IF
medium
A. The array is empty because index resets too early
B. The array contains the first 5 temperature samples from cycles 1 to 5
C. The array contains the last 5 temperature samples from cycles 3 to 7
D. The array contains only the temperature from cycle 7 repeated
Solution
Step 1: Understand the circular buffer logic
The index increments each cycle and resets to 1 after 5, so the array stores the last 5 samples in a loop.
Step 2: Analyze after 7 cycles
After 7 cycles, index has wrapped around once. The array holds samples from cycles 3 to 7, overwriting the first two samples.
Final Answer:
The array contains the last 5 temperature samples from cycles 3 to 7 -> Option C
Quick Check:
Circular buffer stores last 5 samples = The array contains the last 5 temperature samples from cycles 3 to 7 [OK]
Hint: Index resets after max size to overwrite oldest samples [OK]
Common Mistakes:
Thinking array holds first 5 samples only
Assuming array is empty due to index reset
Believing array repeats last sample only
4. Identify the error in this PLC code snippet intended to sample a pressure value every 10 cycles and store it in an array:
VAR samples: ARRAY[1..5] OF REAL; counter: INT := 0; index: INT := 1; pressure: REAL; END_VAR
counter := counter + 1;
IF counter = 10 THEN
samples[index] := ReadPressure();
index := index + 1;
counter := 0;
IF index > 5 THEN
index := 1;
END_IF
END_IF
medium
A. The counter increments every cycle but sampling happens only when counter equals 10
B. The index reset condition should be inside the first IF block
C. The counter should reset after index increments, not before
D. The index reset condition is outside the sampling IF block, causing incorrect indexing
Solution
Step 1: Review index reset placement
The index reset IF was outside the sampling IF, causing it to run every cycle and reset index prematurely.
Step 2: Correct placement
Moving the index reset condition inside the sampling IF block ensures index resets only after a new sample is stored, preventing incorrect indexing.
Final Answer:
The index reset condition should be inside the first IF block -> Option B
Quick Check:
Index reset must be inside sampling block [OK]
Hint: Keep index reset inside sampling condition to avoid errors [OK]
Common Mistakes:
Placing index reset outside sampling condition
Resetting counter at wrong time
Misunderstanding counter increment logic
5. You want to create a trend chart that samples a flow rate every 5 seconds and stores the last 12 samples. Which approach correctly implements this in a PLC program?
hard
A. Use a timer that triggers every 5 seconds to read flow rate, store it in a 12-element array, and use a circular index to overwrite oldest samples.
B. Sample flow rate every cycle without timing, store in a 12-element array, reset index after 12 cycles.
C. Use a timer to sample every 12 seconds, store in a 5-element array, and reset index after 5 samples.
D. Sample flow rate only once at start, store in a 12-element array, then never update.
Solution
Step 1: Match sampling frequency and storage size
The requirement is to sample every 5 seconds and keep last 12 samples, so timer interval and array size must match.
Step 2: Use circular buffer logic
Using a timer to trigger sampling every 5 seconds and a circular index to overwrite oldest samples in a 12-element array fits the need.
Final Answer:
Use a timer that triggers every 5 seconds to read flow rate, store it in a 12-element array, and use a circular index to overwrite oldest samples. -> Option A