Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Number Changing Detector Function Block

sugur
2022-08-31
2022-08-31
  • sugur - 2022-08-31

    I need a function block which detects any changing of the input (of INT, WORD). I believe that there is already such a function block in the present libraries.

     
  • sgronchi - 2022-08-31

    Perhaps you can find it in OSCAT, not in the standard libraries.
    Anyway...

    FUNCTION_BLOCK InputChangeMonitor_WORD
    VAR_INPUT
        i_Value : WORD;
    END_VAR
    VAR_OUTPUT
        o_Changed : BOOL;
    END_VAR
    VAR
        old_Value : WORD;
    END_VAR
    
    o_Changed := i_Value <> old_Value;
    old_Value := i_Value;
    

    And with threshold

    FUNCTION_BLOCK InputChangeMonitorThresholded_WORD
    VAR_INPUT
        i_Value : WORD;
        i_Threshold : WORD;
    END_VAR
    VAR_OUTPUT
        o_Changed : BOOL;
    END_VAR
    VAR
        old_Value : WORD;
    END_VAR
    
    IF i_Value > old_Value THEN
        o_Changed := i_Value - old_Value > i_Threshold;
    ELSE
        o_Changed := old_Value - i_Value > i_Threshold;
    END_IF
    
    IF o_Changed THEN
        old_Value := i_Value;
    END_IF
    

    Just change WORD to INT everywhere if you need to.

     

    Last edit: sgronchi 2022-08-31
  • sugur - 2022-08-31

    Wow! That was easy. Thank you.

     

Log in to post a comment.