Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This code reads the analog value from the keypad using the analogRead() function. The KEY_NONE constant represents the value of the analog pin when no key is pressed.
- The key values are defined based on the voltage divider ratios of the keypad. The if statements in the loop() function determine which key was pressed based on the analog value
- */
- // define the analog pin for the keypad
- const int KEYPAD_PIN = A0;
- // define the key values
- const int KEY_NONE = 1023;
- const int KEY_1 = 0;
- const int KEY_2 = 75;
- const int KEY_3 = 150;
- const int KEY_A = 225;
- const int KEY_4 = 300;
- const int KEY_5 = 375;
- const int KEY_6 = 450;
- const int KEY_B = 525;
- const int KEY_7 = 600;
- const int KEY_8 = 675;
- const int KEY_9 = 750;
- const int KEY_C = 825;
- const int KEY_STAR = 900;
- const int KEY_0 = 930;
- const int KEY_POUND = 970;
- void setup() {
- // initialize the serial port for debugging
- Serial.begin(9600);
- // print a message to the serial port
- Serial.println("Waveshare AD Keypad 4 x 4 ready!");
- }
- void loop() {
- // read the analog value from the keypad
- int analogValue = analogRead(KEYPAD_PIN);
- // determine which key was pressed based on the analog value
- int key = KEY_NONE;
- if (analogValue < KEY_1 + 5) {
- key = KEY_1;
- } else if (analogValue < KEY_2 + 5) {
- key = KEY_2;
- } else if (analogValue < KEY_3 + 5) {
- key = KEY_3;
- } else if (analogValue < KEY_A + 5) {
- key = KEY_A;
- } else if (analogValue < KEY_4 + 5) {
- key = KEY_4;
- } else if (analogValue < KEY_5 + 5) {
- key = KEY_5;
- } else if (analogValue < KEY_6 + 5) {
- key = KEY_6;
- } else if (analogValue < KEY_B + 5) {
- key = KEY_B;
- } else if (analogValue < KEY_7 + 5) {
- key = KEY_7;
- } else if (analogValue < KEY_8 + 5) {
- key = KEY_8;
- } else if (analogValue < KEY_9 + 5) {
- key = KEY_9;
- } else if (analogValue < KEY_C + 5) {
- key = KEY_C;
- } else if (analogValue < KEY_STAR + 5) {
- key = KEY_STAR;
- } else if (analogValue < KEY_0 + 5) {
- key = KEY_0;
- } else if (analogValue < KEY_POUND + 5) {
- key = KEY_POUND;
- }
- // print the key value to the serial port
- if (key != KEY_NONE) {
- Serial.print("Key pressed: ");
- Serial.println(key);
- }
- // wait for a short time
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement