Shop OBEX P1 Docs P2 Docs Learn Events
Hard Task in need of assistance — Parallax Forums

Hard Task in need of assistance

rawrrawr Posts: 4
edited 2004-12-10 09:12 in BASIC Stamp
My robot will have three pushbuttons and three LEDs; red, yellow, and green.· My program will make my robot go fast and have a green LED go on, when the go button is pressed.· It will continue to do this task until another button is pressed.· My robot will go slower when the yellow button is pressed.· It will continue to do this task until another button is pressed.·

My Question is what commands do i use... if else, if than, else? confused.gif

Please help me get my program off the drawing board.

·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-02 11:57
    Give this idea a try; it uses a subroutine to scan and debouce all three buttons and will only change operational modes when a single button is pressed.

    Main: 
      DO 
        GOSUB Scan_Buttons 
        IF (buttons > 0) THEN 
          temp = $F 
          LOOKDOWN buttons, [noparse][[/noparse]%001, %010, %100], temp 
          IF (temp <> $F) THEN mode = temp 
        ENDIF 
    
        ON mode GOSUB Mode_0, Mode_1, Mode_2 
      LOOP 
    
    
    Mode_0: 
      GreenLed = On
      YellowLed = Off
      RedLed = Off
      RETURN 
    
    
    Mode_1: 
      GreenLed = Off
      YellowLed = On
      RedLed = Off
      RETURN 
    
    
    Mode_2: 
      GreenLed = Off
      YellowLed = Off
      RedLed = On
      RETURN
     
     
    Scan_Buttons: 
      buttons = %0111                ' assume pressed 
      FOR idx = 1 TO 4               ' debounce for 20 mS 
        buttons = buttons & ~INA     ' active-low inputs on INA 
        PAUSE 5 
      NEXT 
      RETURN
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • rawrrawr Posts: 4
    edited 2004-12-03 09:48
    thank you
  • rawrrawr Posts: 4
    edited 2004-12-07 11:29
    hey, i'm going to add another function in this program.· I'm going to add something that makes the robot stop when a push button is pressed.

    Main:
    · DO
    ··· GOSUB Scan_Buttons
    ··· IF (buttons > 0) THEN
    ···· ·temp = $F
    ···· ·LOOKDOWN buttons, [noparse][[/noparse]%001, %010, %100], temp
    ···· ·IF (temp <> $F) THEN mode = temp
    ··· ENDIF

    ··· ON mode GOSUB Mode_0, Mode_1, Mode_2
    · LOOP


    Mode_0:
    ··GreenLed = On
    · YellowLed = Off
    ··RedLed = Off
    · RETURN


    Mode_1:
    ··GreenLed =·Off
    · YellowLed = On
    ··RedLed = Off
    · RETURN


    Mode_2:
    ··GreenLed =·Off
    · YellowLed = Off
    ··RedLed = On
    · RETURN


    Scan_Buttons:
    · buttons = %0111··············· ' assume pressed
    · FOR idx = 1 TO·4·············· ' debounce for 20 mS
    ··· buttons = buttons & ~INA···· ' active-low inputs on INA
    ··· PAUSE 5
    · NEXT
    · RETURN

    that's the program you gave me.· How will I add a Mode_3?
  • nick bernardnick bernard Posts: 329
    edited 2004-12-07 14:17
    Main:
    DO
    GOSUB Scan_Buttons
    IF (buttons > 0) THEN
    temp = $F
    LOOKDOWN buttons, [noparse][[/noparse]%001, %010, %100,%1000], temp
    IF (temp <> $F) THEN mode = temp
    ENDIF

    ON mode GOSUB Mode_0, Mode_1, Mode_2, Mode_3
    LOOP


    Mode_0:
    GreenLed = On
    YellowLed = Off
    RedLed = Off
    RETURN


    Mode_1:
    GreenLed = Off
    YellowLed = On
    RedLed = Off
    RETURN


    Mode_2:
    GreenLed = Off
    YellowLed = Off
    RedLed = On
    RETURN


    Mode_3:
    GreenLed = Off
    YellowLed = Off
    RedLed = Off
    RETURN


    Scan_Buttons:
    buttons = %1111 ' assume pressed
    FOR idx = 1 TO 4 ' debounce for 20 mS
    buttons = buttons & ~INA ' active-low inputs on INA
    PAUSE 5
    NEXT
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    League Bowling.... it's not a sport, it's a way of life

    Post Edited (nick bernard) : 12/7/2004 2:20:30 PM GMT
  • nick bernardnick bernard Posts: 329
    edited 2004-12-07 14:21
    something like that?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    League Bowling.... it's not a sport, it's a way of life
  • rawrrawr Posts: 4
    edited 2004-12-10 02:49
    '{$STAMP BS2}
    '{$PBASIC 2.5}

    red VAR IN0
    yellow VAR IN3
    green VAR IN6
    crash VAR IN9
    counter VAR Word

    Main:
    DO
    GOSUB Scan_Buttons
    IF (buttons > 0) THEN
    temp = $F
    LOOKDOWN buttons, [noparse][[/noparse]%001, %010, %100,%1000], temp
    IF (temp <> $F) THEN mode = temp
    ENDIF

    ON mode GOSUB Green, Yellow, Red, Crash
    LOOP


    Green:
    HIGH 6
    DEBUG "Green", CR

    FOR COUNTER = 1 TO 375
    PULSOUT 15, 50
    PULSOUT 14, 1500
    NEXT
    RETURN


    Yellow:
    HIGH 3
    DEBUG "Yellow", CR

    FOR COUNTER = 1 TO 375
    PULSOUT 15, 50
    NEXT
    RETURN


    Red:
    DEBUG "Red", CR
    STOP
    RETURN


    Crash:
    DEBUG "Crash", CR: FREQOUT 9, 1000, 1568
    RETURN

    Scan_Buttons:
    buttons = %1111 ' assume pressed
    FOR idx = 1 TO 4 ' debounce for 20 mS
    buttons = buttons & ~INA ' active-low inputs on INA
    PAUSE 5
    NEXT
    RETURN

    how come buttons is an unidentified symbol??? plz resond quickly.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2004-12-10 09:12
    rawr -

    The simple and obvious answer as to why it's being flagged as undefined, is that it wasn't defined before it was used. It's customary to define all the variables and constants that you are going to use in your program at or near the beginning of your program. Adding the following definition will eliminate that particular errror, but there are many others in the program:

    buttons· var· ·nib· 'allocate 4 bits of RAM for button state

    Just as an observation a good deal was changed in this program from when Jon originally supplied it. Notable among the changes which are about to cause you trouble, are the changes below (as examples):

    Former····· Latter········· Comment
    ······
    ··········
    redled··· -> red····redled was unique, red now defined both as var name + program label

    greenled-> green· greenled was unique, green now defined both as var name + program label.

    Other changes similar to those were also made. You may want to just go back to the original program that Jon provided,·and make your changes again. A good deal was lost in the last alteration process.

    Regards,

    Bruce Bates
Sign In or Register to comment.