Shop OBEX P1 Docs P2 Docs Learn Events
PSU controller - program help — Parallax Forums

PSU controller - program help

qazxsw21000qazxsw21000 Posts: 13
edited 2012-02-21 20:08 in BASIC Stamp
Here is my program:
' {$STAMP BS2}
' {$PBASIC 2.5}


a:                                   'label a
HIGH 0:LOW 1                         'hold PSU off and red LED off


IF IN15=1 THEN                       'if on button pressed
LOW 0                                'turn PSU on
PAUSE 1000                           'wait one second
ELSEIF IN14=1 OR IN13=0 THEN         'else, if off button is pressed or ok signal absent
GOSUB b                              'goto sub label b
GOTO a                               'goto a


ELSE                                 'else
GOTO a                               'goto a
ENDIF                                'end if-then statement
GOTO a                               'goto a


b:                                   'sub label b
IF IN13=0 THEN                       'if ok signal absent
HIGH 0 : HIGH 1                      'turn PSU off and turn red LED on
END                                  'stop running


ELSEIF IN13=1 THEN                   'if ok signal is present
LOW 0 : LOW 1                        'turn red LED off and PSU on
ENDIF                                'end if-then statement
RETURN                               'go back to main program

What happens is that the basic stamp stops executing code towards the beginning. I have to hold the "on" switch down to keep the PSU on. Can anyone help me? I can't find the problem.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-20 15:51
    I can't really tell what you want it to do.

    It looks like if the button is pressed it turns on the power supply, waits a second and starts over "GOTO a" where it turns the power supply back off.

    Can you expain what you want it to do?
  • qazxsw21000qazxsw21000 Posts: 13
    edited 2012-02-20 15:58
    I want it to keep the PSU off and turn the red LED off. Then it waits for input from one button. When it detects that the button has been pressed, it turns the PSU on for one second and looks for the "Power OK" signal generated by the PSU. It then looks for either the second button to be pressed or the "Power OK" signal to go away. If it's the button that was pressed, it turns the PSU off, this is where it goes back to the start. However, if it's the "Power OK" signal that is not present, the BS2 turns the PSU off and the red LED on, and stops running the program; you have to press the reset button to restart the code.

    EDIT:
    the program I was basing my code on, which does not support coding BASIC, had a BASIC viewer which, on the program, gave this:
    main:
    label0:
      Switch on 5
      Switch off 3
    label1:
      if Input0 is On then label2
      goto label1
    label2:
      switch off 5
      pause 500
    label3:
      if Input3 is On then label4
      Switch on 5
      Switch on 3
      end
    label4:
      if Input1 is On then label0
      goto label3
      goto label0
    
    If I edited that, it should say this right?:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    
    main:
    a:
      HIGH 0
      LOW 1
    b:
      IF IN15=1 THEN  c
        GOTO b
    c:
      LOW 0
      PAUSE 1000
    d:
      IF IN13=0 THEN e
      HIGH 0
      HIGH 1
      END
    
    
    e:
      IF IN14=1 THEN a
      GOTO d
      GOTO a
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-20 17:02
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    a:                              'label a
    HIGH 0:LOW 1                    'hold PSU off and red LED off
    IF IN15=1 THEN                  'if on button pressed 
      LOW 0                         'turn PSU on 
      PAUSE 1000                    'wait one second 
      c:
      IF IN14=1 OR IN13=0 THEN      'else, if off button is pressed or ok signal absent
        GOTO b                      'goto label b 
      ELSE
        GOTO c                      'goto c watch off button and ok signal again
      ENDIF
    ENDIF                           'end if-then statement
    GOTO a                          'goto a
    b:                              'b beginning of end, program will end soon
    HIGH 0                          'turn PSU off 
    IF IN13=0 THEN                  'if ok signal absent
      HIGH 1                        'turn red LED on
      PAUSE 2000                    'leave LED on for 2 seconds
    ELSE
      LOW 1                         'turn red LED off (was it ever on?)
    ENDIF                           'end if-then statement
    END
    
    Okay, I don't use Basic Stamps anymore but I think this will do what you want. I really don't like posting code I haven't tested (nor compiled) so let me know if this works. Did you want the LED to stay on? I don't think you can have the LED stay on unless you leave the program running. As it is now, the LED will stay on for two seconds. I think you could use the same button for both on and off if you wanted to. Just use the same pin number.
    Edit: I think the formatting is okay now.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-20 17:18
    Did you want the LED on only if it loses it the "ok" signal? If so, you can remove all the "LOW 1" statements since the LED is never turn on until the program is about to end so it wont ever need to be turned off.
  • qazxsw21000qazxsw21000 Posts: 13
    edited 2012-02-20 19:34
    That works. However, after the off button is pressed, it is supposed to go back to waiting for the on button. The reason for the red LED is to indicate that there was a problem with the PSU. I put "turn the LED off" to make sure that at the start of the execution, the LED is off (no problem should happen yet). And my second code, did not work. But I think I know where mine went wrong: It was running straight into the subroutine instead of waiting for the required condition.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-20 19:52
    So you only want the program to end if the "ok" signal is lost?

    This should let you turn the PSU on and off with the program waiting for the on button after it has been turned off.
    b:                              'b program will end soon if no ok signal found
    HIGH 0                          'turn PSU off 
    IF IN13=0 THEN                  'if ok signal absent
      HIGH 1                        'turn red LED on
      PAUSE 2000                    'leave LED on for 2 seconds
    ELSE
      [COLOR=#ff0000]GOTO a
    [/COLOR]ENDIF                           'end if-then statement
    END
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-20 20:06
    Here's another version.

    This program always stays running. If the ok signal is lost the LED will come on and the PSU will turn off. When the PSU is turned on again and it finds the ok signal, then the LED will be turned back off.

    So if the LED is on, it means the last time the power supply was turned off was because of the ok signal had been lost.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    a:                              'label a
    HIGH 0                          'hold PSU off 
    IF IN15=1 THEN                  'if on button pressed 
      LOW 0                         'turn PSU on 
      PAUSE 1000                    'wait one second 
      c:
      IF IN14=1 OR IN13=0 THEN      'if off button is pressed or ok signal absent
        GOTO b                      'goto label b 
      ELSE
        LOW 1                       'make sure LED is off
        GOTO c                      'goto c watch off button and ok signal again
      ENDIF
    ENDIF                           'end if-then statement
    GOTO a                          'goto a
    b:                              'b prepare to wait for on button again
    HIGH 0                          'turn PSU off 
    IF IN13=0 THEN                  'if ok signal absent
      HIGH 1                        'turn red LED on
    ENDIF                           'end if-then statement
    PAUSE 1000                      'wait a second so on and off button can be shared
    GOTO a
    END
    

    Again, this program will let you use the same button for both on and off if you wanted.
  • qazxsw21000qazxsw21000 Posts: 13
    edited 2012-02-21 17:31
    That code did the trick. Thank you so much. Obviously, I am not meant to be a programmer.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-21 20:08
    Obviously, I am not meant to be a programmer.

    No, no, no. It just means you need more practice. Like so many things, programming gets easier the more you pactice at it.

    And, you're very welcome.
Sign In or Register to comment.