#include <Keypad.h>
#include <Arduino.h>
#define TONE_USE_INT
#define TONE_PITCH 440
#include <TonePitch.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);
const uint8_t buzzerPin = 13;
int YELLOW[] = {10, 11, 12, A0, A1, A2, A3};
int RED = A4;
int GREEN = A5;
const String password = "ABCD123";
String enteredPassword = "";
void setup() {
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
for (int i = 0; i < 7; i++) {
pinMode(YELLOW[i], OUTPUT);
}
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
playNoteForKey(key);
if (key == '>') {
checkPassword();
} else {
if (enteredPassword.length() < 7) {
enteredPassword += key;
lightUpLEDs(enteredPassword.length());
}
if (enteredPassword.length() == 7) { // Check if password length has reached
checkPassword(); // Automatically check the password
}
}
}
}
void playNoteForKey(char key) {
int note;
if (key == '>') {
note = NOTE_C6;
} else {
note = NOTE_C5;
}
tone(buzzerPin, note);
delay(50); // Play the note for 50 milliseconds (adjust as needed)
noTone(buzzerPin);
}
void lightUpLEDs(int numLEDs) {
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
for (int i = 0; i < numLEDs; i++) {
digitalWrite(YELLOW[i], HIGH);
}
for (int i = numLEDs; i < 7; i++) {
digitalWrite(YELLOW[i], LOW);
}
}
void checkPassword() {
if (enteredPassword == password) {
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
} else {
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
}
enteredPassword = "";
delay(1000); // Delay for 1 second to display the result
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
for (int i = 0; i < 7; i++) {
digitalWrite(YELLOW[i], LOW);
}
}