I want to make a program to turn ON turn OFF the LED using the 4x4 matrix keypad. Example, if I press "A' and release, the LED 1 will turn ON, then I press 'A' and release again, it will turn OFF the LED1. I dont understand how to add debounce for matrix keypad, or any programs that can make the LED turn ON and OFF like that. In my program, if I press 'A' the LED will turn ON, but if I release it the LED will turn OFF.
the details:
'A' for LED1
'B' for LED2
'C' for LED3
could you help me with the code? thank you
#include <Keypad.h>
const byte LED1 = 13;
const byte LED2 = 12;
const byte LED3 = 11;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{
{'A', 'B', 'C', 'D'},
{'E', 'F', 'G', 'H'},
{'I', 'J', 'K', 'L'},
{'M', 'N', 'O', 'P'},
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //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 );
void setup()
{
Serial.begin(9600);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop()
{
char keypressed = keypad.getKey();
if (keypressed)
{
Serial.println(keypressed);
}
if (keypressed == 'A')
{
digitalWrite(LED1, HIGH);
delay(250);
}
else
{
digitalWrite(LED1, LOW);
}
}