#include <Servo.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_BME280 bme; // I2C
unsigned long delayTime;
#define potpin A0 // analog pin used to connect the potentiometer
#define buzzer 6
#define button 9
#define led 3 // the pin that the LED is attached to
#define sensor 5 // the pin that the sensor is attached to
int state = LOW; // by default, no motion detected
int val1 = 0; // variable to store the sensor status
int buttonState = 0;
Servo myservo;
int val; // variable to read the value from the analog pin
void setup() {
pinMode(led, OUTPUT); // initialize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
Serial.println("Motion Sensor Activated!");
unsigned status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(5000);
pinMode(buzzer, OUTPUT);
pinMode(button, INPUT);
myservo.attach(A1); // attaches the servo on pin 9 to the servo object
}
void pot_loop(){
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
if(val<= 2){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE, BLACK);
display.setTextWrap(false);
display.setCursor(0, 0);
display.println("OPEN");
display.display();
delay(1000);
}
else if(val>=3 && val<=178){
OLED_loop();
}
else{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE, BLACK);
display.setTextWrap(false);
display.setCursor(0, 0);
display.println("CLOSE");
display.display();
delay(1000);
}
}
void buzzer_loop(){
tone(buzzer, 330); // tone(PIN, FREQUENCY)
delay(400);
tone(buzzer, 390);
delay(400);
tone(buzzer, 260);
delay(400);
noTone(buzzer);
delay(400);
tone(buzzer, 330);
delay(400);
tone(buzzer, 390);
delay(400);
tone(buzzer, 260);
delay(400);
noTone(buzzer);
delay(1000);
}
void butt_loop(){
buttonState = digitalRead(button); // checks the status of the button
if(buttonState == HIGH) { // plays the doorbell melody if the button is pressed
buzzer_loop();
}
}
void motion_loop(){
val1 = digitalRead(sensor); // read sensor value
if (val1 == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(100); // 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(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
Serial.println("");
state = LOW; // update variable state to LOW
}
}
}
void OLED_loop() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE, BLACK);
display.setTextWrap(false);
display.setCursor(0, 0);
display.print(bme.readTemperature());
display.println("*C");
display.display();
delay(1000);
}
void loop() {
pot_loop();
butt_loop();
motion_loop();
}