Arduino 4x4 matrix button pressed or released

Hi can someone help me in this pls?
In the matrix i want some switches and i need to send signal to Joystick button 1 when it is in the ON position and when I turn it to the OFF position.
Here is the code and the wiring

#include <Keypad.h>
#include <Joystick.h>

const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4

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

void setup() {
  Serial.begin(9600);
}

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

  if (key == '4'){
    //case PRESSED
      //Joystick.setButton(0, 1);
      //delay(100);
      //Joystick.setButton(0, 0);
    //case RELEASED
      //Joystick.setButton(0, 1);
      //delay(100);
      //Joystick.setButton(0, 0);
  }
}

You can look at the KeypadEvent example. Below a modified version; you will have to adjust to your needs as this is only for 2x2 keypad.

#include <Keypad.h>
//********************************************
//set up for Switches or Keypad

const byte ROWS = 2;  //final panel many need to be expanded upto 4 Rows
const byte COLS = 2;  //final panel many need to be expanded upto 3 Cols
char keys[ROWS][COLS] = {
  { '1', '2' },
  { '3', '4' }
};
byte rowPins[ROWS] = { 2, 3 };
byte colPins[COLS] = { 4, 5 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup()
{
  Serial.begin(115200);
  while (!Serial)
    ;
  pinMode(ledPin, OUTPUT);               // Sets the digital pin as output.
  digitalWrite(ledPin, HIGH);            // Turn the LED on.
  keypad.addEventListener(keypadEvent);  // Add an event listener for this keypad
}

void loop()
{
  // read keypad; we can ignore the key that was pressed, it's handled in the event handler
  keypad.getKey();
}

// Taking care of some special events.
void keypadEvent(KeypadEvent key)
{
  switch (keypad.getState())
  {
    case PRESSED:
      Serial.write(key);
      Serial.println(F(" pressed"));
      break;
    case RELEASED:
      Serial.write(key);
      Serial.println(F(" released"));
      break;
    case HOLD:
      Serial.write(key);
      Serial.println(F(" held"));
      break;
    case IDLE:
      Serial.println(F("Idle"));
      break;
  }
}

I don't think these libraries work on Uno. You need a Pro Micro, Leonardo, or some other board with native-USB capability.

I have read that Uno can be made to work as a USB HID device, but it's advanced stuff. For a beginner, swapping to a Pro Micro is far easier.

In the online simulator I just trying with the UNO, but I will use the Pro Micro, online instead of Joystick.setButton(0, 1) I simply use Serial.println("1"), delay then "0".

In your original post, you didn't mention what type of arduino you were using, but you did show an image of an Uno. That's why I wanted to warn you that it would not work. If you had mentioned what type of Arduino you planned to use, that would have saved some time and typing.

That's what I want. And how can I watch the state of button '1' and button '3'(just these 2 not all)?

I'm not sure what the best place is to achieve that. For a simple project, I would probably use some global variables that you can set and clear in the event handler and monitor in loop().

As an example (not tested)

#include <Keypad.h>
//********************************************
//set up for Switches or Keypad

const byte ROWS = 2;  //final panel many need to be expanded upto 4 Rows
const byte COLS = 2;  //final panel many need to be expanded upto 3 Cols
char keys[ROWS][COLS] = {
  { '1', '2' },
  { '3', '4' }
};
byte rowPins[ROWS] = { 2, 3 };
byte colPins[COLS] = { 4, 5 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// button states
bool button1 = false;
bool button3 = false;

void setup()
{
  Serial.begin(115200);
  while (!Serial)
    ;
  keypad.addEventListener(keypadEvent);  // Add an event listener for this keypad
}

void loop()
{
  // read keypad; we can ignore the key that was pressed, it's handled in the event handler
  keypad.getKey();

  if(button1 == true)
  {
    // do something
  }

  if(button3 == true)
  {
    // do something else
  }

}

// Taking care of some special events.
void keypadEvent(KeypadEvent key)
{
  switch (keypad.getState())
  {
    case PRESSED:
      Serial.write(key);
      Serial.println(F(" pressed"));
      if (key == '1')
      {
        button1 = true;
      }
      else if (key == '3')
      {
        button3 = true;
      }
      break;
    case RELEASED:
      Serial.write(key);
      Serial.println(F(" released"));
      button1 = false;
      button3 = false;
      break;
    case HOLD:
      Serial.write(key);
      Serial.println(F(" held"));
      break;
    case IDLE:
      Serial.println(F("Idle"));
      break;
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.