Has an input arrived anytime during the a time period?

To troubleshoot a problem, I wish to remotely determine and indicate whether an object moves more than a small distance left to right or visa versa.
Using optical sensors for example, I can easily achieve digital outputs (similar to a shaft encoder) that will give high outputs in one order or the other but it is recognised that differing speeds of movement will result in a variable pause between the signals.

The first part of an imagined code would determine which of two inputs (eg: left or right) has been first triggered and would then need to wait for the second input to be triggered during a specified period of time - lets say 2 seconds - to determine the direction of movement or, if this does not appear because the movement is too small, to return to the waiting state.

If it was certain that the second input signal would arrive before the first signal had returned to zero I would quite easily manage but this cannot be guaranteed and I have no way of being able to ensure this condition can be met by changing the physical environment or the efficacy of the sensors.

The required program enquiry is therefore;

After a high signal is detected at input 1, is there a corresponding high signal at input 2 within two seconds of the high signal at input 1 and regardless of whether the signal at input 1 has remained high?'

If the onset of a movement from the first sensor is detected and on the assumption that the input received from the second sensor would last for a minimum 0.1 of a second, I can see that it is possible to repeat twenty times a do...loop with a pause of 0.1 second asking if the input 2 has gone high. This would certainly achieve my needs but it seems rather clumsy.

I presume there must be a much tidier way of asking 'Has the input pin 2 gone high anytime during the previous two seconds?' and I seek advice on how the far more competent experts than myself might achieve this.

Thank you.
 

lbenson

Senior Member
I can see that it is possible to repeat twenty times a do...loop with a pause of 0.1 second asking if the input 2 has gone high.
I think your technique is the one I would use--nothing clumsy about it:
Code:
for b1 = 0 to 20
  if pinC.2 = 1 then: exit: endif
  pause 100 ' or less by observation to take into account loop overhead
next b1
if b1 < 20 then ' we exited early, so have met the condition
It's possible you might want to double the number of loops and change to PAUSE 50.
 

inglewoodpete

Senior Member
Without fully understanding the nature of the input device, I would suggest that the input be tested/checked at least twice per cycle to ensure that contact bounce or input stutter (consequently miscounting) is minimised. Therefore, I suggest that 40 x 50mS or 50 x 40mS loops and ensuring that two consecutive positive readings occur before acting.
 
Many thanks to both of you. I am a little surprised that there is not an alternative but I can work with that. And yes, I do need to refine the details to ensure reliability which invariably follows getting the basics up and running.
 
Top