Shop OBEX P1 Docs P2 Docs Learn Events
switch inputs — Parallax Forums

switch inputs

Garced WinderGarced Winder Posts: 4
edited 2007-11-12 20:02 in Propeller 1
I recently bought a Propeller kit, just "learning the ropes" if you will. Completed a few of the sample programs and am starting to grasp the unit and its capabilities. I can't find is how to "one shot" a pushbutton input. What I'd like is to increment Index by 1 for every button event (off-on-off) reguardless of on time - this increments Index as long as P18 is held high.
repeat
    ! outa          'led on P3
    
    if ina[noparse][[/noparse]18] == 1  'n/o momentary on P18
      Index++

    waitcnt(clkfreq / Index + cnt)            



What I'd like is to increment Index by 1 for every button event (off-on-off) reguardless of on time - this increments Index by 1 as long as P18 is held high. Is there a one-shot routine or does that need to be coded? Or status register for i/o pins that can be checked then cleared?

I looked at keyboard.spin, got lost in the assembly. My assembly skills are quite rusty (8088 wayyy back in school)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-09 06:38
    The trick is to also wait for the "button off" event like:
    index~ ' index is initially zero
    repeat until ina [noparse][[/noparse] 18 ] == 0 ' make sure button initially off
    repeat
       repeat until ina[noparse][[/noparse] 18 ] == 1 ' wait for on
       index++
       repeat until ina[noparse][[/noparse] 18 ] == 0 ' wait for off
       waitcnt(clkfreq/index + cnt)
    
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-09 06:38
    You shall also need some debouncing considerations
  • Nick MuellerNick Mueller Posts: 815
    edited 2007-11-09 06:51
    The keyword is "edge triggering". That means you don't wait for L or H, but you wait for a transition from L to H (or H to L).
    Debouncing is an issue!


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • Paul MPaul M Posts: 95
    edited 2007-11-09 10:48
    Since you are polling the input, to de-bounce the switch, all you need to do is introduce a small delay after the 'wait for on'·and 'wait for off' statements
    mS:=clkfreq/1000 
    delay:=10*mS                   'delay in milliseconds 
    index~                         ' index is initially zero
    repeat until ina [noparse][[/noparse] 18 ] == 0   ' make sure button initially off
    repeat
       repeat until ina[noparse][[/noparse] 18 ] == 1 ' wait for on
       waitcnt(delay + cnt)
       index++
       repeat until ina[noparse][[/noparse] 18 ] == 0 ' wait for off
       waitcnt(delay + cnt)
    
    

    ·You may need to experiment with the delay depending on the type of switch. The click action switch I tested with required only about 2 mS but you may require up to 20 or 30 mS. Since the push button is likely to be·operated by a·human the delay will not be·noticed.

    BTW what was the purpose of the waitcnt(clkfreq/index·+·cnt) line?

    Paul
  • Nick MuellerNick Mueller Posts: 815
    edited 2007-11-09 11:03
    Sorry for nitpicking!

    milli seconds is "ms".
    "mS" would be milli Siemens. And that is the inverse of resistance (G = 1/R) AKA conductance.
    <http://en.wikipedia.org/wiki/Siemens_(unit)&gt;


    Nick, SI-evangelist smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • hippyhippy Posts: 1,981
    edited 2007-11-09 13:55
    @ Nick : Thanks for that, I never realised I'd been making that error. The other common SI mistake is to use KHz when it should be kHz.
  • Paul MPaul M Posts: 95
    edited 2007-11-09 13:55
    Nick

    Sorry to be pedantic, but 'mS' is the name of a variable·:-)
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-09 19:07
    (a) the SI rules state that ALL units should be wriiten in lower case except if they derive from a person's name.

    This was quite a clever move, as it COULD lead to become a little bit more interested in history. (Who was Volt? Oh it was Volta, Alessandro! And what did he do? Oh dear! Not only a frog eater but a frog tormentor... Sounds like a role model for Dr. Frankenstein....)

    Of course there ar many more famous scientists than units... So with Werner Siemens who later became "von Siemens", who was not only a great scientist but also a great industrialist.
    Thanks God they stopped then with inverting the units, but - maybe maybe - there is still some for you! I always wondered what a "Silva" could be...


    (b) "Bouncing" happens only when closing a switch, no need to debounce when opening it.

    Post Edited (deSilva) : 11/9/2007 7:13:17 PM GMT
  • Garced WinderGarced Winder Posts: 4
    edited 2007-11-09 22:27
    Wow, thanks for the quick replies! Thought about it at work and came up with this.
      dira~~        ' Set pin 3 to output
      Index := 1
      P18Flag := 0     ' Set pin 18 flag open
      
      repeat                   'Endless loop
    
        if ina[noparse][[/noparse]18] == 0        'P18 open 
          P18Flag := 0         'then clear flag   
    
        ! outa             ' Change the state of pin 3 
    
        if (ina[noparse][[/noparse]18] == 1 AND P18Flag == 0)  ' If button pressed and not still down from last cycle
          Index++
          P18Flag :=1                       ' set P18 flag for next cycle
          
        waitcnt(clkfreq / Index + cnt)             ' Wait 1/4 second -> 2 Hz
    
    



    Can hold switch down as long as you want, only increments by 1.
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-09 23:18
    This will work as debouncing, but you have a very bad response time when pressing the key.
    You can improve it by putting the WAITCNT into the IF.

    Note that it will be expected that the kay can be pressed as fast as possible, which is about 10 per second.

    What is the meaning of CLKFREG / Index ???


    BTW: always insert a space after a [noparse][[/noparse]
  • Paul MPaul M Posts: 95
    edited 2007-11-12 10:51
    deSilva said...
    (b) "Bouncing" happens only when closing a switch, no need to debounce when opening it.<!-- Edit -->
    Not necessarily true. See:

    http://www.ganssle.com/debouncing.pdf
    http://www.elexp.com/t_bounc.htm
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-11-12 11:10
    It's hard to see why

    Graham
  • parts-man73parts-man73 Posts: 830
    edited 2007-11-12 14:22
    There's been many threads on input from a switch, and more specifically debouncing.

    Here's a link to a recent thread that I think covers it rather well
    http://forums.parallax.com/showthread.php?p=677248

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Brian

    uController.com - home of SpinStudio

    PropNIC - Add ethernet ability to your Propeller!

    SD card Adapter
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-12 20:02
    "Bouncing" wrt opening switches...
    I always liked the thorough invstigation of Jack Ganssle, I generally give this reference myself, but I definitely had forgotten the part about opening switches..

    The effect itself - as he well describes -is a little bit unclear. It is NOT the mechanical effect when closing a switch... Closing a switch resembles dropping a ball; opening the switch resembles picking it up...

    There is always a small inductance involved and local electrical fields at sharp corners maybe can trick a digital circuit into intermediate triggering. Note the time constant for this effect when opening a switch is very different from closing.

    SPIN programmers will not need to consider "unclean" effects when closing switches.

    The second reference Paul M gave does not sound very reliable wrt to opening switch effects...

    But it will do no harm to consider it. It absolutely depends on how short you start looking again for the switch after you noticed it's closing.

    I have never used software debouncing for opening switches...
Sign In or Register to comment.