LED Control with Button

LED Control with Button

About the Application

This example is intended for illustrative purposes — how one can use the IEC standard in LTPS programming. ST (Structured Text) language allows implementing the simplest logic of a variable operation. To interact with the device, we use the TPS-driver which allows to manage the device variables (analog and GPIO). Let’s create an application that can switch red and green LEDs by the button click on TPS.

What you need

Hardware

Onboard Software

Application

First, we need to create the ST program. Set important properties:

  1. Type - Program;
  2. Task - true;
  3. Device Context - tps (pre-created);
  4. Execution Type - Cycle.

Next Step

Create the local variables. Write this code in the variable area.

The variables source code:

VAR 
    greenLight, redLight : BOOL;
    buttonPressed : BOOL;
    button : BOOL;
END_VAR

After creating the variables, you must select GPIO variables names and their direction.

Final Step

Implementation program logic.

The program source code:

IF button = FALSE THEN
    buttonPressed := TRUE;
ELSIF button AND buttonPressed  THEN
   IF greenLight = FALSE THEN
      greenLight := TRUE;
      redLight := FALSE;
   ELSE
      greenLight := FALSE;
      redLight := TRUE;       
   END_IF       
   buttonPressed := FALSE;  
END_IF

From now on,

you can run the program from the context menu and control LEDs on your Linux TPS.

LED Control with Button