R_Trig using or better

Johan10
2018-04-11
2018-06-04
  • Johan10 - 2018-04-11

    Hello everybody, I try to make an HMI on Codesys in language ST, whitch controls engines. And for example I have a button  « Left » witch is linked to a variable « ButtonLeft »whitch allows to turn left. When I rest above, the button stays in 1 and the engine continues to turn even when I release the button. That's why I shall like putting it on a rising edge. I saw the function R_Trig but I do not know how to use it. Anybody has an example or can help me? I have made something like this. That is correct? Does my variable « ButtonLeft » will return to 0 when I’ll stop push the button. Thank’s a lot.

    IMG: 1111DD5B

     
  • Lo5tNet - 2018-04-11

    It sounds like you want it to only turn left as long as you have the button held down. R_TRIG is not necessary in the situation. All you need to do is tie the input (Button) to the turn left output. If your input (Button) is staying true when you release it then it is not setup correctly. How do you have the <g> button connected to the variable <buttonleft>?</buttonleft></g>

     
  • Johan10 - 2018-04-11

    Comingback4u hat geschrieben:
    It sounds like you want it to only turn left as long as you have the button held down. R_TRIG is not necessary in the situation. All you need to do is tie the input (Button) to the turn left output. If your input (Button) is staying true when you release it then it is not setup correctly. How do you have the <g> button connected to the variable <buttonleft>?</buttonleft></g>

    Before, it is what I made, I put
    If (ButtonLeft == 1) Then
    pin1: = 0;
    pin2: = 1;
    pin3: = 1;
    End_If
    but the problem is that, when I pressed on the button and when it passed in 1. It stays in 1, of the blow the engine continues to turn on its left.

    Excuse-me it was a mistake, the variable ButtonLeft is linked to my Button « Left »

     
  • Lo5tNet - 2018-04-11

    It looks like you just need to add an else statement so it would look like:

    IF ButtonLeft = 1 THEN
        //Put logic in here to turn on the output if ButtonLeft is active
        pin1:= 0;
        pin2:= 1;
        pin3:= 1;
    ELSE
        //Put logic in here to turn off the output if ButtonLeft is not active
        pin1:= 0;
        pin2:= 0;
        pin3:= 0;
    END_IF
    

    Without the else statement your outputs will never turn off.

    A cleaner approach without using an IF/ELSE statement would be:

    pin1:= 0;
    pin2:= ButtonLeft = 1;
    pin3:= ButtonLeft = 1;
    

    This would turn on pin2/3 if ButtonLeft is active if all data types are type BOOL.

     
  • Johan10 - 2018-04-12

    Comingback4u hat geschrieben:
    It looks like you just need to add an else statement so it would look like:

    IF ButtonLeft = 1 THEN
        //Put logic in here to turn on the output if ButtonLeft is active
        pin1:= 0;
        pin2:= 1;
        pin3:= 1;
    ELSE
        //Put logic in here to turn off the output if ButtonLeft is not active
        pin1:= 0;
        pin2:= 0;
        pin3:= 0;
    END_IF
    

    No I really need a rising Edge because,
    Pin1:=0;
    Pin2:=0;
    Pin3:=0;
    Is a combination to reverse.
    Do you know how to use a rising edge please? There is a library to add?
    Without the else statement your outputs will never turn off.
    A cleaner approach without using an IF/ELSE statement would be:

    pin1:= 0;
    pin2:= ButtonLeft = 1;
    pin3:= ButtonLeft = 1;
    

    This would turn on pin2/3 if ButtonLeft is active if all data types are type BOOL.

     
  • Lo5tNet - 2018-04-12

    I guess I have no idea what you are asking then. R_TRIG is in the standard library and by your example you already know how to use it. Just remember that the output Q is only TRUE for one cycle. After that one cycle it gets reset back to false so your pins will stay on even without the button being pressed.

    VAR
        fb_LeftButtonRTrig: R_TRIG;
        xLeftButton: BOOL;
    END_VAR
    fb_LeftButtonRTrig(CLK:= xLeftButton);
    IF fb_LeftButtonRTrig.Q THEN  //Set pins to true if left button is pressed
        pin1:= 0;
        pin2:= 1;
        pin3:= 1;
    END_IF
    
     
  • Johan10 - 2018-04-12

    Comingback4u hat geschrieben:
    I guess I have no idea what you are asking then. R_TRIG is in the standard library and by your example you already know how to use it. Just remember that the output Q is only TRUE for one cycle. After that one cycle it gets reset back to false so your pins will stay on even without the button being pressed.

    VAR
        fb_LeftButtonRTrig: R_TRIG;
        xLeftButton: BOOL;
    END_VAR
    fb_LeftButtonRTrig(CLK:= xLeftButton);
    IF fb_LeftButtonRTrig.Q THEN  //Set pins to true if left button is pressed
        pin1:= 0;
        pin2:= 1;
        pin3:= 1;
    END_IF
    

    Ok. Yes I just wanted to Enter my « If » only when I pressed my button. So In my code, R_Trig doesn’t appaer in color. That’s normal? There is a library to add? Because the program is not running. Thank’s a lot for your precious help

     
  • Lo5tNet - 2018-04-12

    Best I can do I guess is give you an example project. If you run this example and simulation you will see how when you press the left button it will turn pin2/3 on.

    IMG: RTrigExampleVisu.PNG

    RTrigExample.project [120.1 KiB]

     
  • MaraP - 2018-06-04

    Johan10 hat geschrieben:
    Hello everybody, I try to make an HMI on Codesys in language ST, whitch controls engines. And for example I have a button  « Left » witch is linked to a variable « ButtonLeft »whitch allows to turn left. When I rest above, the button stays in 1 and the engine continues to turn even when I release the button. That's why I shall like putting it on a rising edge. I saw the function R_Trig but I do not know how to use it. Anybody has an example or can help me? I have made something like this. That is correct? Does my variable « ButtonLeft » will return to 0 when I’ll stop push the button. Thank’s a lot.

    You don't need any code to fix your problem i think. I believe your button configuration is just done wrongly. Im quite sure that under input configuration you had put your variable to field "Toggle". Instead of this you should put it to fied "TAP".

    See picture below...

    IMG: Bild

     

Log in to post a comment.