#include <EEPROM.h>
// Pin assignments
const int buttonPins[] = {9, 8, 7, 6, 5, 4, 3, 2};
const int ledPins[] = {A0, A1, A2, A3, A4, A5, 12, 13};
const int lockPin = 10;
const int buzzerPin = 11;
// Note mappings
const char notes[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B', 'H'}; // Using 'H' instead of duplicate 'C'
const int sequenceLength = 5;
char generatedSequence[sequenceLength];
char inputSequence[sequenceLength];
int inputIndex = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 8; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Make sure all LEDs are off initially
}
pinMode(lockPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
startSequence();
}
void loop() {
for (int i = 0; i < 8; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
inputSequence[inputIndex] = notes[i];
inputIndex++;
playTone(notes[i], ledPins[i]);
delay(200); // Debounce delay
if (inputIndex == sequenceLength) {
if (checkSequence()) {
openLock();
delay(1000); // Wait for 3 seconds
} else {
incorrectSequence();
delay(2000); // Wait for 3 seconds
}
startSequence();
inputIndex = 0; // Reset for next input
}
}
}
}
void startSequence() {
generateSequence();
playSequence();
}
void generateSequence() {
randomSeed(analogRead(0));
for (int i = 0; i < sequenceLength; i++) {
generatedSequence[i] = notes[random(0, 8)];
}
Serial.print("Generated Sequence: ");
for (int i = 0; i < sequenceLength; i++) {
Serial.print(generatedSequence[i]);
Serial.print(' ');
}
Serial.println();
}
void playSequence() {
for (int i = 0; i < sequenceLength; i++) {
int ledPin = getLedPin(generatedSequence[i]);
playTone(generatedSequence[i], ledPin);
delay(500); // Delay between notes
}
}
void playTone(char note, int ledPin) {
int frequency;
switch (note) {
case 'C': frequency = 261; break;
case 'D': frequency = 294; break;
case 'E': frequency = 329; break;
case 'F': frequency = 349; break;
case 'G': frequency = 392; break;
case 'A': frequency = 440; break;
case 'B': frequency = 493; break;
case 'H': frequency = 523; break; // 'H' for high C
}
digitalWrite(ledPin, HIGH);
tone(buzzerPin, frequency, 300);
delay(200); // Ensure LED stays on for the duration of the tone
digitalWrite(ledPin, LOW);
}
bool checkSequence() {
for (int i = 0; i < sequenceLength; i++) {
if (inputSequence[i] != generatedSequence[i]) {
return false;
}
}
return true;
}
void openLock() {
digitalWrite(lockPin, HIGH); // Unlock the door
playWinnerMelody(); // Play the winner melody
delay(500); // Keep the door unlocked for 2 seconds
digitalWrite(lockPin, LOW); // Lock the door again
}
void incorrectSequence() {
// Play "wrong code" melody
int melody[] = {523, 493, 440, 392, 349, 329, 294, 261}; // H, G, E, C
int noteDurations[] = {200, 200, 200, 200, 200, 200, 200, 200};
for (int i = 0; i < 8; i++) {
int ledPin = getLedPinFromFrequency(melody[i]);
digitalWrite(ledPin, HIGH);
tone(buzzerPin, melody[i], noteDurations[i]);
delay(noteDurations[i] * 1.3); // Adding a delay between notes
digitalWrite(ledPin, LOW);
}
}
void playWinnerMelody() {
// Play "winner" melody 3 times
int melody[] = {261, 294, 329, 349, 392, 440, 493, 523}; // C, D, E, F, G, A, B, H
int noteDurations[] = {50, 50, 50, 50, 50, 50, 50, 50};
for (int j = 0; j < 4; j++) { // Repeat the melody 3 times
for (int i = 0; i < 8; i++) {
int ledPin = getLedPinFromFrequency(melody[i]);
digitalWrite(ledPin, HIGH);
tone(buzzerPin, melody[i], noteDurations[i]);
delay(noteDurations[i] * 1.3); // Adding a delay between notes
digitalWrite(ledPin, LOW);
}
}
}
int getLedPin(char note) {
for (int i = 0; i < 8; i++) {
if (notes[i] == note) {
return ledPins[i];
}
}
return -1; // In case no match is found
}
int getLedPinFromFrequency(int frequency) {
switch (frequency) {
case 261: return A0; // C
case 294: return A1; // D
case 329: return A2; // E
case 349: return A3; // F
case 392: return A4; // G
case 440: return A5; // A
case 493: return 12; // B
case 523: return 13; // H (high C)
}
return -1; // In case no match is found
}