Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Example Code for 4x4 Keypad with Raspberry Pi Pico #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Add Example Code for 4x4 Keypad with Raspberry Pi Pico #542
Changes from 2 commits
79b986c
8a653c7
b30685b
f787419
139d981
b80d96d
52a7f7b
c56503e
7324b2a
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's worth trying to detect if multiple keys are being pressed (and signal that as an error condition), rather than just assuming that the first key detected is the only key pressed? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about the same but allow multiple keys pressed at the same time, but did not find an "simple" way of doing this ...
This is what I have in mind, let me know what you think about this.
Enhanced Keypad changes
Define a macro that allows switching between the basic single key read mode and the enhanced mode, which supports multiple key presses
When ENHANCED_KEYPAD is defined, the code uses a structure to hold the result of the key press. The MAX_MULTI_KEYS defines the maximum number of keys that can be pressed at the same time, and KeypadResult is a structure that contains the count of pressed keys and an array of those keys.
Depending on whether the
ENHANCED_KEYPAD
macro is defined, the code will either use the simple function that returns a single pressed key or the enhanced function that returns aKeypadResult
structure. The enhanced function iterates through the rows and columns of the keypad matrix, checking each key, and saves the number of keys pressed and their values in theKeypadResult
structure.The main method is modified to handle the enhanced keypad mode. When ENHANCED_KEYPAD is defined, the program will print the list of keys pressed. If no keys are pressed, it will print a message indicating that. Otherwise, it will print the number of keys pressed and list the keys in a comma-separated format.
Summary of Changes:
Enhanced Keypad Mode:
ENHANCED_KEYPAD
macro, allowing the detection of multiple key presses simultaneously.MAX_MULTI_KEYS
to specify the maximum number of keys that can be pressed at once.KeypadResult
structure to store the count and list of pressed keys.Enhanced
get_keypad_result
Function:KeypadResult
structure whenENHANCED_KEYPAD
is defined.KeypadResult
structure.ENHANCED_KEYPAD
is not defined.Updated
main
Method:main
method to handle the enhanced keypad mode:"No key pressed"
.ENHANCED_KEYPAD
is not defined.Improved Code Documentation:
For a raw diff of the changes, you can view this link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let me know if you agree with this implementation. I have created the
README.adoc
but this is including the 'Enhanced Keypad`.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that actually work? I thought that for these simple matrix keypads, if more than one key is pressed at a time you can't actually reliably detect which keys have been pressed? 🤷
That's why I suggested that if you detect that more than one key has been pressed, you return this as an error-condition.
(although I'm more of a software-person than a hardware-person, so I might be wrong?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this method is also working.
A keypad is a matrix of buttons arranged in rows and columns, typically used in devices like old phones or security systems. Each button on the keypad connects a unique row and column. When a key is pressed, it completes a circuit between a specific row and column, which can be detected by a microcontroller like the Raspberry Pi Pico.
To detect which key is pressed, the microcontroller sequentially sets each row to a high voltage (1) and checks the voltage on each column. If pressing a key connects the active row to a column, the microcontroller detects the column as high (1). By knowing which row and column are connected, the microcontroller can determine which key was pressed.
I went ahead and merged this enhanced code. I think it is a more complete example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid I'm still not convinced, see e.g. https://arduino.stackexchange.com/questions/50869/4x4-matrix-keypad-multiple-inputs-cause-extra-readings