Page 1 of 1

switch debounce

Posted: Fri Jul 17, 2009 2:05 pm
by jadiflow
Just a confirmation please: the properties setting for a switch component has an entry for debounce time.
It looks that this is only used in the sim but that it does not generate any code so for actual hardware I need to take care of the debounce in software.
Correct?

Thanks,

Jan Didden

Re: switch debounce

Posted: Sun Jul 19, 2009 12:05 pm
by medelec35
If just adding a input and switch icons and setting debounce delay in switch properties will not work. There will not be any software delay generated.

If you want to setup a switch debounce delay then you must use waituntilhigh or waitUntilLow within the macro for switches. Then if you set debounce delay in switch properties, there will be correct software delay generated.

Take a look here:
http://www.matrixmultimedia.com/mmforum ... unce#p1653

Re: switch debounce

Posted: Sun Jul 19, 2009 2:55 pm
by jadiflow
Thanks for the reference Medelec.

In my app I don't use interrupts, but poll the switches (pushbutton and rotary encoder) in an endless loop. Currently, when I detect a change of state on any switch, I wait for a debounce time (typically 5mS) and then look if the change is still there, hoping to have skipped the initial period where the contact bounces around. That does work, no missed pulses from the encoder even when turning very fast.
OTOH if there is a provision in the component macro it would be more elegant to use that one.
Is there any documentation on those WaitUntillLow or WaitUntillHigh functions you mentioned?

Edit: I just 'discovered' the Switch component macro! Until now I just used an input statement to read the switch state and process it.
I feel slightly stupid right now...

Jan Didden

Re: switch debounce

Posted: Sun Jul 19, 2009 3:58 pm
by medelec35
Your welcome.
The only problem with WaitUntilHigh and WaitUntilLow, is program is halted at switch macro until condition is met. So you would miss the pulses. If you want to do to other functions whilst at same time waiting for key press, then you will need to use the old trusted method of: Has port changed? If yes, delay for a short time. has port still changed?, if yes do port change routine. Which is the way you have been doing it.

Re: switch debounce

Posted: Sun Jul 19, 2009 4:24 pm
by jadiflow
Yes. Thanks!

Jan Didden

Re: switch debounce

Posted: Sun Jul 19, 2009 9:03 pm
by medelec35
If you never want to miss a pulse (especially when your program grows), then I believe capture by interrupts is the best way to go. Depending on chip used there are several options. GP2 and RB port change. I have used a flowchart to detect taco from two motors using masked port B interrupts and calculate both motor RPM precisely using Timer0 for 1 second intervals RPM for both motors is number of times both port B's interrupted for the second, then x by 60. even with both motors going at over 3000 RPM the calulated RPM is 100% accurate.
I have also created a flowchart to detect present of tacos form both motors without using interrupts. Code is not halted for long waiting for i/p's to change. sooner he change sooner code continues. Code also continues if i/p does not change at all.

Another code I have done is react to change only (non interrupt method) . So if switches or pots stay the same, then code is just continually looping. However if any of the 7 switches or 3 pots change in value then recaluacultions and different actions take place.
If you are interested in any of the code, let me know and I will post flowchart of code or just help if I can.

Re: switch debounce

Posted: Mon Jul 20, 2009 7:20 am
by jadiflow
medelec35 wrote:[snip]Another code I have done is react to change only (non interrupt method) . So if switches or pots stay the same, then code is just continually looping. However if any of the 7 switches or 3 pots change in value then recaluacultions and different actions take place.
If you are interested in any of the code, let me know and I will post flowchart of code or just help if I can.
Yes, that's how I do it atm. Thanks for your help; right now it's working fine and I'm happy with it.

Jan Didden