#include <Keypad.h>
#include <string.h>
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1', '2', '3'}, {'4', '5', '6'},
{'7', '8', '9'}, {'*', '0', '#'},
};
byte rowPins[rows] = {14, 15, 16, 17};
byte colPins[cols] = {18, 19, 20};
byte buzzerPin = 21;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
char combination[5] = "EEEE"; // Set to default combination
char correctCombination[5] = "1234"; // Correct combination
byte pointer = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
combination[pointer] = key;
pointer++;
Serial.println(pointer);
if (pointer >= 4) {
combination[pointer] = '\0';
if (strcmp(combination, correctCombination) == 0) {
tone(buzzerPin, 1500);
} else {
tone(buzzerPin, 1000);
}
Serial.println(combination);
Serial.println(strcmp(combination, correctCombination) == 0 ? "Correct" : "Wrong");
delay(700);
noTone(buzzerPin);
// Reset combination
memset(combination, 'E', sizeof(combination) - 1); // Reset to "EEEE"
combination[4] = '\0'; // Ensure null-termination
pointer = 0;
}
}
}