plcLib (Arduino): Structured Text

Software developed to use the PLC library may incorporate a wide range of standard program statements, such as variable assignments, calculations and user defined functions, plus a variety of program structures, including decisions and loops.

Possibilities for coding are largely limited by your imagination – and knowledge of C / C++ programming of course. There are a few caveats however, so a good understanding of the internal operation of the plcLib software, and its use of variables, is recommended before attempting to develop your own structured text programs.

Lots of information related to Arduino programming is available online, including the and pages.

Using Program Structures

You can use a variety of program structures in your sketches, but care should be taken to avoid halting the PLC scan cycle, as this will in turn halt the processing of any other tasks.

Available program structures include: -

  • if – conditional execution of a code section
  • if ... else – conditional execution of a main section or an alternative, based on a tested logical condition
  • do ... while – repeat a section while a condition is true. (The test is at the end, so this type of loop always runs at least once, even if the test condition is initially false.)
  • while – repeat a section while a condition is true. (The test is at the start, so this type of loop may not run at all if the test is initially false.)
  • for – repeat a section a fixed number of times, with the loop variable progressing through a predetermined series of values.
  • Switch case – execute a single section, chosen from a series of options

As an example, the following sketch first reads digital input X0. It then uses an if statement, based on the state of the digital input, to conditionally read an analogue input, using this result to control the brightness of an LED.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   IF - Conditional control of a PWM output
   LED brightness may be varied using the potentiometer, but only when X0 is pressed.
   Previous PWM output is displayed otherwise. 

   Input - switch connected to input X0 (Arduino pin A0)
   Input - potentiometer connected to input X1 (Arduino pin A1)
   Output - LED connected to output Y0 (Arduino pin 3)

   Software and Documentation:


unsigned int myVar = 0;   // Create a user defined variable and set initial value

void setup() {
  setupPLC();             // Setup inputs and outputs

void loop() {
  myVar = in(X0);         // Read digital Input 0, storing result in user variable

  if (myVar == 1) {       // Vary PWM, if enabled
    inAnalog(X1);         // Read Analogue Input 1
    outPWM(Y0);           // Send to Output 0 as PWM waveform
Source location: File > Examples > plcLib > StructuredText > If

The above sketch allows the brightness of the LED to be varied, but only when the switch is pressed. At other times, the previously set PWM value is used, in effect acting as a 'programmable' light dimmer.

A range of other examples are available from the same directory as the above example, with each one illustrating a different control structure.

The C / C++ programming language used by the Arduino (and hence in the examples) is similar, but not identical, to the Pascal-style programming language described by the appropriate standard (IEC 61131-3).

Related Topics