1

I am currently writing a program that, when a variable reaches a certain point, a connected light will flash on and off every second. I know the light is properly hooked up, and I know that the program to alternate between on and off works, because it did it multiple times a second. I tried adding a wait timer to slow the flashing down.

Here is the chunk of code I am trying to add:

VAR
    delay : TON;
    Count : INT := 0;
END_VAR

delay(IN := TRUE, PT:= T#5S);
    IF NOT (delay.Q) THEN
        RETURN;
    END_IF;

delay(IN := FALSE);

When I add it to my code, I get the error invalid time constant.

I'm not sure if it matters too much, but I am using Schneider Electric's EcoStruxure Machine Expert to write and execute my code.

For those that wish to see the entire program, if it would help, here it is:


IF (change < 70) THEN
    Light13 := FALSE;
END_IF;

IF (change >= 70) AND (change <= 90) THEN
    Light13 := TRUE;
END_IF;

IF (change > 90) THEN
    WHILE change > 90 DO
        IF (index MOD 2 = 0) THEN
            Light13 := TRUE;

        END_IF;

        IF (index MOD 2 <> 0) THEN
            Light13 := FALSE;

        END_IF;

        delay(IN := TRUE, PT:= T#5s);
            IF NOT (delay.Q) THEN
                RETURN;
            END_IF;

        delay(IN := FALSE);

        index := index + 1;

    END_WHILE;

END_IF;

To avoid getting a repeat question to this question, Timers in PLC - Structured Text, I will again reiterate that I am getting an error using this method. Just wanted to clarify beforehand.

I am not at all set on using this way if there is a better option. Thanks for the help!

Jackson148
  • 115
  • 1
  • 2
  • 9

2 Answers2

3

Schneider Electric's EcoStruxure Machine Expert is CoDeSys based. So you have a few options.

  1. Use BLINK in Util library

Open library manager, search for BLINK and double click it. Now you have blink block available. Use it like this.

VAR
   fbBlink: BLINK;
END_VAR

fbBlink(ENABLE := TRUE, TIMELOW := T#1s, TIMEHIGH := T#300ms, OUT => bSignal);

The advantage of this method that you can set a different times for LOW and HIGH states of your lite and use different signals. For instance, short blink once a 2 seconds error 1 and short blink every half second error 2.

  1. Create your own BLINK function as it is suggested by @Filippo.
Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38
  • I am unable to find the BLINK option in library manager; is there somewhere I would need to download it, or am I looking in the wrong spot? Thanks! – Jackson148 Jan 20 '20 at 21:09
1

If you want to flash your light on and off each second you can use this code:

Declaration part:

FUNCTION_BLOCK FB_Flash
VAR_INPUT
    tFlashTime : TIME;
END_VAR
VAR_OUTPUT
    bSignal : BOOL;
END_VAR
VAR
    fbTonDelay : TON;
END_VAR

Implementation part:

fbTonDelay(IN := NOT fbTonDelay.q, PT:= tFlashTime);
IF fbTonDelay.Q
THEN
    bSignal := NOT bSignal;
END_IF

You can call it like this:

fbFlash(tFlashTime := T#1S, bSignal => bFlashLight);

Where bFlashLight is your hardware output. Now if you want the light to flash when a special condition is fullfilled, you can do like this:

IF bSpecialCondition
THEN
    fbFlash(tFlashTime := T#1S, bSignal => bFlashLight);
ELSE
    bFlashLight := FALSE;
END_IF

Try to reach your goals with maximum simplicity and clarity.

Filippo Boido
  • 1,136
  • 7
  • 11
  • 1
    Thank you for your help! I am trying this and everything looks to me like it should be working, but I am running into an error with the declaration part. This popped up when I tried to compile it: ```MAIN(H:3) - error C16389: Unexpected token 'FUNCTION_BLOCK => Syntax Error``` I looked up the error code and it said it was an "empty array init value" which doesn't make sense to me. I understand how your code should work, so I haven't changed any of it. Do you have any idea why this might be causing a problem? Thanks! – Jackson148 Jan 20 '20 at 20:51
  • 1
    FUNCTION_BLOCK FB_Flash ... is the declaration part of a new function block.You have to create a new function block with the code of the declaration and implementation part I gave you.After that, you can declare it for example in MAIN like this fbFlash : FB_Flash; and call it in the implementation part of MAIN as showed above. – Filippo Boido Jan 21 '20 at 07:35