#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
#define pinrelay 26
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {34, 35, 32, 33}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {25, 26, 27}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
String StringVal;
int val, pinkey = 2021;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (pinrelay, OUTPUT);
digitalWrite(pinrelay, 1);
}
void loop() {
// put your main code here, to run repeatedly:
char Key = customKeypad.getKey();
if (Key) {
if (Key >= '0' && Key <= '9') {
StringVal += Key;
Serial.println(StringVal);
}
else if (Key == '#') {
if (StringVal.length() > 0) {
val = StringVal.toInt();
Serial.println(val);
if (val == pinkey){
Serial.println("Pintu Terbuka");
digitalWrite (pinrelay, 0);
delay(2000);
digitalWrite (pinrelay, 1);
} else {
digitalWrite(pinrelay, 1);
Serial.println("Pin Salah");
}
StringVal = "";
}
}
else if (Key == '*') {
StringVal = "";
}
}
}