Shop OBEX P1 Docs P2 Docs Learn Events
Variable Questions — Parallax Forums

Variable Questions

railroad signalrailroad signal Posts: 5
edited 2013-01-13 14:00 in BASIC Stamp
Hello everybody.
I am trying to understand variables. What I am trying to accomplish.When I momentarily push a button I would like to register that the push buttonhas or has not been pressed in the DEBUG HOME. I am having problems. Cannot getthe input to communicate with the variable. What is your recommendations. Thank you.

' {$STAMP BS2}' {$PBASIC 2.5}
hold VAR Bit
DO
DEBUG HOME
DEBUG ? IN0
DEBUG ? hold
INPUT 0
LOOP

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2013-01-13 13:56
    How do you have the button hooked up to pin p0?

    The program looks okay. The variable "hold" doesn't do anything. A simpler test program is,

    DO
    DEBUG in0+48 ' prints 0 or 1
    PAUSE 100
    LOOP

    That simply shows a continuous line, 00000011111100000011...
  • SapphireSapphire Posts: 496
    edited 2013-01-13 14:00
    If your input is normally LOW and goes HIGH when you press the button, use this:
    [SIZE=2][FONT=courier new][COLOR=#000000]' {$STAMP BS2}
    ' {$PBASIC 2.5}[/COLOR]
    [COLOR=#000000]
    hold VAR Bit
    hold = 0
    
    INPUT 0[/COLOR]
    [COLOR=#000000]
    DO[/COLOR]
    [COLOR=#000000]  hold = hold | IN0
      DEBUG HOME
    [/COLOR][COLOR=#000000]  DEBUG ? IN0
    [/COLOR][COLOR=#000000]  DEBUG ? hold
    [/COLOR][COLOR=#000000]LOOP[/COLOR]
    [/FONT][/SIZE]
    

    But if your input is normally HIGH and goes LOW when you press the button, then use this:
    [FONT=courier new][SIZE=2][COLOR=#000000]' {$STAMP BS2}
    ' {$PBASIC 2.5}[/COLOR]
    [COLOR=#000000]
    hold VAR Bit
    hold = 1
    
    INPUT 0[/COLOR]
    [COLOR=#000000]
    DO
      hold = hold & IN0
    [/COLOR][COLOR=#000000]  DEBUG HOME
    [/COLOR][COLOR=#000000]  DEBUG ? IN0
    [/COLOR][COLOR=#000000]  DEBUG ? hold
    [/COLOR][COLOR=#000000]LOOP[/COLOR]
    [/SIZE][/FONT]
    

    Once you press the button, hold will retain the state. You will need to clear/reset hold later in your program to capture another button press.
Sign In or Register to comment.