Help with timing, parallel processes between two BS2's
Blake
Posts: 74
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:
second stamp:
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
Comments
But, my code does work (most of the time). I think it just needs to be refined. I think my question about a better way still has merits... I think mine needs to work in a similar way to the BoeBot.
Another way to do it is to make the high pulse on the timer Stamp much longer, but use code on the listening Stamp to detect the 0-->1 transition instead of just the high level. If it detects just the IN7=1, there is danger that it will execute the code more than once for one trigger, as you have already observed. The transition code requires a couple of bit variables to detect the change:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 3/9/2010 4:29:21 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
Here's my updated code (again thanks everyone, especially Tracy):
Timing Stamp:
Listening/Acting Stamp:
Blake, another way to deal with the negative issue is to nip it in the bud in the counter instruction:
counter = counter - 1 ' subtract 1 from counter
counter = counter MIN 1 - 1 ' subtract 1 from counter, but never go negative.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com