Shop OBEX P1 Docs P2 Docs Learn Events
why isn't this button command working? — Parallax Forums

why isn't this button command working?

brettmbrettm Posts: 36
edited 2006-06-12 15:28 in BASIC Stamp
It seems to me this code should blink LED2 until the button at P0 is pressed, then it should blink LED5

' {$STAMP BS1}
' {$PBASIC 1.0}

Main:
'--------------------Blink 2 if not pressed--------------------------------------------'
HIGH 2
PAUSE 1000
LOW 2
PAUSE 1000
'--------------------Check Button------------------------------------------------------'
BUTTON 0, 1, 255, 1, B1, 0, Nothing
GOTO BlinkLED5 'NOTE: This happens only if the Button is pressed.....
Nothing:
GOTO Main:

BlinkLED5:
HIGH 5
PAUSE 1000
LOW 5
PAUSE 1000
GOTO BlinkLED5



However, when I run it, it blinks LED2 twice, then LED5 indefinitely. That's with no button input. Pressing the button changes nothing. The only thing I can get the button to do is if I press it after the first LED2 blink it goes immediately to the LED5 blinks.

What am I not understanding about the button command?

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-10 21:06
    Well, for one thing, as you have it listed, once it goes to BlinkLED5 it will stay there because that is what you tell it to do having GOTO BlinkLED5 BlinkLED5:
    HIGH·5
    PAUSE·1000
    LOW·5
    PAUSE·1000
    GOTO·BlinkLED5
    Change it to GOTO Main
  • brettmbrettm Posts: 36
    edited 2006-06-10 21:18
    I understand that, and it is intentional. But why is it going to BlinkLED5 without pressing the button? Without button input, why is it not looping the BlinkLED2?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-10 21:23
    How is your button hooked up?
  • brettmbrettm Posts: 36
    edited 2006-06-10 21:28
    I rewrote it a little different, where the button command is looking for the button to be pressed rather than not pressed (changed TargetState to 1). It still goes to blinking LED5 without any button input.

    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    
    Main:
    '--------------------Blink 2 if not pressed--------------------------------------------'
    HIGH 2
    PAUSE 1000
    LOW 2
    PAUSE 1000
    '--------------------Check Button------------------------------------------------------'
    BUTTON 0, 1, 255, 1, B1, [color=#009900]1[/color], BlinkLED5
    GOTO Main:
    
    BlinkLED5:
    HIGH 5
    PAUSE 1000
    LOW 5
    PAUSE 1000
    GOTO BlinkLED5
    
  • brettmbrettm Posts: 36
    edited 2006-06-10 21:30
    PJ Allen said...
    How is your button hooked up?

    from Vdd to a momentary button to P0
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-10 21:34
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    
     
    [color=red]B1 = 0[/color]
    
    Main:
    '--------------------Blink 2 if not pressed---
    HIGH 2
    PAUSE 1000
    LOW 2
    PAUSE 1000
    '--------------------Check Button--
    BUTTON 0, 1, 255, 1, B1, 1, BlinkLED5
    GOTO Main
    
    BlinkLED5:
    HIGH 5
    PAUSE 1000
    LOW 5
    PAUSE 1000
    GOTO BlinkLED5
    
    
  • brettmbrettm Posts: 36
    edited 2006-06-10 21:42
    nope, same thing. that just clears the RAM, right?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-10 21:42
    brettm said...
    from Vdd to a momentary button to P0
    You ought to have a 10K resistor from P0 to Ground, too.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-10 21:45
    The Book says: "Bytevariable is the workspace for Button. It must be cleared to a 0 before being used by Button for the first time."
    Feels Like the First Time.
  • brettmbrettm Posts: 36
    edited 2006-06-10 21:46
    PJ Allen said...
    brettm said...

    from Vdd to a momentary button to P0
    You ought to have a 10K resistor from P0 to Ground, too.

    okay, good to know, but would it affect this?

    I'm not even pressing the button and it won't loop to main
  • brettmbrettm Posts: 36
    edited 2006-06-10 21:50
    PJ Allen said...
    The Book says: "Bytevariable is the workspace for Button. It must be cleared to a 0 before being used by Button for the first time."
    Feels Like the First Time.
    It also says that it's automatically cleared on powerup, so I didn't worry about it.· Regardless, nothing changes when I run the program with the B1=0 in there.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-10 21:50
    Sometimes BUTTON stinks.
    How does this work?
    Also, please place the aforementioned resistor, too.
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    
    INPUT 0
     
    Main:
      '---Blink 2 IF not pressed---
      HIGH 2
      PAUSE 1000
      LOW 2
      PAUSE 1000
      '---Check Button--
      IF PIN7 = 0 THEN Main
    
    BlinkLED5:
      HIGH 5
      PAUSE 1000
      LOW 5
      PAUSE 1000
      GOTO BlinkLED5
    
  • brettmbrettm Posts: 36
    edited 2006-06-10 21:56
    "Sometimes BUTTON stinks" doesn't seem like a very satisfactory answer. It exists for a reason, right? I figured this is about the most basic code I could try with it and yet I still can't get it to work. I would really like to use Button, mainly for the debounce characteristics.

    I just tried that new code and it does the same thing, but I didn't add that resistor. I just got this thing and don't have any resistors lying around. I'll go swing by RadioShack and report back later this evening. Maybe that's all there is to it...
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-10 21:57
    I don't like either of the circuits shown in the book (BASIC Stamp starter kit V.1).· Attached, please see a schematic of what is, in my opinion, THE BEST, switch circuit.


    Post Edited (PJ Allen) : 6/10/2006 10:01:35 PM GMT
    201 x 375 - 13K
  • bennettdanbennettdan Posts: 614
    edited 2006-06-10 22:32
    Hey try this code to see if this is what you need.

    ' Led Test .BAS

    ' {$STAMP BS1}
    ' {$PBASIC 1.0}




    '
    [noparse][[/noparse] Declarations ]

    SYMBOL btnWrk = B2 ' workspace for BUTTON


    '
    [noparse][[/noparse] Main Routine ]

    Main:
    BUTTON 0, 0, 255, 250, btnWrk, 0, Blink_LED5 ' Goto Blink_LED5 if P0 = 1
    HIGH 2
    PAUSE 1000
    LOW 2
    PAUSE 1000

    Blink_LED5:
    HIGH 5
    PAUSE 1000
    LOW 5
    PAUSE 1000
    GOTO Main ' repeat endlessly
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-10 23:25
    [noparse][[/noparse] SYMBOL...· Whenever I'm working with BS1 I use the book that I have and SYMBOL isn't listed in the 'PBASIC Language' section -- and I almost always miss it. ]
    Good thinking, bennettdan.
  • brettmbrettm Posts: 36
    edited 2006-06-11 06:26
    it must have been the lack of resistors! a 1k and 10k added like your diagram and it works!

    thank you!· i figured it would be something dumb and simple like that...

    Post Edited (brettm) : 6/11/2006 6:32:12 AM GMT
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-11 12:28
    Hey, guy -- you better smile when you say that!· smile.gif··

    Resistors are NOT dumb, but they are taken for granted.·

    Which reminds of when The Brain, The Eyes, and The Ears got into an argument, each figuring he was "in charge", going on strike in turn.· After each had his turn, with inconclusive results, The Seat announced that he'd show them all: when he stopped then The Brain couldn't think, The Eyes couldn't see, and The Ears couldn't hear.
  • brettmbrettm Posts: 36
    edited 2006-06-11 13:25
    I wasn't calling resistors dumb, I was calling myself dumb [noparse];)[/noparse]

    thanks again! [noparse]:D[/noparse]

    [noparse]:D[/noparse]


    [noparse]:D[/noparse]
  • Tom WalkerTom Walker Posts: 509
    edited 2006-06-12 15:28
    brettm,
    Just as a brief explanation, without the resistor pulling the line into a "known" state (tied to Vcc or Vss) then the input pin on the Stamp "floats" and acts as an antenna...picking up whatever stray signals might be floating around near your rig, causing your Stamp to see either a high or a low, depending on the strength of the "air signal".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...
Sign In or Register to comment.