plcLib (Arduino): Stack-based Storage and Logic

Version 1.0 of the plcLib software adds the ability to create and use a software-based stack for temporary storage and retrieval of single-bit values. This capability may be combined with block logic commands, to simplify the solution of complex networks based on Boolean algebra – but without the need to create individual user variables for temporary storage, as discussed previously.

A stack is a special area of memory which may be used for temporary data storage and retrieval. Information is stored by being pushed onto the stack and is later retrieved by popping from the stack. The most recently stored information is always the first to be removed, so the stack acts as a last-in, first-out store.

A useful analogy to aid understanding of stack-based data storage and retrieval is a pile of plates, where each plate represents a single piece of information. Storing data is equivalent to adding a new plate to the top of the pile, which causes the 'stack' of plates to grow higher. Conversely, information is retrieved by removing a plate. The most recently added plate will always be at the top, and the oldest at the bottom.

The first step when writing a stack-based sketch is to use the Stack command to create a stack object. (For example, the command Stack stack1; creates a stack called stack1, capable of holding up to 32 single-bit numbers.) Values may be added or removed from the stack by using the push() or pop() methods of the previously created stack object. The following sketch demonstrates the use of the stack to store and subsequently retrieve a series of single bit values.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Push and Pop values from a single-bit software stack

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Input - switch connected to input X1 (Arduino pin A1)
   Input - switch connected to input X2 (Arduino pin A2)
   Input - switch connected to input X3 (Arduino pin A3)
   Output - LED connected to output Y0 (Arduino pin 3)
   Output - LED connected to output Y1 (Arduino pin 5)
   Output - LED connected to output Y2 (Arduino pin 6)
   Output - LED connected to output Y3 (Arduino pin 9)

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

*/

Stack stack1;     // Create a single-bit stack with 32 levels

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

void loop() {

                  // Push 4 values onto the stack
                  // 1) X0, 2) X1, 3) X2, 4) X3

  in(X0);         // Read Input 0
  stack1.push();  // Push X0 onto the stack

  in(X1);         // Read Input 1
  stack1.push();  // Push X1 onto the stack

  in(X2);         // Read Input 2
  stack1.push();  // Push X2 onto the stack

  in(X3);         // Read Input 3
  stack1.push();  // Push X3 onto the stack

                  // Remove 4 values from the stack and
                  // send to outputs in reverse order
                  // 1) X3->Y0, 2) X2->Y1, 3) X1->Y2, 4) X0->Y3
                  // (a stack is a last-in first-out or LIFO store)

  stack1.pop();   // Remove X3 value from the stack
  out(Y0);        // Send to Output 0

  stack1.pop();   // Remove X2 value from the stack
  out(Y1);        // Send to Output 1

  stack1.pop();   // Remove X1 value from the stack
  out(Y2);        // Send to Output 2
  
  stack1.pop();   // Remove X0 value from the stack
  out(Y3);        // Send to Output 3
}
Source location: File > Examples > plcLib > Stack > PushPop

The ability to store temporary calculation results on the stack may be used to simplify the solution of complex logic networks. Options are available to combine parallel or series branches by using block-based logical AND and OR operations, as discussed in the next section.

Block Logic Operations

A logic network consisting of two parallel branches may be solved by first calculating the upper branch, then saving this intermediate result on the stack. The second branch may then be solved and combined with the earlier result, using the orBlock() method of the stack object (which also removes the previous result from the stack).

The following sketch demonstrates the approach.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Logical OR of two parallel switch branches using Block OR instruction

                           X0          X1
                      ----|  |--------|  |----
                      |                      |     Y0
                  -----                      -----(  )-----
                      |    X2          X3    |
                      ----|  |--------|  |----		

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Input - switch connected to input X1 (Arduino pin A1)
   Input - switch connected to input X2 (Arduino pin A2)
   Input - switch connected to input X3 (Arduino pin A3)
   Output - LED connected to output Y0 (Arduino pin 3)


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

*/

Stack stack1;        // Create a single-bit stack with 32 levels

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

void loop() {

                     // Calculate First Branch
  in(X0);            // Read switch connected to Input 0
  andBit(X1);        // Logical AND with Input 1
  stack1.push();     // Push temporary result onto the stack

                     // Calculate second branch
  in(X2);            // Read switch connected to Input 2
  andBit(X3);        // Logical AND with Input 3
  
  stack1.orBlock();  // Merge branches using Block OR
  out(Y0);           // Send result to Output 0

}
Source location: File > Examples > plcLib > Stack > OrBlock

A similar technique may be applied with series connections of switch groups, which may be combined using an AND Block.

The following example first calculates the result of the switch group at the left, which is stored as an intermediate result on the stack. The right hand block is then solved and combined with the earlier result by using the andBlock() method of the stack object.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Logical AND of two series switch groups using Block AND instruction

                     X0                     X2
                ----|  |----           ----|  |----
                |          |           |          |     Y0
            -----          -------------          -----(  )-----
                |    X1    |           |    X3    |
                ----|  |----           ----|  |----		

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Input - switch connected to input X1 (Arduino pin A1)
   Input - switch connected to input X2 (Arduino pin A2)
   Input - switch connected to input X3 (Arduino pin A3)
   Output - LED connected to output Y0 (Arduino pin 3)


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

*/

Stack stack1;        // Create a single-bit stack with 32 levels

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

void loop() {

                     // Calculate First Branch
  in(X0);            // Read switch connected to Input 0
  orBit(X1);         // Logical OR with Input 1
  stack1.push();     // Push temporary result onto the stack

                     // Calculate second branch
  in(X2);            // Read switch connected to Input 2
  orBit(X3);         // Logical OR with Input 3
  
  stack1.andBlock(); // Merge series branches using Block AND
  out(Y0);           // Send result to Output 0

}
Source location: File > Examples > plcLib > Stack > AndBlock

The next section discusses methods of creating custom input/output allocations, should the standard configuration be unsuitable.

Related Topics

top