switch debounce

Forum for problems or queries regarding other Flowcode Components. Eg LEDs, Switches, LCD, Gfx LCD etc

Moderators: Benj, Mods

jadiflow
Flowcode v5 User
Posts: 249
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 16 times
Been thanked: 16 times
Contact:

switch debounce

Postby jadiflow » Fri Jul 17, 2009 2:05 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8236
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2359 times
Been thanked: 3347 times
Contact:

Re: switch debounce

Postby medelec35 » Sun Jul 19, 2009 12:05 pm

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:
viewtopic.php?f=5&t=752&p=1653&hilit=debounce#p1653
Last edited by medelec35 on Sun Jul 19, 2009 4:01 pm, edited 1 time in total.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

jadiflow
Flowcode v5 User
Posts: 249
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 16 times
Been thanked: 16 times
Contact:

Re: switch debounce

Postby jadiflow » Sun Jul 19, 2009 2:55 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8236
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2359 times
Been thanked: 3347 times
Contact:

Re: switch debounce

Postby medelec35 » Sun Jul 19, 2009 3:58 pm

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.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

jadiflow
Flowcode v5 User
Posts: 249
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 16 times
Been thanked: 16 times
Contact:

Re: switch debounce

Postby jadiflow » Sun Jul 19, 2009 4:24 pm

Yes. Thanks!

Jan Didden

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8236
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2359 times
Been thanked: 3347 times
Contact:

Re: switch debounce

Postby medelec35 » Sun Jul 19, 2009 9:03 pm

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.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

jadiflow
Flowcode v5 User
Posts: 249
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 16 times
Been thanked: 16 times
Contact:

Re: switch debounce

Postby jadiflow » Mon Jul 20, 2009 7:20 am

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