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
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;
}
}
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.
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;
}
}