#include <Wire.h>
#include <Keypad.h>
#include <Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define motorPin A2
#define buzzerPin A3
int ledPin = 13; // choose the pin for the LED
int inputPin = 8; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
///////// Servo ////////////////
int servo_position = 0;
Servo doorMotor; // create servo object to control a servo
///////////////////////////// For key board ////////////////////////////////////
String password ="5555";
const byte ROWS = 4; /* four rows */
const byte COLS = 4; /* four columns */
/* define the symbols on the buttons of the keypads */
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5,4,3,2}; /* connect to the row pinouts of the keypad */
byte colPins[COLS] = {A1,A0,7,6}; /* connect to the column pinouts of the keypad */
/* initialize an instance of class NewKeypad */
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
//pinMode(buzzerPin, OUTPUT);
doorMotor.attach(motorPin);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(10, 30); // position to display
oled.println("Starting"); // text to display
oled.display(); // show on OLED
}
////////////////////////////////////////////////////////
void loop() {
char customKey = customKeypad.getKey();
if (customKey){
//Serial.println(customKey);
//printToOled(String(customKey),0,0);
checkPassword();
}
else{
checkIntruders();
}
}
/////////////////////////////////////////////////////////
void buzz(int frequency, int duration) {
//int period = 1000000L / frequency; // Calculate the period in microseconds
int halfPeriod = 1500; // Half of the period
digitalWrite(buzzerPin, HIGH); // Turn the buzzer on
delay(halfPeriod); // Wait for half the period
digitalWrite(buzzerPin, LOW); // Turn the buzzer off
//delay(halfPeriod); // Wait for half the period
}
void printToOled(String text , int x , int y){
//Serial.println(text);
//oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(x, y); // position to display
oled.println(text); // text to display
oled.display();
}
void checkPassword(){
String pin = "";
int cursorX = 10;
oled.clearDisplay(); // clear display
printToOled("Enter pin:",0,0);
//Serial.println("Enter pin:");
//delay(500);
bool pinProtected = true;
while(pinProtected){
char customKey = customKeypad.getKey();
if (customKey){
if(cursorX>=58){
if(pin == password){
oled.clearDisplay(); // clear display
printToOled("Access ",0,20);
printToOled("Granted!",20,35);
//Serial.println("Access Granted!");
delay(500);
doorLock(true);
break;
}
else{
oled.clearDisplay(); // clear display
printToOled("Access",0,20);
printToOled("Denied!",20,35);
//Serial.println("Access Denied!");
delay(500);
break;
}
}
if(customKey == '#'){
oled.clearDisplay(); // clear display
printToOled("Canceled",0,0);
break;
}
else{
//Serial.println(customKey);
printToOled(String(customKey),cursorX,20);
pin+=customKey;
cursorX+=12;
//Serial.println(pin);
//Serial.println(cursorX);
}
}
}
}
void doorLock(bool open){
//buzzerBeep();
// buzz(1000, 300); // Buzz for 100 milliseconds with a 1000 Hz frequency
// delay(1000); // Pause for 1 second
for (servo_position= 0; servo_position<= 180; servo_position+= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
doorMotor.write(servo_position); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (servo_position= 180; servo_position>= 0; servo_position-= 1) { // goes from 180 degrees to 0 degrees
doorMotor.write(servo_position); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
void checkIntruders(){
oled.clearDisplay(); // clear display
val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW)
{
//Serial.println("Motion detected!"); // print on output change
printToOled("detected",0,20);
delay(100);
pirState = HIGH;
}
}
else
{
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH)
{
//Serial.println("Motion ended!"); // print on output change
printToOled("ended!",0,20);
delay(100);
pirState = LOW;
}
}
}