Active2 years, 7 months ago
$begingroup$![4*4 matrix keypad arduino library 4*4 matrix keypad arduino library](/uploads/1/2/5/6/125637438/300577033.png)
I recently bought a 4x4 keypad.
It is one like this:
http://www.learningaboutelectronics.com/images/4x4-matrix-keypad-pinout.png
It is one like this:
http://www.learningaboutelectronics.com/images/4x4-matrix-keypad-pinout.png
Learn about 4x3 & 4x4 Membrane Keypad along with its Working, Pinout, Wiring, Library, Arduino Code for Reading Key Presses. Arduino Uno, 4x4 Keypad to relay. Ask Question Asked 2 years, 4 months ago. Arduino UNO seems to be dead. Arduino relay module not switching. Arduino uno serial communication. Arduino Uno Coding. Arduino-4X4 matrix keypad with I/O shift register-3. Keypad 4x4 in Arduino.
Now every code I found on the net was either using a highly overloaded library or was poorly hacked by a guy in the arduino forums.
I realized that almost all blogs about electronics just seem to copypaste the same code and changing the pictures so I felt like I had to work out something by myself.
I realized that almost all blogs about electronics just seem to copypaste the same code and changing the pictures so I felt like I had to work out something by myself.
Now, the common explained 'tactic' was to iterate through every column and give it a LOW signal [for button pressed DOWN] and then check if anything came in at the row sides.
Then use a technique from video game programming to make sure the button would not stay in a pin-down state [so one press would count at a time and not a press occurred every whatsomany nanoseconds].
Then use a technique from video game programming to make sure the button would not stay in a pin-down state [so one press would count at a time and not a press occurred every whatsomany nanoseconds].
I was highly uncomfortable with this solution, so I came up with this code:
As you can see, I reversed the idea (kind of) by giving every output a permanent LOW and made sure the program flow is stopped until the user releases the button.
I would like to hear your thougths on this solution.Is it wasting memory or CPU time?
Arduino programming is absolutely fun but I just recently started with it and want to get better so I ask all of you 'old veterans' to look over the lines and give me a feedback.
Arduino programming is absolutely fun but I just recently started with it and want to get better so I ask all of you 'old veterans' to look over the lines and give me a feedback.
clockw0rk
clockw0rkclockw0rk
$endgroup$1 Answer
$begingroup$Your general idea is correct - looping over columns to find out if one of them is active, and then only looping over the rows to find out which button was pressed will be somewhat faster (as in: more responsive) than checking each possible button separately.
If I were to suggest any improvements in the method - you could look into using pin change interrupts instead of busy looping. This would provide you with hardware support to detect when a pin goes LOW.This would also allow you to put your arduino to sleep to save power while waiting for a button to be pressed.
You might also need to debounce the keypress since you can get momentary false readings from a switch. This would basically require a small delay (usually less than 100ms) after you detect a keypress, and then rechecking after the delay if that switch is still active.
the comments below are going beyond your question but I decided to add them for some perspective on coding style
As far as your code goes I would suggest putting the entire key-checking logic in a single function that uses local variables for your column and row counters. This primarily helps with reasoning about the code but also has a small positive effect on memory usage overall (memory on the stack will be reclaimed after a return from the function).
Instead of using magic numbers throughout your code you should use constants/macros. This has the benefit of giving the value a name, and having a single place that would require changing instead of multiple values that are not easily ctrl+f'able. Calculating array length can be automated. This eases adding changes further on, and helps enforce correctness by the compiler.
You should also declare your pin arrays as const if possible. This will make optimizing your code easier for the compiler.
A sample of how this would look together:
OpiFOpiF
$endgroup$Not the answer you're looking for? Browse other questions tagged arduinoserial-portdevice-driver or ask your own question.
This tutorial of Robo India explain how a 4X4 Matrix Keypad (16 Buttons Keyboard) can be attached to Arduino.
1. Introduction:
A step by step illustrated tutorial to explain how to embed a 16 buttons 4X4 Keypad matrix to Arduino. All these buttons are connected to each other in a form of 4X4 matrix in row and column arrangement. This matrix keypad has got 8 pins, 4 for column and 4 for rows. To get it working output is given to either column or row and output is detected. This process is done at a high frequency for all of the columns and rows.
2. Required Hardware
Following Hardware will be required to perform this sketch of shift register.
S.No. | Item | Quantity |
1. | R-Board with FTDI or Arduino Board | 1 |
2. | 4X4 Matrix Keypad | 1 |
3. | Male to female Jumpers | 8 |
3. Building Circuit
Make following circuit with the help of above mentioned components.
3.1 You may go with Robo India’s R-Board(UNO Compatible)-
or
3.2 You may go with original Arduino UNO Board-
4. Programming:
![Arduino Uno Keypad 4x4 Arduino Uno Keypad 4x4](http://www.circuitbasics.com/wp-content/uploads/2017/07/Arduino-Keypad-Tutorial-4X4-and-3X4-Keypad-Connection-Diagram.png)
Once we are done with circuit part, here is our programme to this circuit. Every command of the following programme is explained in the comment section.
5. Output:
After uploading this sketch open serial monitor and you will see the following like result.
This circuit looks like following on R-Board and original UNO Board.
If you have any query please write us at [email protected]
our whatsapp helpline: +91 9694011188
Thanks and Regards
Content Development Team
Robo India
http://roboindia.com
Content Development Team
Robo India
http://roboindia.com