Shop OBEX P1 Docs P2 Docs Learn Events
Newb push-button question — Parallax Forums

Newb push-button question

willdabeastwilldabeast Posts: 6
edited 2009-04-08 10:57 in BASIC Stamp
Hi all. I'm trying to get a push button to cycle thru 4 choices. I will make the sub-routines later, but for now I'm just trying to get the button to work. I've tried using the button command, but it was making my head hurt.
Here's the code I have:

i VAR Byte
btn PIN 2

Main:

DO
INPUT btn

IF btn = 1 THEN i=i+1
SELECT i
CASE i=1
DEBUG "one"
CASE i=2
DEBUG "two"
CASE i=3
DEBUG "three"
CASE i=4
DEBUG "four"
ENDSELECT
IF btn = 0 THEN i=i
IF i=5 THEN i=1
LOOP

I have it inside a DO/LOOP because I need it to run for a long time.
I know my physical assembly of the cct is working, so something ain't right with the code.
Thanks,
-Will

Comments

  • GeorgeLGeorgeL Posts: 131
    edited 2009-04-07 05:27
    Check that the pin is trully pin 2. On the boe-bot board and its variants, the first pin is PIN 0
  • GeorgeLGeorgeL Posts: 131
    edited 2009-04-07 05:28
    Also, do you need this "IF btn = 0 THEN i=i"
  • willdabeastwilldabeast Posts: 6
    edited 2009-04-07 17:36
    I have it hooked up to pin 2 because 0 and 1 are otherwise occupied. As for that last line...now that I look at it I think it is pointless. I deleted it, but the button still doesn't work.
    -Will
  • FranklinFranklin Posts: 4,747
    edited 2009-04-07 18:30
    Try if i > 4 then i = 1

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • willdabeastwilldabeast Posts: 6
    edited 2009-04-07 18:48
    Isn't "if i>4 then i=1"
    the same as "if i=5 then i=1"

    They both check to see if i = 5.
    If it does, it sets i to 1
    If it doesn't it lets i stay the same.

    -Will
  • FranklinFranklin Posts: 4,747
    edited 2009-04-07 18:57
    No they are not the same. 5 is a finite number so if i = 6 it will fail. but your button may (will) bounce and you may have a number of any size in i. If you still think they are the same I'll give you $5 for $1000 (>$4)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-04-07 19:07
    check syntax of SELECT .
    Your code is assigning values to variable i instead of evaluating it.
    It should reed

    SELECT i
    · CASE 1
    ···· DEBUG "One"
    .....


    IF btn = 1 THEN i=i+1
    SELECT i
    CASE i=1
    DEBUG "one"
    CASE i=2
    DEBUG "two"
    CASE i=3
    DEBUG "three"
    CASE i=4
    DEBUG "four"
    ENDSELECT
    IF btn = 0 THEN i=i
    IF i=5 THEN i=1
    LOOP
  • willdabeastwilldabeast Posts: 6
    edited 2009-04-07 19:13
    ahhhh bouncing.
    That does make since. I overlooked the little fact it may skip over 5.
    I think I'll add a latch to debounce the button.
    Thanks.
    -Will
  • willdabeastwilldabeast Posts: 6
    edited 2009-04-07 19:49
    OK. I made the previously mentioned change to the code; however I'm still getting nothing on the debug screen when I push the button.
    Any ideas?
    -Will
  • FranklinFranklin Posts: 4,747
    edited 2009-04-07 20:00
    did you make the change vaclav_sal mentioned?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • willdabeastwilldabeast Posts: 6
    edited 2009-04-08 02:11
    That is a bit odd. I must have had this reply window open while vaclav_sal posted his reply.
    Anyway. Thank you both for the input. The button works although it repeats endlessly. I think I know where the problem is.
    Thanks again.
    -Will
  • MrBi11MrBi11 Posts: 117
    edited 2009-04-08 10:57
    you need to make it trigger on only the leading edge of the button press
    [noparse][[/noparse]single shot controlled by flg variable]

    
    do
    if in2=1 then
     if flg=0 then
      btn=btn+1
      if btn>4 then btn=1
      select case btn
         case 1
             ....
         case 4
      endsel
      flg=1
     endif
    else
     flg=0
    endif
    loop
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Smile ... It increases your face value!
Sign In or Register to comment.