'***************************************************************************
'Galil Motion Control Sample DMC Code
'***************************************************************************
'Description:
'This example features a subroutine that is designed to read an input 
'several times in quick succession before determining its state. This may
'help deal with system noise by ensuring that a consistent state is 
'reported. Note that this is a software solution and will only help with
'high frequency noise on a low frequency input. It is generally best to 
'eliminate system noise issues in hardware by utilizing best wiring 
'practices and using optoisolation on digital inputs.
'***************************************************************************
#a
'initialize variables outside of JS because defining
'variables in a subroutine  may cause the variable table
'to shift. See command reference entry for JS for more
'information about the proper use of JS.
checkint= 0
chkini= 0
chkinn= 0
chkint= 0.0000
chkthr= 0
in= 0
out= 0


'check input 2 and print status
in= 2
JS #chkin
MG "input>", in, " status>", out

'check input 3 and print status
in= 3
JS #chkin
MG "input>", in, " status>", out
EN


#chkin
'subroutine designed to read an input several times
'this helps deal with noise on the input by sampling
'several times and ensuring that the state is consistent
'for at least threshold samples
chkini= 0;'  loop counter
chkint= 0;'  input count for input triggered
chkinn= 10;' number of input samples
chkthr= 3;'  threshold for determining state
out= -1;'    flag for it input was determined
#chkinl;' looop to check input chkInn times
chkint= chkint + @IN[in];' if input is active count it
chkini= chkini + 1
JP #chkinl,chkini < chkinn
IF (chkint > (chkinn - chkthr))
  'input will be 1 if the input was 1 for 
  'more than n - threshold samples
  out= 1
ENDIF
IF (chkint < chkthr)
  'input will be 0 if the input was 0 for 
  'less than threshold times
  out= 0
ENDIF
IF (out = -1)
  'if it was not in one state or the other for more than
  'threshold samples (very unusual) we will check the 
  'input again
  JP #chkin
ENDIF
EN;' return from sub