Shop OBEX P1 Docs P2 Docs Learn Events
BS1 Controlling Relays — Parallax Forums

BS1 Controlling Relays

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


Comments

  • ercoerco Posts: 20,256
    edited 2016-03-07 15:42
    The problem could be in your button wiring. Looks like your downstate is 0 or low, so do you have a pullup resistor on pin 6 (your switch)? See http://www.parallax.com/go/PBASICHelp/Content/LanguageTopics/Commands/BUTTON.htm

    Also, your PINS command is hinky, it should probably be PINS=%00000011, 8 numbers, pins 0 and 1 are outputs.
  • Thanks for the reply. I will double check my circuit and make a change in the PINS command and see how it goes when I get home from work.
  • ercoerco Posts: 20,256
    Actually you have several errors more serious than PINS. END after mainloop and returns without gosubs. Take another shot at it.
  • ercoerco Posts: 20,256
    edited 2016-03-07 21:05
    Here's my quick attempt, untested. Try it, it's set up for a normally high switch. Real programmers can improve on this, I'm sure.
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    
    'bit0 is a toggling flag, relays off or on (LSB of B0)
    'B1=POT value
    'B2=fudge factor to calibrate timing interval, 1-100
    'W2=calculated timing interval, B1*B2
    'W3=timing counter
    'switch on pin 6=normally high, low when pressed
    
    B2=100   '  fudge factor to calibrate relay timing interval, 1-200
    
    Main:
    checkswitch:IF PIN6=1 THEN notpressed    'button not pressed, jump ahead
    PAUSE 250  ' short debounce delay
    BIT0=BIT0+1  ' increment BIT0, always 0 or 1
    pressed:IF PIN6=0 THEN pressed ' keep looping back here if button pressed
    
    notpressed:
    IF BIT0=1 THEN relays  ' if flag BIT0=1 then goto relays
    LOW 0    'relay 0 off
    LOW 1    'relay 1 off
    GOTO Main
    
    relays:
    HIGH 0        ' relay 0 on
    HIGH 1        ' relay 1 on
    
    relay1toggle:
    POT 5,20,B1          ' read pot
    W2=B1*B2+5000        ' calculate timing interval, or number of loops, minimum 5000
    
    FOR W3=1 TO W2       ' start timing loop
    IF PIN6=0 THEN main  ' if button pressed, exit loop and go back to main
    PAUSE 1              ' short pause
    NEXT W3              ' end timing loop
    TOGGLE 1             ' toggle relay 1 state
    GOTO relay1toggle    ' loop back to relay1toggle
    

  • Thank you for your input. I am a novice. But I am having fun! I finally found a use for this BS1 I bought in the 1990's!
    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
Sign In or Register to comment.