1

I have been trying to get a button matrix to work but im not experienced enough in programming.

I Have tried to read up but i don't know how to execute so some help would be greatly appreciated.

I want the keypad to function as a game controller of sorts. when i pres the 1 key on the keypad i want the read light to light up and only go out when i release the button. code was copied from "mstanley" on arduino forum.

Q: How do i use the Keypad.h library to get my keypad press to run one line of code when i press a button and another line when i release the button.

I Was testing with leds now, the code is identical, digitalWrite(1, HIGH); then digitalWrite(1, LOW); this can easily get exchanged for joystic.PressButton 1 and Joystick.ReleaseButton 1

enter image description here

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char holdKey;
unsigned long t_hold;

void setup()
{
  Serial.begin(9600);
  
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}
 
void loop(){
  char key = keypad.getKey();
 
   if (key){
     holdKey = key;
     Serial.println(key);
   }
 
   if (keypad.getState() == HOLD) {
      if ((millis() - t_hold) > 100 ) {
          switch (holdKey) {
              case '1':
                  digitalWrite(11, HIGH);
                  delay(200);
                  digitalWrite(11, LOW);
                  break;
              case '2':
                  digitalWrite(12, HIGH);
                  delay(200);
                  digitalWrite(12, LOW);
                  break;
              case '3':
                  digitalWrite(13, HIGH);
                  delay(200);
                  digitalWrite(13, LOW);
          }
          t_hold = millis();
      }
   }
}
4
  • What do you mean by "game input device"? Do you want to use it as gamepad for your PC? Or is this just about the LEDs?
    – chrisl
    Aug 26 '20 at 13:44
  • I Was testing with leds now, the code is identical, digitalWrite(1, HIGH); then digitalWrite(1, LOW); this can easily get exchanged for joystic.PressButton 1 and Joystick.releasebutton 1 Aug 26 '20 at 13:52
  • do you have a question? ... it is unclear what problem you are having
    – jsotola
    Aug 26 '20 at 14:29
  • sorry it was unclear. Q: How do i use the Keypad.h library to get my keypad press to run one line of code when i press a button and another line when i release the button. Aug 27 '20 at 5:17
2

I did a post about keypad handlers. I'm reluctant to mention it because we aren't supposed to post "link only" answers.

It refers to a library available on GitHub which handles keypads. In particular you can have a callback function for both the keydown and the keyup, for multiple keys.

How do i use the Keypad.h library to get my keypad press to run one line of code when i press a button and another line when i release the button.

That seems to be what you want to do.

Example code:

/*
 * 
 * Example of using Keypad_Matrix with a 4x4 keypad matrix.
 * 
 */

#include <Keypad_Matrix.h>

const byte ROWS = 4;
const byte COLS = 4;

// how the keypad has its keys laid out
const char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'},
};

const byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
const byte colPins[COLS] = {2, 3, 4, 5}; //connect to the column pinouts of the keypad

  // Create the Keypad
Keypad_Matrix kpd = Keypad_Matrix( makeKeymap (keys), rowPins, colPins, ROWS, COLS );

void keyDown (const char which)
  {
  Serial.print (F("Key down: "));
  Serial.println (which);
  }

void setup() 
{
  Serial.begin (115200);
  Serial.println ("Starting.");
  kpd.begin ();
  kpd.setKeyDownHandler (keyDown);
}

void loop() 
{
  kpd.scan ();
  // do other stuff here
}

You can also use setKeyUpHandler to do another function when a key is released. You can also call isKeyDown to see if a key is down (eg. when pressing key A is key B also down?).

Note that (as mentioned in the linked post) you need to use diodes to stop having false (ghost) key presses if more than two keys are pressed at once.

1
  • Hi! Thank you for the answer! Aug 31 '20 at 8:57
1

How do i use the Keypad.h library to get my keypad press to run one line of code when i press a button and another line when i release the button

Edit 1:

void loop() {
  char key = keypad.getKey();

  if (key != NO_KEY) {
    Serial.println(key);
  }

  if (keypad.isPressed('*'))
  {
  randomr = random(10, 2000);
  Serial.println(randomr);
  }

}


From the link: https://playground.arduino.cc/Code/Keypad/ you can find all the functions supported by the Keypad library.

One relevant function is

KeyState getState()

It returns the current state of any of the keys. The four states supported are IDLE, PRESSED, RELEASED, and HOLD.

Hence you can use this function for the keys you are interested in.
Also, if helpful consider this function as well.

setHoldTime(unsigned int time)

Set the number of milliseconds the user will have to hold a button until the HOLD state is triggered.


You can find the keypad.h library in action here. I motivate you to also try the code with this online Arduino emulator

you already have a builtin function defined to get the status of the key pressed

void loop() {
  char key = keypad.getKey();

  if (key != NO_KEY) {
    Serial.println(key);
  }
}

I have a poorly taken screenshot for the same for your quick reference.

Original Arduino emulator link: (you can play with the logic) enter image description here

https://wokwi.com/playground/keypad


Disclosure: I use wokwi.com, to create interesting tools and libraries for the Arduino community 🤟 https://github.com/wokwi

for a more free demonstration of the libraries:
https://github.com/wokwi/arduino-libraries

2
  • Hi! Thank you for your answer. im aware och the KeyState getState() command but i do not understand how to implement it in code. i have not found an example that shows it doing what i'm after. in kombination wit the keyStateChanged() komand i feel like it should definitely be possible. Aug 28 '20 at 7:14
  • @OpticalOpal I will get back to you with an example
    – ArduinoFan
    Aug 28 '20 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.