Hi everyone! i am trying to make a very simple 4 x 4 keypad, as my keyboard doesn’t have a number keypad on it.I do a lot of trading and require typing in numbers a lot into my broker,so I want to make a simple keypad Hid using a Xiao rp2040 and a generic 4 x 4 key pad. i have tried every which way and cannot get the keypad to work. nothing comes up in serial monitor when i press a button i can load the code and it compiles OK. if i use an Arduino Uno it works fine to serial monitor so keypad is working.
My code is bellow and i have wired the keypad directly to the Xiao. many thanks for your help
#include <Keypad.h>
#include <Adafruit_TinyUSB.h>
const byte ROW_NUM = 4; //four rows
const byte COL_NUM = 4; //four columns
char keys[ROW_NUM][COL_NUM] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COL_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COL_NUM);
// USB HID object
Adafruit_USBD_HID usb_hid;
// HID report descriptor using TinyUSB’s template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1))
};
void setup() {
Serial.begin(9600);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.begin();
// wait until device mounted
while( !USBDevice.mounted() ) delay(1);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
uint8_t keycode[6] = { 0 };
keycode[0] = key - '0';
usb_hid.keyboardReport(1, 0, keycode);
delay(10);
usb_hid.keyboardRelease(1);
}
}