Shop OBEX P1 Docs P2 Docs Learn Events
push button routine — Parallax Forums

push button routine

graffixgraffix Posts: 389
edited 2012-11-09 20:47 in BASIC Stamp
I have a NO push button when I press the button my debug changes btn2=0 to btn2 = 1. When btn2 = 1 then mode = mode + 1. When mode =1 then light a led.

IF Btn2 = 1 THEN
mode = mode + 1
IF mode = 1 THEN HIGH 14

IF mode = 0 THEN LOW 14

I'm sure theres a few ways to get it to latch.
It dosent matter how long I hold down the push button the debug terminal shows a constant value of 1 for Btn2. The modes debug value constantly changes from 0 to 1 depending on how long I hold the button. I need the mode led to only change once no matter how long I hold the button. Im not sure how to code it? If I could get a suggestion for this portion I can work on the rest.
Overview:
The code is part of a larger controller/ boe bot/ xbee project. Eventually I'd like when mode = 1 then my left right joystick controls the ping servo's movement proportional to the joystick.Instead of turning the boe bot. While sending the ping readings to the controllers lcd screen. If not during the movement just after movement stops send the ping reading.

If you want to see more of the codes I have I can post them. Thanks ahead of time.

Comments

  • davejamesdavejames Posts: 4,047
    edited 2012-10-31 13:12
    Hi graffix,

    I'd say, post your code so we can see all the nuances. :thumb:

    I'm not understanding the explanation/description given.
  • ercoerco Posts: 20,256
    edited 2012-10-31 17:07
    Do you have a pulldown resistor on your switch?
  • graffixgraffix Posts: 389
    edited 2012-11-01 05:05
    boe bots code
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    'Recieving Boe Bot
    
    ' -----[ Constants ]-------------------------------------------------------
    
    L_FWD            CON     1000
    L_REV            CON     500
    L_STOP           CON     750
    R_FWD            CON     500
    R_REV            CON     1000
    R_STOP           CON     750
    
    CmConstant       CON     2260
    InConstant       CON     890
    
    BAUD_MODE        CON     84
    
    '---Variables-------------------------------------------
    UD               VAR     Byte
    LR               VAR     Byte
    a                VAR     Byte
    b                VAR     Byte
    c                VAR     Byte
    d                VAR     Byte
    e                VAR     Byte
    f                VAR     Byte
    
    Btn1             VAR     Byte
    Btn2             VAR     Byte
    
    cmDistance       VAR     Word
    inDistance       VAR     Word
    time             VAR     Word
    
    mode             VAR     Byte
    
    '---Pin Descriptions------------------------------------
    xbee_rts         PIN     9
    xbee_Rx          PIN     10    ' Dout
    xbee_Tx          PIN     11    ' Din
    
    nInp             PIN     12    'Define the input pin of servo pal
    Alarm            PIN     13    'Define the alarm pin of servo pal
    
    HM55B_clk        PIN     0
    HM55B_en         PIN     1
    HM55B_Din_Dout   PIN     2
    
    Bi_clrLEDred     PIN     6     ' when high + low 7
    Bi_clrLEDgreen   PIN     7     ' when high + low 6
    
    ping             PIN     15
    ping_servo       PIN     14
    
    '---initializations------------------------------------
    INPUT nInp                'Make sure nInp isn't being driven.
    DO : LOOP UNTIL nInp      'Wait for ServoPAL to power up.
    
    LOW nInp                  'Set pin to an output and hold it low
    PAUSE 100                 '  for 100mS to reset ServoPAL.
    HIGH nInp                 'Raise the pin.
    PAUSE 100
    
    
    '---main program------------------------------------------
    DO
      SERIN XBee_Tx\xbee_rts, 84, [WAIT("!"),UD,LR,Btn1,Btn2,mode]
    
      PULSOUT 15, 5
      PULSIN 15, 1, time
    
      cmDistance = cmConstant ** time
      inDistance = inConstant ** time
    
      DEBUG HOME, "Boe Bot Com 13", CLREOL, CR,
                  "UD = ", DEC UD, CLREOL, CR,
                  "LR = ", DEC LR, CLREOL, CR,
                  "Btn1 = ", DEC Btn1, CLREOL, CR,
                  "Btn2 = ", DEC Btn2, CLREOL, CR,
                  "cm = ", DEC3 cmDistance,CLREOL, CR,
                  "in = ", DEC3 inDistance,CLREOL, CR,
                  "mode = ", DEC mode,CLREOL
    
      SELECT Btn2                                  'code locks up when pressed
        CASE Btn2 = 1
          SEROUT XBee_Rx\xbee_rts, 84, ["in",indistance]
    
      ENDSELECT
    
          'IF LR = > 140 THEN PULSOUT 14, 1200 ELSE PULSOUT 14,750
          'IF LR = < 116 THEN PULSOUT 14, 250
      SELECT UD
        CASE < 90
          UD = a
          GOSUB forwards
        CASE > 110
          UD = b
          GOSUB backwards
        CASE 90 TO 110
          UD = c
      ENDSELECT
    
      SELECT LR
        CASE < 110
          LR = d
          GOSUB right
        CASE > 140
          LR = e
          GOSUB left
        CASE 110 TO 140
          LR = f
      ENDSELECT
    
      IF (UD = c)AND(LR = f) THEN
        GOSUB stall
      ENDIF
    
    LOOP
    
    '---Subroutines------------------------------------------
    forwards:
      PULSOUT nInp, 1000       'Program right servo for full forward.
      PULSOUT nInp, 500        'Program left servo for full forward.
      PULSOUT nInp, 10         'Program alarm for 1 seconds.
    
      DO : LOOP UNTIL Alarm    'Wait for Alarm.
    
    RETURN
    
    backwards:
      PULSOUT nInp, 500        'Program right servo for full reverse.
      PULSOUT nInp, 1000       'Program left servo for full reverse.
      PULSOUT nInp, 10         'Program alarm for 1 seconds.
    
      DO : LOOP UNTIL Alarm    'Wait for Alarm.
    
    RETURN
    
    left:
      PULSOUT nInp, 1000       'Program right servo for full forward.
      PULSOUT nInp, 2000       'Turn right servo off.
      PULSOUT nInp, 10         'Program alarm for 1 seconds.
    
      DO : LOOP UNTIL Alarm    'Wait for Alarm.
    RETURN
    
    right:
      PULSOUT nInp, 2000       'Turn left servo off.
      PULSOUT nInp, 500        'Program left servo for full forward.
      PULSOUT nInp, 10         'Program alarm for 1 seconds.
    
      DO : LOOP UNTIL Alarm    'Wait for Alarm.
    RETURN
    
    stall:
      PULSOUT nInp, 2000       'Turn right servo off.
      PULSOUT nInp, 2000       'Turn left servo off.
    RETURN
    
    controllers code
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' XBee controller
    
    '----------------[Initialize]---------------------------
    PAUSE 5
    SEROUT 0, 84, [22,12]
    
    SEROUT 0, 84,50, [22,17, "Hello", 13, "John"]
    PAUSE 1000
    
    '---------------[Variables]-----------------------------
    LR            VAR     Word
    UD            VAR     Word
    Btn1wrk       VAR     Byte
    Btn2wrk       VAR     Byte
    green         VAR     Byte
    red           VAR     Byte
    
    inDistance    VAR     Word
    mode          VAR     Bit
    
    '---------------[Constants]-----------------------------
    #SELECT $STAMP
     #CASE BS2, BS2E, BS2PE
       T9600      CON     84
    #ENDSELECT
    
    Lcd           PIN     0
    Btn1          PIN     7
    Btn2          PIN     5
    Tx            PIN     9  ' XBee DIN
    Rx            PIN     10 ' XBee DOUT
    RTS           PIN     8  ' XBee RTS
    LRjoy         PIN     6
    UDjoy         PIN     4
    Led_A         PIN     15
    Led_B         PIN     14
    
    PIEZO         PIN     3
    
    stickUD       CON     25
    stickLD       CON     25
    Baud          CON     T9600
    capture       CON     500
    '-------------------------[Main]---------------------------
    
    Main:
    DO
      HIGH 6
      PAUSE 2
      RCTIME 6, 1, LR
      HIGH 4
      PAUSE 2
      RCTIME 4, 1, UD
    
      UD = UD/3
      LR = LR/3
    
      DEBUG HOME, "Controller Com 1" ,CLREOL, CR,
                  "UD = ", DEC UD, CLREOL, CR,
                  "LR = ", DEC LR, CLREOL, CR,
                  "Btn1 = ", DEC Btn1, CLREOL, CR,
                  "Btn2 = ", DEC Btn2, CLREOL, CR,
                  "inDistance = ", DEC3 inDistance, CLREOL, CR,
                  "mode = ", DEC mode, CLREOL
      SEROUT Rx\RTS, 84, ["!",UD,LR,Btn1,Btn2,mode]
    
    
    
      IF Btn2 = 1 THEN
        mode = mode + 1
        'GOSUB Get_ping
      ENDIF
    
      IF mode = 1 THEN HIGH 14
        'GOSUB Get_ping
      IF mode = 0 THEN LOW 14
    LOOP
    ' -----[ Subroutines ]--------------------------------------------------------
    'Get_ping:
      SERIN Tx\RTS, 84, 200, Com_error,
            [WAIT("in"),inDistance]
      RETURN
    
    
    Com_Error:                      ' Handle timeout during tilt controlled navigation
    
      DEBUG "A communication error occurred...Reconnecting", CR     ' Error message
    
        FREQOUT PIEZO, 100, 3000                 ' Indicate error
        PAUSE 100
        FREQOUT PIEZO, 100, 3000
        PAUSE 100
        FREQOUT PIEZO, 100, 3000
        PAUSE 1000
    
      GOTO main
    

    Ok Im back, took my kids out Trick or Treatn. Yes Erco I have pull down resistors on both of my push buttons. These are the latest codes I have. They are still in a rough design phase. A few lines arent being used. They have the apostrophe inserted. Where I was trying to get a time out function working.
  • graffixgraffix Posts: 389
    edited 2012-11-01 09:49
    Im uploading a video. As soon as I can attach it I will. I also changed the code above to match the video. I made a few changes since I posted it earlier. I didnt really wanna turn this into two threads on the same project.

    http://www.youtube.com/watch?v=KYnqRgbv6PU&feature=g-crec-u

    It's kind of a crappy video, Idk hope it helps
  • graffixgraffix Posts: 389
    edited 2012-11-02 08:12
    Well I worked on it some more. I made some progress and found another bug :). I made a case statement out of the mode sequence. So when I get btn2 to latch (mode = 1). Then my LR joystick controls the ping servo. Except it controls both the drive servo's and the ping servo. I see the problem and I'll keep working on it.

    edit: I fixed that also. When mode = 1 the LR joystick only controls the ping servo. The problem of how long I hold Btn2 down and the mode flickering 0,1,0,1 is still an issue. I also still am not getting the ping reading back to the contollers LCD yet.

    boe code
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    'Recieving Boe Bot
    
    '---[ Constants ]-------------------------------------------
    
    L_FWD            CON     1000
    L_REV            CON     500
    L_STOP           CON     750
    R_FWD            CON     500
    R_REV            CON     1000
    R_STOP           CON     750
    
    CmConstant       CON     2260
    InConstant       CON     890
    
    BAUD_MODE        CON     84
    
    '---[Variables]-------------------------------------------
    UD               VAR     Byte
    LR               VAR     Byte
    a                VAR     Byte
    b                VAR     Byte
    c                VAR     Byte
    d                VAR     Byte
    e                VAR     Byte
    f                VAR     Byte
    
    Btn1             VAR     Byte
    Btn2             VAR     Byte
    
    cmDistance       VAR     Word
    inDistance       VAR     Word
    time             VAR     Word
    
    mode             VAR     Byte
    
    '---[Pin Descriptions]------------------------------------
    xbee_rts         PIN     9
    xbee_Rx          PIN     10    ' Dout
    xbee_Tx          PIN     11    ' Din
    
    nInp             PIN     12    'Define the input pin of servo pal
    Alarm            PIN     13    'Define the alarm pin of servo pal
    
    HM55B_clk        PIN     0
    HM55B_en         PIN     1
    HM55B_Din_Dout   PIN     2
    
    Bi_clrLEDred     PIN     6     ' when high + low 7
    Bi_clrLEDgreen   PIN     7     ' when high + low 6
    
    ping             PIN     15
    ping_servo       PIN     14
    
    '---[Initializations]------------------------------------
    INPUT nInp                'Make sure nInp isn't being driven.
    DO : LOOP UNTIL nInp      'Wait for ServoPAL to power up.
    
    LOW nInp                  'Set pin to an output and hold it low
    PAUSE 100                 '  for 100mS to reset ServoPAL.
    HIGH nInp                 'Raise the pin.
    PAUSE 100
    
    
    '---[Main Program]------------------------------------------
    DO
      SERIN XBee_Tx\xbee_rts, 84, [WAIT("!"),UD,LR,Btn1,Btn2,mode]
    
      PULSOUT 15, 5
      PULSIN 15, 1, time
    
      cmDistance = cmConstant ** time
      inDistance = inConstant ** time
    
      DEBUG HOME, "Boe Bot Com 13", CLREOL, CR,
                  "UD = ", DEC UD, CLREOL, CR,
                  "LR = ", DEC LR, CLREOL, CR,
                  "Btn1 = ", DEC Btn1, CLREOL, CR,
                  "Btn2 = ", DEC Btn2, CLREOL, CR,
                  "cm = ", DEC3 cmDistance,CLREOL, CR,
                  "in = ", DEC3 inDistance,CLREOL, CR,
                  "mode = ", DEC mode,CLREOL
    
      SELECT Btn2                                  'code locks up when pressed
        CASE Btn2 = 1
          SEROUT XBee_Rx\xbee_rts, 84, ["in",indistance]
    
      ENDSELECT
    
      SELECT mode
        CASE = 1
          IF LR = > 140 AND mode = 1 THEN PULSOUT ping_servo, 1200 ELSE PULSOUT ping_servo,750
          IF LR = < 110 AND mode = 1 THEN PULSOUT ping_servo, 250
    
      ENDSELECT
    
      SELECT UD
        CASE < 90
          UD = a
          GOSUB forwards
        CASE > 110
          UD = b
          GOSUB backwards
        CASE 90 TO 110
          UD = c
      ENDSELECT
    
      SELECT LR
        CASE < 110
          LR = d
          IF LR = < 110 AND mode = 0 THEN GOSUB right
        CASE > 140
          LR = e
          IF LR = > 140 AND mode = 0 THEN GOSUB left
        CASE 110 TO 140
          LR = f
      ENDSELECT
    
      IF (UD = c)AND(LR = f) THEN
        GOSUB stall
      ENDIF
    
    LOOP
    
    '---[Subroutines]------------------------------------------
    forwards:
      PULSOUT nInp, 1000       'Program right servo for full forward.
      PULSOUT nInp, 500        'Program left servo for full forward.
      PULSOUT nInp, 10         'Program alarm for 1 seconds.
    
      DO : LOOP UNTIL Alarm    'Wait for Alarm.
    
    RETURN
    
    backwards:
      PULSOUT nInp, 500        'Program right servo for full reverse.
      PULSOUT nInp, 1000       'Program left servo for full reverse.
      PULSOUT nInp, 10         'Program alarm for 1 seconds.
    
      DO : LOOP UNTIL Alarm    'Wait for Alarm.
    
    RETURN
    
    left:
      PULSOUT nInp, 1000       'Program right servo for full forward.
      PULSOUT nInp, 2000       'Turn right servo off.
      PULSOUT nInp, 10         'Program alarm for 1 seconds.
    
      DO : LOOP UNTIL Alarm    'Wait for Alarm.
    RETURN
    
    right:
      PULSOUT nInp, 2000       'Turn left servo off.
      PULSOUT nInp, 500        'Program left servo for full forward.
      PULSOUT nInp, 10         'Program alarm for 1 seconds.
    
      DO : LOOP UNTIL Alarm    'Wait for Alarm.
    RETURN
    
    stall:
      PULSOUT nInp, 2000       'Turn right servo off.
      PULSOUT nInp, 2000       'Turn left servo off.
    RETURN
    
    contoller code
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' XBee controller
    
    '---[Initialize]-------------------------------------------------
    PAUSE 5
    SEROUT 0, 84, [22,12]
    
    SEROUT 0, 84,50, [22,17, "Hello", 13, "John"]
    PAUSE 1000
    
    '---[Variables]---------------------------------------------------
    LR            VAR     Word
    UD            VAR     Word
    Btn1wrk       VAR     Byte
    Btn2wrk       VAR     Byte
    green         VAR     Byte
    red           VAR     Byte
    
    inDistance    VAR     Word
    mode          VAR     Bit
    
    '---[Constants]----------------------------------------------------
    #SELECT $STAMP
     #CASE BS2, BS2E, BS2PE
       T9600      CON     84
    #ENDSELECT
    
    stickUD       CON     25
    stickLD       CON     25
    Baud          CON     T9600
    capture       CON     500
    
    '---[Pin descriptions]---------------------------------------------
    
    Lcd           PIN     0
    Btn1          PIN     7
    Btn2          PIN     5
    Tx            PIN     9  ' XBee DIN
    Rx            PIN     10 ' XBee DOUT
    RTS           PIN     8  ' XBee RTS
    LRjoy         PIN     6
    UDjoy         PIN     4
    Led_A         PIN     15
    Led_B         PIN     14
    
    PIEZO         PIN     3
    
    
    '---[Main]-----------------------------------------------------------
    
    Main:
    DO
      HIGH 6
      PAUSE 2
      RCTIME 6, 1, LR
      HIGH 4
      PAUSE 2
      RCTIME 4, 1, UD
    
      UD = UD/3
      LR = LR/3
    
      DEBUG HOME, "Controller Com 1" ,CLREOL, CR,
                  "UD = ", DEC UD, CLREOL, CR,
                  "LR = ", DEC LR, CLREOL, CR,
                  "Btn1 = ", DEC Btn1, CLREOL, CR,
                  "Btn2 = ", DEC Btn2, CLREOL, CR,
                  "inDistance = ", DEC3 inDistance, CLREOL, CR,
                  "mode = ", DEC mode, CLREOL
      SEROUT Rx\RTS, 84, ["!",UD,LR,Btn1,Btn2,mode]
    
    
    
      IF Btn2 = 1 THEN
        mode = mode + 1
        'GOSUB Get_ping
      ENDIF
    
      IF mode = 1 THEN HIGH 14
        'GOSUB Get_ping
      IF mode = 0 THEN LOW 14
    LOOP
    '---[ Subroutines ]--------------------------------------------------------
    'Get_ping:
      SERIN Tx\RTS, 84, 200, Com_error,
            [WAIT("in"),inDistance]
      RETURN
    
    
    Com_Error:                      ' Handle timeout during tilt controlled navigation
    
      DEBUG "A communication error occurred...Reconnecting", CR     ' Error message
    
        FREQOUT PIEZO, 100, 3000                 ' Indicate error
        PAUSE 100
        FREQOUT PIEZO, 100, 3000
        PAUSE 100
        FREQOUT PIEZO, 100, 3000
        PAUSE 1000
    
      GOTO main
    

    CODE REVISED see here http://forums.parallax.com/showthread.php?143290-xbee-not-recieving-proper-values
    post #8
  • skylightskylight Posts: 1,915
    edited 2012-11-08 14:59
    IF Btn2 = 1 THEN
    mode = mode + 1
    'GOSUB Get_ping
    ENDIF
    
    IF mode = 1 THEN HIGH 14
    'GOSUB Get_ping
    IF mode = 0 THEN LOW 14
    

    Looking at your code there is no use of the BUTTON command as debounce and when you press the button you could be adding +1 to the variable "mode" several times,you're conditions only allow for mode to be 1 or 0 you need to make sure mode cannot be higher than 1 if the button contacts bounce or if the button is pressed more than once by putting something like IF mode >1 then mode =1 in your code.
  • graffixgraffix Posts: 389
    edited 2012-11-08 19:17
    Thanks for the reply. I'll give that try as soon as I get home from work.
  • graffixgraffix Posts: 389
    edited 2012-11-08 21:56
    It didnt work? Or I did it wrong some how? Idk
  • graffixgraffix Posts: 389
    edited 2012-11-09 08:35
    I'm looking at page 140 of the basic stamp syntax reference manual. I think that will help. I gotta work now I'll check later.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-11-09 08:45
    You want the button action to happen only once each time the button is pressed. The BUTTON command help with that, but it is more efficient to use a second Bit variable.
    [FONT=courier new]Btn2new VAR Bit
    Btn2old VAR Bit
    Btn2 PIN 5
    
    Btn2new = Btn2
    IF Btn2new=1 AND Btn2old=0 THEN
       mode = mode + 1
       'GOSUB Get_ping
       ENDIF
    Btn2old=Btn2new[/FONT]
    

    You see, what that does is to cause mode to flip only when the button input makes the transition from 0 to 1. So you have to release the button and press it again to flip the mode back.
  • graffixgraffix Posts: 389
    edited 2012-11-09 20:47
    That worked great, Thank You Tracy.
Sign In or Register to comment.