Page 1 of 1

Momentary pushbutton debounce help

Posted: Mon Feb 17, 2014 6:50 pm
by Drakkor
Hi, I know that my code could be improved but I am only interested in the debounce part right now. I made a project with a Pic12f675 for a flasher that will flash two tail lights on a tractor. The program has eight flash modes. It starts out with a count of ONE so it starts in a slow flash rate by default. The switch I have is a push button, momentary, non-snap action. Since I am using change on port, It seems I MUST read rise and fall edges? The switch seems to be working, but not 100% of the time as it will skip over some sequences (Increments Cnt1) sometimes. What I would like is a way to increment the count ONCE, no matter how long the button is held, and also ignore the falling edge when released. Originally, I just had a total count of 16.....if cnt1 =1 || cnt1 = 2....do this if cnt1 = 3 || cnt1 = 4 do this. Now, as the code will show, I use a total of 8 counts total if cnt1 = 1.............do this if cnt1 = 2.....do this. Any simple solutions/help would be awesome : )

Re: Momentary pushbutton debounce help

Posted: Tue Feb 18, 2014 11:06 am
by Kenrix2
That type of switch is always a challenge. For snap action switches I insert a delay of 200ms before it is checked again but, for your switch, I would try a quarter or third of a second. I think you will have to change to the the int pin to get rising or falling only for that particular chip.

Re: Momentary pushbutton debounce help

Posted: Tue Feb 18, 2014 12:44 pm
by Drakkor
Kenrix2, Thanks for the help. I am not sure if I can use the int pin on this chip? Could you show me a quick example? Thanks again : )

Re: Momentary pushbutton debounce help

Posted: Tue Feb 18, 2014 5:59 pm
by medelec35
Hi Drakkor,
Do you want to see if attached flowchart is any better?
I know you wanted to ignore falling edge, but I thought we could use just a single falling edge to trigger, and anything else will be ignored until cnt1 has changed.

I can change your flowchart to work on rising edge if you wish?

Martin

Re: Momentary pushbutton debounce help

Posted: Tue Feb 18, 2014 6:04 pm
by Benj
Hello,

For interest if you really want to simplify the debounce process then select the switch on the panel and go to the EXT properties.

In here you will find the debounce delay. Then calling one of the switch component macros will automatically include the amount of debounce specified.

The macro will only return when the input signal to the switch pin has settled for at least the amount of debounce time.

Re: Momentary pushbutton debounce help

Posted: Tue Feb 18, 2014 6:14 pm
by medelec35
Benj wrote:select the switch on the panel and go to the EXT properties.

In here you will find the debounce delay. Then calling one of the switch component macros will automatically include the amount of debounce specified.
I would normally agree with Ben on that one, but with several delays within main loop e.g. there is one when Cnt1 = 6 which comprises of 750ms + 750ms + 500ms +500ms
if the switch is briefly pressed at the wrong time, then if not using interrupt for switch detection then switch detection can get missed?
It's useful to know the switch functions with debounce are there of course

Re: Momentary pushbutton debounce help

Posted: Wed Feb 19, 2014 10:52 am
by medelec35
Hi Drakkor,
One thing I spotted in your flowchart is port interrupt placed in the wrong place.
it requires moving to a place before Loop within Main

I forgot to correct it before posting flowchart.

I have corrected it with attached Flowchart.

Martin