Hi,
I have currently in my possession an Arduino Leonardo, 25 on-(off) momentary 2 pin push buttons and 5 on-off toggle switches.
I have been looking online and have found some information, however I haven't found anything in relation to a working schematic for a 6x6 matrix.
I dont need to be able to push more than one button at a time, so assume diodes aren't a necessity, and I would be able to use the Leonardos internal pull up resistor?
Let me know if I need to provide any more information.
Will you be debouncing in hardware of software or not at all?
To read the matrix;
Set rows and columns pins to INPUT (neutral) to start. Then set 1 row pin as INPUT_PULLUP and 1 column pin as OUTPUT LOW and read the input. Set that column to INPUT (LOW) and the next column pin as OUTPUT LOW and read, etc for the row and then the next.
If you write non-blocking (fast) code then don't read the whole matrix every pass through loop but rather
void loop() // no blocking, no even slowing down, the reads can happen at a several 10's of times per millisecond.
{
// use the indexes rowIdx and colIdx to read only that one switch, including mode setting
// set the read value into switchState
if ( switchState == 0 ) // INPUT_PULLUP, this is button pressed/grounded
{
// use rowIdx and colIdx to match the button pressed to some action
// you may want to only do that on state change by saving the last state value and check for difference
}
Though it may not seem intuitive, debouncing this in software will not slow the loop() down if done right (no blocking!) though yes, a debounced switch takes longer to "count", at least it only counts 1 press and release for 1 actual press and release. How long it takes depends on the debounce code, wait 50ms code has to wait 50ms (for press only) which is faster than human button presses anyway even if it's not how I do it.
Debouncing in hardware means soldering a 1uF cap across the switch leads (the cap in parallel with the switch) to eat any bounces.
Next level: using the diodes or not, you should check for multiple presses "at once" (meaning over the next 200ms or more) before acting on any inputs or just accept that pressing more than one even by accident will produce errors in the sketch output.
Thanks for your response, will read and reread several times so it sticks....its all a little bit new to me.
I am embarrassed to ask but what does debouncing mean?
I will be using the button box to emulate keyboard presses so essentially button 1 will press 'W' once...does that make sense?
CharM14:
I am embarrassed to ask but what does debouncing mean?
You know how a basketball bounces when you drop it? Then after a bit it settles down and rests peacefully on the ground? Same principle. The point being, the processor is so fast it can see each bounce as a separate keypress. Debouncing is telling the processor to wait 'til the ball has been steady on the floor for X units of time before accepting the input.
dougp:
You know how a basketball bounces when you drop it? Then after a bit it settles down and rests peacefully on the ground? Same principle. The point being, the processor is so fast it can see each bounce as a separate keypress. Debouncing is telling the processor to wait 'til the ball has been steady on the floor for X units of time before accepting the input.
oh...erm....well I will be wanting to press a button several times in relatively quick succession to navigate through a menu....do you suggest I consider debouncing?
A very good tutorial on contact switches and Arduino: www.gammon.com.au/switches
The bouncing usually happens over about 2 milliseconds. Just before the contacts close (and just after they open) when the gap is microscope-view small there will be sparks jumping that gap that will look like super-fast press and release events.
No human is anywhere near fast enough to match that, not even 1% of that. One full second is 200 50ms intervals. Who can press and release a button that fast?
I still do something else myself and my button value wraps current state, previous state and bounce state into 1 number so the user doesn't have to keep a copy of previous state to compare to current state at all, just act or not on the single button value which can be 0 to 3 if not bouncing and > 3 when bouncing.
Thanks DougP and GoForSmoke for your assistance, I shall set about reading those tonight when I get in from work, appreciate the help. I know it can be frustrating when people know little and ask silly questions....so again thanks.
The bouncing usually happens over about 2 milliseconds.
That's somewhat optimistic, Nick only tested one switch type and bounce can vary widely by switch design. I have some 6mm square devices that don't appear to bounce at all and some 12mm's that can bounce for upwards of 30 milliseconds. You will not know for sure until you test the switches in question and if you cannot test, make your denounce time adjustable so that you address any variability.
A more general, non-Arduino specific in-depth research paper on switch debounce can be found here:
My test button is a jumper that I ground on the board USB port housing.
And Nick's blog isn't the only place I've read up on contact bouncing.
Yes there are switches with likely built-up corrosion or other faults that are very dirty. Some don't settle the metal cleanly, press forces that vary side to side will make them give false inputs that just BTW can be filtered out, my library does that too by only debouncing odd bounces since every even bounce goes back to the original before-press state, no change to report.
Maybe the reason why I wrote attention-intensive debounce code is because of things that can go wrong or maybe I just don't like the whole "wait a while and assume things went as planned" approach.
I don't think that I'd keep crap buttons around to bug up my projects. I debounce jumper taps with a 2ms to 5ms no-change period on an odd bounce and I expect that much from buttons since I can DIY that well.
I'm more into cap sense anyway. Also IR beam break, IR reflect, magnetic sensing and light sensing with leds.
Switches are so dirty OTOH but dirt cheap OTOH.
Ok so I need to do some reading up on debouncing.....but I have done a quick sketch, and it seems i only need 25 push buttons and 5 toggle switches....is the attached correct? I dont know where to connect the circuit to ground, or the +ve
You make a grid and one row pin is INPUT_PULLUP at a time and one column pin is OUTPUT LOW at a time.
The one row pin that is INPUT_PULLUP (at a time) provides weak current (good) and the one OUTPUT LOW (at a time) column pin provides ground. Only if that row & column button is pressed will the INPUT_PULLUP pin real LOW, otherwise it reads HIGH and that does not include bounce.
The circuit is powered by the input pin and grounded by the output pin.
The outline of code I gave above shows one way to move through all the row and column pin combinations over and over, ready for any button to be pressed and where to put the button-pressed-now-act code.