Momentary pushbutton debounce help

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Drakkor
Posts: 41
Joined: Sun Dec 30, 2012 9:34 pm
Has thanked: 5 times
Been thanked: 4 times
Contact:

Momentary pushbutton debounce help

Post 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 : )
Attachments
TractorLight_02.fcf
(20.25 KiB) Downloaded 278 times

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Has thanked: 72 times
Been thanked: 177 times
Contact:

Re: Momentary pushbutton debounce help

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

Drakkor
Posts: 41
Joined: Sun Dec 30, 2012 9:34 pm
Has thanked: 5 times
Been thanked: 4 times
Contact:

Re: Momentary pushbutton debounce help

Post 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 : )

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Momentary pushbutton debounce help

Post 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
Attachments
TractorLight_03.fcf
(20.23 KiB) Downloaded 281 times
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Momentary pushbutton debounce help

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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Momentary pushbutton debounce help

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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Momentary pushbutton debounce help

Post 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
Attachments
TractorLight_03a.fcf
(20.23 KiB) Downloaded 292 times
Martin

Post Reply