Hi,
I'm trying to get this to count pulses from an opto detector which goes High when the beam is broken.
Problems are,it stops the Hmtr at three pulses instead of four and if the beam isn't broken in quick succession,hmtr continues to run no matter how many times beam is broken.
My complete thanks to Melanie for the first (most so far) part of code!



DEFINE OSC 4
CMCON=7

'Declare Variables,Constants and Aliases for Ports


In1 VAR PORTA.1 'pulse input from selector 24vac/dc via optocoupler
In2 VAR PORTA.2 'pulse input from selector 24vac/dc via optocoupler
In3 VAR PORTA.3 'pulse input from selector 24vac/dc via optocoupler
Button1 VAR PORTA.4 'pulse input from selector 24vac/dc via optocoupler
Hmtr VAR PORTB.0 ' motor on/off
DATAPLS VAR PORTA.0 'sense output from passthru opto
Alert VAR PORTB.1 'Beeper
CounterA VAR BYTE 'counter
i VAR WORD 'General purpose variable


'Set initial state of ports

Hmtr=0 ' motor off
TRISA = %11111111 'Make all Porta pins Inputs
TRISB = %00000000 'Make all Portb Outputs
Pause 500 'Settling time


'Start of program

runloop:
While In1=1 'Wait here for input pulse
CounterA=0 'Rest time counter
Wend



Timerloop:

While Button1=1 'Wait for dispense button to be pressed
CounterA=CounterA+1 'Increment counter by 100ms
IF CounterA=200 Then Startloop 'If 20 sec elapses,goto start and await new activation

Pause 100 'Wait 100ms for smooth operation
Wend



runit:

High Hmtr
Pause 100
While DATAPLS=0 'wait for High from passthru opto
Wend
GoSub cnt

GoTo runloop


cnt:
PulsIn DATAPLS, 1, i
IF i > 4 Then
Low Hmtr
Else
GoTo runit
EndIF
Return


Any suggestions ?

Thank you