#include <Stepper.h>
#include <Keypad.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution for your motor
// Stepper motor on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// Define the keypad layout and pins
const byte ROWS = 4; // Keypad rows
const byte COLS = 4; // Keypad columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {30, 31, 32, 33}; // Connect to the row pins of the keypad
byte colPins[COLS] = {34, 35, 36, 37}; // Connect to the column pins of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputCode = ""; // String to store entered code
String correctCode = "1234"; // Define the correct 4-digit code
bool doorIsOpen = false; // Keeps track of the door's current state
const int buttonPin = 6; // Pin for button
const int pirPin = 7; // Pin for PIR sensor
bool motorState = false; // Track motor state: true = open, false = closed
bool lockdown = false; // Lockdown state: true = disabled
void setup() {
// Set motor speed
myStepper.setSpeed(60);
// Initialize serial communication
Serial.begin(9600);
// Set button and PIR sensor as input
pinMode(buttonPin, INPUT);
pinMode(pirPin, INPUT);
}
void loop() {
// Check button and PIR sensor states
if (digitalRead(buttonPin) == HIGH || digitalRead(pirPin) == HIGH) {
lockdown = !lockdown; // Toggle lockdown on or off
delay(1000); // Debounce delay
}
// If the motor is locked down, skip control
if (lockdown) {
Serial.println("Motor is locked down.");
return;
}
// Handle keypad input
char key = keypad.getKey();
if (key) {
Serial.println(key);
// If the # key is pressed, toggle motor state (open/close)
if (key == '#') {
if (motorState) {
// Close the door (rotate motor back)
Serial.println("Closing door...");
myStepper.step(-stepsPerRevolution); // Close motor (1 revolution back)
motorState = false;
} else {
// Open the door (rotate motor forward)
Serial.println("Opening door...");
myStepper.step(stepsPerRevolution); // Open motor (1 revolution forward)
motorState = true;
}
}
// You can add more key functionality here if needed
}
}