0

I am very new to structured text, so pardon my simple question.

I am using OpenPLC to create this simple program. I have been following the example from the link below to create flowing lights simple program with structured text. In this video, they used 5LEDs and controlled it with case statements. However, my question is, if my program needs to turn on 100 lights, how should I change the code? Should I use for loops? How?

https://www.youtube.com/watch?v=PXnaULHpxC8&t=25s

M. ahmed
  • 53
  • 2
  • 11

2 Answers2

0

Yes you can use for loops etc. to make the program more "dynamic".

Unfortunately most of the PLC's don't give you dynamic access to their digital outputs. This means that at the end you will have to write code that will translate the value from array (which you will be looping through) into digital outputs.

Jakub Szlaur
  • 1,852
  • 10
  • 39
0

There are a few ways to do that. First let me show how you can create chasing light for up to 16.

PROGRAM PLC_PRG
    VAR
        iNumOfLights : INT := 6;
        fbCounter : CTU := ;
        fbTicker : BLINK := (ENABLE := TRUE, TIMELOW := T#100MS, TIMEHIGH := T#1S);
        wOut: WORD;
    END_VAR

    fbTicker();
    fbCounter(CU := fbTicker.OUT, RESET := fbCounter.Q, PV := iNumOfLights);
    wOut := SHL(2#0000_0000_0000_0001, fbCounter.CV);

    A := wOut.0;
    B := wOut.1;
    C := wOut.2;
    D := wOut.3;
    E := wOut.4;
    F := wOut.5;
    G := wOut.6;

END_PROGRAM

Or if you know output address you can do it directly to outputs.

PROGRAM PLC_PRG
    VAR
        iNumOfLights : INT := 6;
        fbCounter : CTU := ;
        fbTicker : BLINK := (ENABLE := TRUE, TIMELOW := T#100MS, TIMEHIGH := T#1S);
        wOut AT %QB0.1: WORD;
    END_VAR

    fbTicker();
    fbCounter(CU := fbTicker.OUT, RESET := fbCounter.Q, PV := iNumOfLights);
    wOut := SHL(2#0000_0000_0000_0001, fbCounter.CV);
END_PROGRAM

You can also change type of chasing lights by something like.

IF fbCounter.CV = 0 THEN
    wOut := 0;
END_IF;
wOut := wOut OR SHL(2#0000_0000_0000_0001, fbCounter.CV);

Now what is behind this. SHl operator will move 1 to the left on set number. For example SHL(2#0000_0000_0000_0001, 3) will result in 2#0000_0000_0000_1000. So we assign it to wOut and then access individual bits by wOut.[n].

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38