Shop OBEX P1 Docs P2 Docs Learn Events
Button command Help! — Parallax Forums

Button command Help!

MagicalmanMagicalman Posts: 3
edited 2011-02-15 09:21 in Propeller 1
Hello I am very, very new to the Propeller and apologize for such a basic question however i could sure use some direction, I have used pbasic for a number of years but just cant seem to figure this out in Spin

All i would like to do is be able to alternately turn on and off say 8 Led's on p1 through p9 each time i press a button on p0, it seems simple?


I am able to turn the leds on and off using the outa command but just cant wrap my head around how the button loop works in Spin. once again i apologize for the trivial question but could really use some advice !

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-02-14 12:51
    Think of the BUTTON statement as a specialized form of GOTO that goes one way if the button is pressed and the other way if it's not. Internally, it debounces the button and optionally handles repeated button presses if the button is held down, but you don't see the details of that. If you want to use alternate button presses to turn something on and off, you'll need two BUTTON loops, one to wait for the even time the button is pressed and the other for the odd time for the button to be pressed. When it "falls through" the first BUTTON loop, your program turns something on. When it "falls through" the second BUTTON loop, your program turns something off. Eventually your program does a GOTO to the beginning and it all starts over again.
  • MagicalmanMagicalman Posts: 3
    edited 2011-02-14 13:10
    Thank you very much!! i do kind of get that !! i guess what i need to know is do i keep the button statement in this case "ina[0]" in a "repeat" loop or do i just add it prior to each outa statement !
  • AribaAriba Posts: 2,690
    edited 2011-02-14 14:12
    Here is an untested Spin code fragment that should show you how a button press can be detected.
    Depending of how you have connected your button you may need to swap the states for pressed (1) and released (0).
    dira[8..1] := %11111111
    select := 1
    repeat
      outa[8..1] := select
    
      repeat until ina[0] == 1    'Wait until button pressed
      waitcnt(clkfreq/50 + cnt)   'debounce
      repeat until ina[0] == 0    'wait until button released
      waitcnt(clkfreq/50 + cnt)   'debounce
     
      select := select << 1
      if select > 1<<8
        select := 1
    
    To make a running code out of that, you need to add a PUB and declare the select variable (as long).

    Andy
  • MagicalmanMagicalman Posts: 3
    edited 2011-02-14 14:56
    Thank you very much Andy!! thats exactly what i was looking for !!! it's all starting to make some sense now Cheers
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-15 08:50
    Thanks for for sharing this code
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-02-15 09:00
    Here's a bit of code that I use for debouncing a single input; the purpose of this code is to wait until an input has gone high and stays high for the specified duration; contact "bouncing" is filtered out.

    This code is currently deployed in the Buzz Lightyear hand-stamper that will start showing up in Disney parks.
    pub wait_press(pin, ms) | debounce, t
    
    '' waits for (active-high) button press of ms milliseconds
    
      dira[pin] := 0                                                ' set pin to input mode
    
      debounce := 0                                                 ' clear debounce timer
    
      t := cnt                                                      ' sync with system cnt
      repeat until (debounce == ms)                                 ' wait specified db time
        waitcnt(t += clkfreq/1_000)                                 ' hold 1ms
        debounce := ++debounce * ina[pin]                           ' scan input
    

    Again, this is for a single input and will not return until the condition is met. The fun is in the last line of the loop: the debounce timing is incremented and then multiplied by the state of the pin. If the pin bounces open (0) then the timer is reset.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-15 09:21
    Jon

    Thanks for share this
Sign In or Register to comment.