Button Inputs Occassionally Not Detected

elec_mech

New Member
Hello All,

I've finally gotten my first PICAXE program to function the way I'd like. The only problem at the moment is I'm using the button command to check the status of four buttons and while this works most of the time, once in a while the button press is not detected and must be pressed again (in order to advance a countdown display).

I was wondering if there is a way to increase reliability.

Some thoughts I had were:

- Elimination the button command altogether and perhaps adding a 20ms pause to allow for debounce

- Adding something external to eliminate debounce such as a capacitor and resistor and scanning each input

- Somehow reduce the total number of lines of code/processes to increase button checking speed

I just wanted to check and see if anyone had similar problems using the button command and how they overcame them. Perhaps the problem is how I've written the code and not the button commands themselves. I've learned the PICAXE takes a lot longer to run than I'm used to and I've learned a lot over the past week and half.

Thank you in advance for any advice and assistance.
 

Attachments

John West

Senior Member
I don't have any experience with the "Button" command, but my guess is that the missed button is being pushed while the code is churning along elsewhere. If that's the case, there are several ways to make sure the button press doesn't get missed. Reorganizing your program loop to speed it up, or simply adding in button scans at more spots in the code so the buttons get checked more often may solve the problem.

I see we are both in the USA, while most of the forum members who are competent programmers (I'm not) are from other continents and are sleeping now. Check back in the morning if my suggestion doesn't work.
 

BeanieBots

Moderator
The 'button' command needs to be within a loop and when the code is doing anything other than the button command there is a risk of missing a very breif push.
To minimise the risk you can either do as already suggested and decrease any time spent elsewhere, add a capacitor across the button (which will effectively hold it down longer as well as offer debounce) or delve into the world of interrupts.
 

elec_mech

New Member
Thank you all. I've been playing with this for days. I was able to change the code so that only the button commands were looked at. It sort of helped. I then skipped the button commands altogether and just used IF statements to check the status of the input pins. MUCH faster. Button operation was a little iffy, so we added 0.1uF caps across the switch leads which improved things quite a bit. Now I'm adding pauses to delay the switch responses as a single press can advance the counter several places instead of just one. Thanks again!
 

Bill.b

Senior Member
The other alternative is to use an interupt to detect then buttons operation.

Refer to picaxe manual "getting started" page 84 on how to use interupts.

Bill
 

elec_mech

New Member
Thank you Bill. I'm already using an interrupt to detect a clock pulse from a DS1307, so I'm trying not to add to it, but I've done this before with PICs and it is a fallback plan in the worst-case scenario. Thank you for your advice, I will certainly keep it in mind.
 

geoff07

Senior Member
Perhaps the simplest method is this: With an interrupt you can wire each button to a separate input (with pull-ups). Then diode-multiplex them each to another input that can trigger interrupts (the diodes are to separate the signals so they don't all take the value of the one that is pressed). In the ISR, don't wait for the the interrupt to clear, just scan the buttons to see which one caused the interrupt, and set a variable to say which it was. Then wait for it to clear, reset, and thence back to the main program where you can deal with the variable at leisure. If your buttons are particularly bouncy use a 20mS pause in the ISR before you return.
 
Top