Blake
03-09-2010, 09:08 AM
Hey, I am fairly inexperienced at pbasic and electrical engineering in general. I was wondering if anyone had any tips or tricks to using one basic stamp to time operations and another to execute code based on this timing. I looked briefly into using a 555 timer in astable mode, but I was unable to get the pulse period I wanted easily. I have a feeling a propeller chip would be a better option for this, but I already have two bs2's lying around.
In the following code the first stamp keeps time and sends a signal to the second stamp every X seconds. The second stamp activates a motor which spits out a coin from a machine. The machine has an optical interrupt which senses when a coin has left the machine. I put in the "counter" variable so that if the machine is unable to spit out a coin before the next pulse from the first stamp, the second stamp activates the motor until two coins are spit, etc. The "coin" variable is so that the bs2 doesn't count the same coin blocking the interrupt twice (or hundreds of times) because the stamp is cycling through its code numerous times before the coin has had a chance to leave the interrupt.
Every so often, this code fails. I think it must have to do with the timing between the stamps, the second stamp missing a pulse signal or getting the same one twice, etc.
So, is there a better way to do this?
Thanks for any help,
Blake
first stamp:
' {$STAMP BS2}
' {$PBASIC 2.5}
'first stamp, performs timing.
LOW 0 'pin to send signal to second bs2
main:
PAUSE 3505
HIGH 0
PAUSE 5
LOW 0
GOTO main
second stamp:
' {$STAMP BS2}
' {$PBASIC 2.5}
'second stamp, performs actions.
'input (blue line) from opd917BZ
INPUT 0
'input from timer bs2
INPUT 7
counter VAR Byte
coin VAR Byte
main:
counter = 0
LOW 5
GOTO start
start:
DEBUG DEC counter
IF (IN7 = 1) THEN 'if there is a signal from timer
counter = counter +1 'add one to count
GOTO motoron 'turn on motor
ELSEIF (IN0 = 0) THEN 'if coin rolls through interrupt
GOTO evalmotoroff 'decide whether to turn off motor
ELSEIF (IN0 = 1) THEN 'if there is no coin in interrupt
coin = 0 ' set coin present variable to 0
ENDIF
GOTO start
evalmotoroff:
IF (coin = 0) THEN 'if this is the first cycle since coin has been interrupted
coin = 1 'set coin present to 1. this will null future coin present cycles until interrupt is again clear.
counter = counter - 1 'subtract one from coin counter
GOTO turnoff 'begin motor turn off
ENDIF
GOTO start
turnoff:
IF (counter = 0) THEN 'if first cycle
LOW 5 'deactivate motor relay
GOTO start
ENDIF
GOTO start
motoron:
HIGH 5 'trigger motor relay
GOTO start
Post Edited (Blake) : 3/9/2010 2:16:24 AM GMT
In the following code the first stamp keeps time and sends a signal to the second stamp every X seconds. The second stamp activates a motor which spits out a coin from a machine. The machine has an optical interrupt which senses when a coin has left the machine. I put in the "counter" variable so that if the machine is unable to spit out a coin before the next pulse from the first stamp, the second stamp activates the motor until two coins are spit, etc. The "coin" variable is so that the bs2 doesn't count the same coin blocking the interrupt twice (or hundreds of times) because the stamp is cycling through its code numerous times before the coin has had a chance to leave the interrupt.
Every so often, this code fails. I think it must have to do with the timing between the stamps, the second stamp missing a pulse signal or getting the same one twice, etc.
So, is there a better way to do this?
Thanks for any help,
Blake
first stamp:
' {$STAMP BS2}
' {$PBASIC 2.5}
'first stamp, performs timing.
LOW 0 'pin to send signal to second bs2
main:
PAUSE 3505
HIGH 0
PAUSE 5
LOW 0
GOTO main
second stamp:
' {$STAMP BS2}
' {$PBASIC 2.5}
'second stamp, performs actions.
'input (blue line) from opd917BZ
INPUT 0
'input from timer bs2
INPUT 7
counter VAR Byte
coin VAR Byte
main:
counter = 0
LOW 5
GOTO start
start:
DEBUG DEC counter
IF (IN7 = 1) THEN 'if there is a signal from timer
counter = counter +1 'add one to count
GOTO motoron 'turn on motor
ELSEIF (IN0 = 0) THEN 'if coin rolls through interrupt
GOTO evalmotoroff 'decide whether to turn off motor
ELSEIF (IN0 = 1) THEN 'if there is no coin in interrupt
coin = 0 ' set coin present variable to 0
ENDIF
GOTO start
evalmotoroff:
IF (coin = 0) THEN 'if this is the first cycle since coin has been interrupted
coin = 1 'set coin present to 1. this will null future coin present cycles until interrupt is again clear.
counter = counter - 1 'subtract one from coin counter
GOTO turnoff 'begin motor turn off
ENDIF
GOTO start
turnoff:
IF (counter = 0) THEN 'if first cycle
LOW 5 'deactivate motor relay
GOTO start
ENDIF
GOTO start
motoron:
HIGH 5 'trigger motor relay
GOTO start
Post Edited (Blake) : 3/9/2010 2:16:24 AM GMT