int pirState = LOW;
#define Buzzer 4
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int interval = 5000;
long previousMillis = 0;
int IntervalR = 3000; // Interval at which to toggle LED3 (milliseconds)
long previousMillisR = 0;
bool ledStateR = LOW;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(17, INPUT);
pinMode(23, OUTPUT);
pinMode(18, OUTPUT);
Serial.begin(9600);
pinMode(Buzzer, OUTPUT);
lcd.init(); //set LCD
lcd.backlight();
}
void loop()
{
const int IP = digitalRead(17);
delay(100);
long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (IP == 1) // If motion is detected by the PIR sensor
{
digitalWrite(23, HIGH);
digitalWrite(18, LOW);
if (pirState == LOW) // Update pirState to indicate motion detection
{
Serial.println("Bin is opening!");
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Bin is opening");
pirState = HIGH;
}
else
{
if (pirState == HIGH) // Update pirState to indicate motion detection
{
Serial.println("Bin is closing...");
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Bin is closing");
pirState = LOW;
delay(3000);
Serial.println("Bin is closed.");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Bin is closed...");
delay(500);
digitalWrite(Buzzer, HIGH); //buzzer make sound
delay(100);
digitalWrite(Buzzer, LOW);
digitalWrite(23, LOW);
digitalWrite(18, HIGH);
}
digitalWrite(23, LOW);
digitalWrite(18, HIGH);
}
}
}
}