BS1 Controlling Relays
RichSki
Posts: 3
in BASIC Stamp
Good Morning,
I am trying to write code for an old BS1 that will control 2 relays. The First relay (on Port 0) is controlled by a N/O Push Button (on Port 6). Press button once and the relay turns on. Press button again it turns off relay 1. The Push Button will also start relay 2 (on Port 1) to TOGGLE On and Off at a cycle rate determined by a 10K Pot (on Port 5). The second button press will stop the TOGGLE Cycle.
The problem I am having is that when I load the program all of the relays are off. Relay 2 will start to cycle even though I have not pressed the On/Off switch. If I adjust the Pot, the TOGGLE rate will change. I have to press and hold the On/Off button until the second relay finishes its cycle. Both relays turn on and stay on. The second button press turns off both relays. Relay 2 starts to cycle again on its own.
I was expecting relay 1 and 2 to be off and wait for the first button press. But for some reason relay 2 starts to toggle. Is there something wrong with the ONOff: subroutine?
Here is the code listing.
SYMBOL Btn1 = b0
SYMBOL PVal = b1
SYMBOL Delay = w3
Start:
PINS = %0000000
GOSUB CheckPot
OnOff:
BUTTON 6,0,255,0,Btn1,0,OnOff
TOGGLE 0
TOGGLE 1
GOSUB CheckPot
MainLoop:
PAUSE Delay
TOGGLE 1
GOSUB CheckPot
BUTTON 6,0,255,0,Btn1,0,MainLoop
END
CheckPot:
POT 5,20,PVal
IF PVal = 0 THEN Five
IF PVal > 0 and PVal <51 Then Ten
IF PVal >= 51 and PVal < 102 Then Fifteen
IF PVal >= 102 and PVal < 153 Then Twenty
IF PVal >= 153 and PVal < 204Then TwentyFive
IF PVal > 204 Then Thirty
Five:
Delay = 5000
Return
Ten:
Delay = 10000
Return
Fifteen:
Delay = 15000
Return
Twenty:
Delay = 20000
Return
TwentyFive:
Delay = 25000
Return
Thirty:
Delay = 30000
Return
I am trying to write code for an old BS1 that will control 2 relays. The First relay (on Port 0) is controlled by a N/O Push Button (on Port 6). Press button once and the relay turns on. Press button again it turns off relay 1. The Push Button will also start relay 2 (on Port 1) to TOGGLE On and Off at a cycle rate determined by a 10K Pot (on Port 5). The second button press will stop the TOGGLE Cycle.
The problem I am having is that when I load the program all of the relays are off. Relay 2 will start to cycle even though I have not pressed the On/Off switch. If I adjust the Pot, the TOGGLE rate will change. I have to press and hold the On/Off button until the second relay finishes its cycle. Both relays turn on and stay on. The second button press turns off both relays. Relay 2 starts to cycle again on its own.
I was expecting relay 1 and 2 to be off and wait for the first button press. But for some reason relay 2 starts to toggle. Is there something wrong with the ONOff: subroutine?
Here is the code listing.
SYMBOL Btn1 = b0
SYMBOL PVal = b1
SYMBOL Delay = w3
Start:
PINS = %0000000
GOSUB CheckPot
OnOff:
BUTTON 6,0,255,0,Btn1,0,OnOff
TOGGLE 0
TOGGLE 1
GOSUB CheckPot
MainLoop:
PAUSE Delay
TOGGLE 1
GOSUB CheckPot
BUTTON 6,0,255,0,Btn1,0,MainLoop
END
CheckPot:
POT 5,20,PVal
IF PVal = 0 THEN Five
IF PVal > 0 and PVal <51 Then Ten
IF PVal >= 51 and PVal < 102 Then Fifteen
IF PVal >= 102 and PVal < 153 Then Twenty
IF PVal >= 153 and PVal < 204Then TwentyFive
IF PVal > 204 Then Thirty
Five:
Delay = 5000
Return
Ten:
Delay = 10000
Return
Fifteen:
Delay = 15000
Return
Twenty:
Delay = 20000
Return
TwentyFive:
Delay = 25000
Return
Thirty:
Delay = 30000
Return
Comments
Also, your PINS command is hinky, it should probably be PINS=%00000011, 8 numbers, pins 0 and 1 are outputs.
After much trial and error I came up with this program. The only quirk to the operation of the relays is when you turn the relays off. I have to turn the potentiometer all the way down to the shortest timing and then press the momentary push button a few times. Sometimes it turns off right away. Other times it takes a few presses.
It will certainly do the job. I had to put PAUSES in to help the push button be more reliable.
PVal = b1
SYMBOL Delay = w3
Start:
PAUSE 300
HIGH 0
PAUSE 300
HIGH 1
PAUSE 300
OnOff:
IF Pin6 = 1 then OnOff
PAUSE 300
Debug "Turn On" ,cr
LOW 0
TOGGLE 1
GOSUB CheckPot
MainLoop:
Debug "Main Loop" ,cr
IF Pin6 = 0 then Start
PAUSE Delay
IF Pin6 = 0 then Start
PAUSE 300
TOGGLE 1
IF Pin6 = 0 then Start
PAUSE 300
GOSUB CheckPot
GOTO MainLoop
CheckPot:
Debug "Check Pot" , cr
POT 5,121,PVal
IF PVal <= 5 THEN Five
Delay = PVal/5*1000
Return
Five:
Delay = 1000
Return