#include <Keypad.h>
#include <ezBuzzer.h>
#include <Servo.h>
#define SERVO_PIN A0
Servo myservo;
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns
int led = 12; // the pin that the LED is atteched to
int sensor = 11; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const String password = "1234"; // change your password here
String input_password;
const int busser = 10;
int angle = 0;
unsigned long lastTime;
ezBuzzer buzzer(busser);
void setup(){
Serial.begin(9600);
input_password.reserve(32); // maximum input characters is 33, change if needed
pinMode(busser, OUTPUT);
myservo.attach(SERVO_PIN);
myservo.write(0);
lastTime = millis();
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
}
void loop(){
char key = keypad.getKey();
if (angle == 90 && (millis() - lastTime) > 5000) { // 5 seconds
angle = 0;
myservo.write(angle);
Serial.println("Rotating Servo Motor to 0°");
}
if (key) {
Serial.println(key);
if(key == '*')
{
input_password = ""; // clear input password
} else if(key == '#')
{
if(password == input_password) {
Serial.println("password is correct");
angle = 90;
myservo.write(angle);
lastTime = millis();
// DO YOUR WORK HERE
}
else if(password != input_password) {
Serial.println("password is incorrect, try again");
tone(busser, 1000);
delay(1000);
noTone(busser);
delay(1000);
}
input_password = ""; // clear input password
}
else
{
input_password += key; // append new character to input password string
}
}
MotionSensor();
}
int MotionSensor() {
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(500); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(500); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}