// Pin definitions
const int doorbellPin = 2; // Doorbell button input
const int avRelayPin = 8; // Relay for analog AV switch control
const int joystickVRX = A0; // Analog input for X-axis of joystick
const int joystickVRY = A1; // Analog input for Y-axis of joystick
const int joystickSW = 3; // Digital input for joystick button
const int buttonB = 7; // Button B for part of the sequence
const int buttonA = 12; // Button A for part of the sequence
const int buttonStart = 13; // Start Button for part of the sequence
const int doorLockRelayPin = 10;// Relay for magnetic door lock control
const int led1Pin = A2; // LED 1 for sequence feedback
const int led2Pin = A3; // LED 2 for sequence feedback
const int led3Pin = A4; // LED 3 for sequence feedback
const int led4Pin = A5; // LED 4 for sequence feedback
// Threshold for joystick movement detection
const int threshold = 200;
// Variables to track system state
unsigned long avStartTime = 0;
unsigned long avDuration = 90000; // 90 seconds
bool avActive = false;
int currentStep = 0;
void setup() {
// Initialize pins
pinMode(doorbellPin, INPUT_PULLUP);
pinMode(avRelayPin, OUTPUT);
pinMode(doorLockRelayPin, OUTPUT);
pinMode(joystickSW, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonStart, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
// Initial state
digitalWrite(avRelayPin, LOW); // AV feed initially off
digitalWrite(doorLockRelayPin, LOW);// Door locked initially
digitalWrite(led1Pin, LOW); // LEDs off initially
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
}
void loop() {
// Check for doorbell press to activate AV feed
if (digitalRead(doorbellPin) == LOW && !avActive) {
avActive = true;
avStartTime = millis();
digitalWrite(avRelayPin, HIGH); // Switch AV to live feed
}
// Turn off AV feed after 90 seconds
if (avActive && (millis() - avStartTime > avDuration)) {
avActive = false;
digitalWrite(avRelayPin, LOW); // Switch AV back to idle feed
}
// Check joystick movement for puzzle sequence
int xValue = analogRead(joystickVRX);
int yValue = analogRead(joystickVRY);
if (xValue > (512 + threshold)) {
handleJoystickRight();
} else if (xValue < (512 - threshold)) {
handleJoystickLeft();
}
if (yValue > (512 + threshold)) {
handleJoystickUp();
} else if (yValue < (512 - threshold)) {
handleJoystickDown();
}
// Check if joystick button is pressed
if (digitalRead(joystickSW) == LOW) {
handleJoystickPress();
}
// Check for button presses as part of the sequence
if (digitalRead(buttonB) == LOW) {
handleButtonB();
}
if (digitalRead(buttonA) == LOW) {
handleButtonA();
}
if (digitalRead(buttonStart) == LOW) {
handleButtonStart();
}
delay(100); // Short delay to debounce and reduce CPU usage
}
void handleJoystickUp() {
// Handle joystick up logic
if (currentStep == 0 || currentStep == 1) {
currentStep++;
if (currentStep == 2) {
digitalWrite(led1Pin, HIGH); // Light up LED 1
}
} else {
resetSequence(); // Incorrect sequence, reset
}
}
void handleJoystickDown() {
// Handle joystick down logic
if (currentStep == 2 || currentStep == 3) {
currentStep++;
if (currentStep == 4) {
digitalWrite(led2Pin, HIGH); // Light up LED 2
}
} else {
resetSequence(); // Incorrect sequence, reset
}
}
void handleJoystickLeft() {
// Handle joystick left logic
if (currentStep == 4 || currentStep == 6) {
currentStep++;
} else {
resetSequence(); // Incorrect sequence, reset
}
}
void handleJoystickRight() {
// Handle joystick right logic
if (currentStep == 5 || currentStep == 7) {
currentStep++;
if (currentStep == 8) {
digitalWrite(led3Pin, HIGH); // Light up LED 3
}
} else {
resetSequence(); // Incorrect sequence, reset
}
}
void handleJoystickPress() {
// Placeholder for joystick button press handling
}
void handleButtonB() {
if (currentStep == 8) {
currentStep++;
} else {
resetSequence(); // Incorrect sequence, reset
}
}
void handleButtonA() {
if (currentStep == 9) {
currentStep++;
digitalWrite(led4Pin, HIGH); // Light up LED 4
} else {
resetSequence(); // Incorrect sequence, reset
}
}
void handleButtonStart() {
if (currentStep == 10) {
// Sequence completed correctly, unlock the door
unlockDoor();
resetSequence(); // Reset for future attempts
} else {
resetSequence(); // Incorrect sequence, reset
}
}
void unlockDoor() {
digitalWrite(doorLockRelayPin, HIGH); // Unlock the magnetic door
delay(5000); // Keep the door unlocked for 5 seconds
digitalWrite(doorLockRelayPin, LOW); // Lock the door again
}
void resetSequence() {
currentStep = 0;
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
}