plcLib (Arduino): Producing Repeating Waveforms

A repeating pulse may be defined using the duration of the low and high components of the waveform.

The time taken for one complete cycle is called the periodic time (or period), which is obtained by adding the durations of the low and high components (a duration of 1 second in the above example).

Repeating pulses may either be created manually, from simpler components, or by using a dedicated command.

Manual Pulse Creation

A pair of cross-connected on-delay timers may be used to create a repeating pulse, as shown in the following example.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Repeating pulse using a pair of linked on-delay timers

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Output - LED connected to output Y0 (Arduino pin 3)


   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

unsigned long TIMER0 = 0;  // Variable to hold elapsed time for Timer 0
unsigned long TIMER1 = 0;  // Variable to hold elapsed time for Timer 1

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

void loop() {
  in(X0);                  // Read Input 0 (enable)
  andNotBit(Y0);           // And with inverted output
  timerOn(TIMER0, 900);    // 900 ms (0.9 s) delay
  set(Y0);                 // Set Output 0 on time-out

  in(X0);                  // Read Input 0 (enable)
  andBit(Y0);              // And with output
  timerOn(TIMER1, 100);    // 100 ms (0.1 s) delay
  reset(Y0);               // Reset Output 0 on time-out
}
Source location: File > Examples > plcLib > Waveforms > PulsedOutputManual

With the program running, it will be seen that continuously pressing the enable input (X0) causes the output (Y0) to turn on for 0.1 s, then off for 0.9 s in a repeating sequence.

The program makes use of a pair of on-delay timers, plus a set-reset latch based around output Y0. The first timer is initially enabled, assuming output Y0 is zero, and the enable input is also on. The first timer output goes high after 900 ms, which in turn sets output Y0 to 1. At this point, the second on-delay timer is enabled and begins to count. After a further 100 ms, the second timer output becomes set, which causes output Y0 to be cleared, and the whole cycle begins again.

This is an example of a very simple Finite State Machine (FSM), which is the basis of sequential function charts.

Don't worry if the above example seems rather complex, as this same functionality is provided by the timerCycle() command, discussed next.

Using the timerCycle() Command

The timerCycle() command may be used to produce a repeating pulse waveform, which could for example be used to repeatedly flash an LED. (As the command name suggests, this is sometimes called a cycle timer.)

An equivalent function block diagram symbol is shown below.

The command requires four parameters to be specified for each waveform. Parameters 1 and 3 must be specified as variables (of type unsigned long) and are used internally by the plcLib software to measure the elapsed time for the low and high sections of the waveform respectively. Associated low and high pulse widths are defined in the 2nd and 4th parameters. The following example illustrates the process.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Pulsed Output - Creating a repeating pulse using the timerCycle command

   Connections:
   Input - Enable input connected to input X0 (Arduino pin A0)
   Output - Pulse Waveform on LED connected to output Y0 (Arduino pin 3)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

// Variables:
unsigned long AUX0 = 0;                 // Pulse low timer variable
unsigned long AUX1 = 0;                 // Pulse high timer variable


void setup() {
  setupPLC();                           // Define inputs and outputs
}

void loop() {
  in (X0);                              // Read Enable input (1 = enable)
  timerCycle(AUX0, 900, AUX1, 100);     // Repeating pulse, low = 0.9 s, high = 0.1 s
                                        // (hence period = 1 second)
  out(Y0);                              // Send pulse waveform to Output 0
}
Source location: File > Examples > plcLib > Waveforms > PulsedOutput
A typical application is the creation of a repeating 'alarm armed' flashing pulse, as demonstrated in the File > Examples > plcLIB > Applications > AlarmWithArmedStatus example sketch.

Related Topics

top