I am considering to use 2 pins for each switch I use; and was wondering if this is actually feasible.
The idea is to have 8 digital pins, and make a 4x4 matrix; which would end up giving me 16 switch.
So the first switch would be connected to pin 3 and 7, the second to 3 and 8, the third to 3 and 9 and so on.
Is it possible to do this; or it won't work? I may use a multiplexer in the worst case, but I was wondering about this, since I recall that this technique is used by old computer keyboards to allow all the keys to fit on limited IO .
You could use a different resistor for each key and connect them all to a single analog pin. You then need to analogRead() the pin which will give you a different value for each key pressed
This is what everyone does for keypads, keyboards, and all other places where you have lots of switches, from industrial equipment to pinball machines.
You can even buy pre-made matrix keypads with the switches wired up appropriately for this - search matrix keypad on ebay.
I thought I had to handle interrupts when using multiple pins and go deep down in the rabbit hole, but this seems pretty straightforward (at least looking at the example; then it is a matter of actually make the circuit and try it out).
Does this works even if I am not using a keypad? Can I connect straight away a button or a toggle switch; to the 2 pins and get the same result when I call getkey()? I am not knowledgeable about keypads, so I don't even know what other components may be inside a pre-made one.
shinyknight:
Does this works even if I am not using a keypad? Can I connect straight away a button or a toggle switch; to the 2 pins and get the same result when I call getkey()? I am not knowledgeable about keypads, so I don't even know what other components may be inside a pre-made one.
A keyboard matrix typically has no components other than switches. Schematically it looks like this:
So you could flash an example sketch and connect a single switch to the pins for Row1 and Col1 and when the switch was closed the software would detect it as switch SW1 active. In this scenario the microcontroller doesn't know the difference between the other switches being open and the other switches not existing in the circuit.
The comments about the inability of a system of multiple buttons on an analog pin to handle multiple simultaneous button presses are not necessarily correct. It depends on how these are wired and the choice of resistor values.
For example, if the following resistors are wired in series: 1k, 2k, 4k, and 8k and across each resistor a button is wired so pressing the button effectively shorts out the resistor it is wired across, then every combination of button presses (all 16 in this case) will yield a unique resistance of the chain in the range of zero to 15k which can be tested (indirectly - another resistor is needed to complete the potential divider) with analogRead ().
Programming it is not trivial because you cannot assume that the user will press the required multiple buttons exactly simultaneously so there is a timing problem to solve or you must wait until all the buttons are again released before further processing. I did once write a class to handle this situation for an application. The constructor took all the resistor values and a method returned the button(s) pressed.
For example, if the following resistors are wired in series: 1k, 2k, 4k, and 8k
The big problem here is the resistor tolerance, you can not get resistors in these values in the first place and if you make them up with other resistors the values have to be very tight in order for this trick to work with even four buttons. You get better results using an R/2R ladder where all the resistors are one value or double it, but you are still limited in the number of buttons you can use due to how close these resistors are to the exact value.