Shop OBEX P1 Docs P2 Docs Learn Events
Octal Button Debouncer — Parallax Forums

Octal Button Debouncer

Dan EDan E Posts: 61
edited 2011-01-25 18:23 in Propeller 1
Hi,
I was looking for a debounce object in the object exchange, and I saw the Octal button debouncer. I assume from reading through the D8C_BUTEngine that it can handle up to 8 debounce buttons.

I am looking to have six different push buttons in my project, and I wanted each of them when pressed to interrupt the main program to perform their respective function.

Can this program able to do these tasks at any point in time during a main program when pressed?

Thanks,
Dan

Comments

  • KyeKye Posts: 2,200
    edited 2011-01-24 20:39
    The object just tells you if a button was pressed, the state of the button, and if it was released... If does not execute any special code for you.
  • Dan EDan E Posts: 61
    edited 2011-01-24 22:06
    Hi, I attached a sample code that shows what I am trying to do, but i know these commands may not actually do what I intended.

    sample debounce code.jpg



    Thanks,
    Dan
    1024 x 672 - 45K
  • KyeKye Posts: 2,200
    edited 2011-01-25 06:58
    Okay, thats the idea. Read up on what the functions return and take however inside of the button debouncer object. You are not calling them properly there.
  • Dan EDan E Posts: 61
    edited 2011-01-25 08:38
    Okay, I did some updating and reading, is it 'result' that I need to check to be true, i'm not sure how to check each button specifically and if 'result' = true. Maybe do I need to call the function first for each button, then check if 'result' = true?
    debounce test 3.PNG


    Here is what I actually tried:
    debounce test 2.jpg


    And here is the full code, I added sound to test if the buttons were working or not, using the setup I already had.

    debounce test - Archive [Date 2011.01.25 Time 11.20].zip

    Thanks,
    Dan
  • JonnyMacJonnyMac Posts: 8,987
    edited 2011-01-25 11:17
    There are times when a debouncing cog is useful but I don't think it's beneficial here -- you can create a simple debouncing method that handles your pins. In this case you can pass the # of [contiguous] pins, the base pin of the group, and the debounce timing in milliseconds.

    Simpler is often better.
    pub main | btns
    
      repeat
        btns := scan(6, 12, 50)                                     ' scan buttons
        case btns
          %00_0001:                                                 ' base_pin pressed
            playwav(string("music1.wav")
         
          %00_0010:                                                 ' base_pin + 1pressed
            playwav(string("music2.wav")
         
          %00_0100:                      
            playwav(string("music3.wav")
         
          %00_1000:                      
            playwav(string("music4.wav")
         
          %01_0000:                      
            playwav(string("music5.wav")
         
          %10_0000:                       
            playwav(string("music6.wav")
     
    
    pub scan(pins, base, ms) : debounce | t
    
    '' Active-high debounce
    '' -- pins is # of contiguous pins to debounce
    '' -- base is the lsb pin of the group
    '' -- ms is the debouce timing in milliseconds
    
       debounce := (-1 >> (32 - pins))                              ' assume active
    
       t := cnt                                                     ' sync for timing
       repeat ms                                                    
         waitcnt(t += clkfreq/1000)                                 ' wait 1ms
         debounce &= (ina >> base)                                  ' scan inputs
    
  • pgbpsupgbpsu Posts: 460
    edited 2011-01-25 12:18
    JonnyMac-

    I always love your posts because they are simple, straight forward, clever, and exactly what's needed. This one is no exception, although it leaves me wondering what happens if debounce returns with more than one bit high. Is that even possible if someone were to hold 2 buttons for more than 50ms?

    Thanks,
    Peter
  • JonnyMacJonnyMac Posts: 8,987
    edited 2011-01-25 14:19
    Peter,

    Thanks for the kind words. You'll note that in Main a case structure is used; you could in fact use this to detect multiple inputs (I used binary to make this easier to visualize). If the return value falls through the case structure without a match then nothing happens. If you want a default action for no match you can use the other option with case.

    There is an advantage to returning multiple pins: you could use one as a "shift" key and get 10 sections (five un-shifted, five shifted) from your six inputs.

    Keep in mind that the debounce method works by checking which pin(s) stayed pressed during the debounce period, that is, they didn't bounce. In your practical application you may want to allow the audio to finish before rescanning or, at the very least, force a release of all buttons (return of %000000) before going back to the top of your scan loop.
  • pgbpsupgbpsu Posts: 460
    edited 2011-01-25 18:23
    JonnyMac-

    Thanks for answering my question. I'd forgotten that case has a default case. I like your idea if a shift button and with the binary representation for the individual cases, that's a very easy thing to visualize (and code). I'll have to remember this post next time I need button debounce.

    Regards,
    Peter
Sign In or Register to comment.