I found a 4x4 button keypad module that I thought I could use to make a custom USB “quick keys” HID since I use a lot of the same keystrokes all day every day. I'm using a knock-off Sparkfun ProMicro controller.
The setup looked really easy as it only used one analog pin, 5v, and GND. A really simple sketch should do the trick to test what they claim for the values for each button:
#include <Keyboard.h>
int buttonPin = A0;
int buttonValue;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
analogWrite(buttonPin, HIGH);
}
void loop() {
int buttonValue = analogRead(buttonPin);
if (buttonValue != 0)
{
PRINT_INT(buttonValue);
delay(1000);
}
}
void PRINT_INT(int VAL)
{
char buffer[60];
sprintf(buffer, "PRINT: %d", VAL);
Serial.println(buffer);
}
Reading the pin works, printing the button value works, but the numbers are nothing like the manufacturer printed on the back of the keypad. I’m wondering if there’s something wrong with the code, or if I just got bad hardware from the supplier?
I’ve used the same method of reading the pin and expecting a certain percentage of the 1023 from the 5v on a detent potentiometer and it works like a charm, so the code seems like it should work.
analogWrite(buttonPin, HIGH);
What is this for? Are you trying to turn on the internal pullup on A0? If so, use digitalWrite, but I would leave the pullup off because the keypad has a 10K pulldown resistor to ground.
Get rid of that 'analogWrite()'. It is NOT doing anything good.
Get rid of the 'pinMode()' since it is not needed for analog input.
Do you then at least get a very similar number each time you press a particular button?
You will probably need to add some buffering around the values since they are unlikely to match exactly. The chart shows that the closest two numbers (435 and 465) are 30 apart. I would allow plus-or-minus ten to count as a match. You may also want to adjust the 'center' numbers for your particular keypad.
Note: PRINT_INT(buttonValue); adds nothing that you don't get with 'Serial.print("PRINT: "); Serial.println(buttonValue);'
david_2018:
analogWrite(buttonPin, HIGH);
What is this for? Are you trying to turn on the internal pullup on A0? If so, use digitalWrite, but I would leave the pullup off because the keypad has a 10K pulldown resistor to ground.
Remnants of the sample code to grabbed online. I'll remove that per your suggestion and see what happens.
Do you then at least get a very similar number each time you press a particular button?
Yes, the numbers are the same each time I press each button. Button #1 is 1014 (or 1013, I was going to accommodate for the buffering as you suggested later), button #2 is 29, and all the others are 0-3. Each button remains consistent with its value each time it is pressed- its the fact that the values for each button are very wrong that is the issue.
Both responses referenced what (now) looks like unnecessary remnants of the example code I started with, that I see now took a different approach using digital pins and not the same keypad. I'll rework things based on the suggestions here, thanks!
justerson:
Yes, the numbers are the same each time I press each button. Button #1 is 1014 (or 1013, I was going to accommodate for the buffering as you suggested later), button #2 is 29, and all the others are 0-3. Each button remains consistent with its value each time it is pressed- its the fact that the values for each button are very wrong that is the issue.
You may have a hardware problem. Get out a magnifying glass and check for bad solder connections, misplaced resistors that miss the solder pad on one end, etc. If you have a voltmeter, check the actual voltage coming from the keypad output pin. Also check your wiring for bad connections.
Get rid of that 'analogWrite()'. It is NOT doing anything good.
Get rid of the 'pinMode()' since it is not needed for analog input.
Yep, did the trick. Thanks all!