#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD screen with address 0x27, 16 columns and 2 rows
const int ledPin = 13;
const int buzzerPin = 10;
const int pirPin = 9;
const int ldrPin = A0;
const int potPin = A1;
const int buttonPin = 8; // Pin for the push button
int pirState = LOW;
int ldrState = LOW;
int potValue = 0;
bool alarmeActivee = false; // Variable to track the alarm state
bool buttonPressed = false; // Variable to debounce the button press
void setup() {
lcd.init(); // Initialize the LCD screen
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Alarm System");
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(ldrPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP); // Enable the pull-up resistor for the push button
}
void loop() {
// Read the button state
bool buttonState = digitalRead(buttonPin);
// Check if the button is pressed and the alarm is not active
if (buttonState == LOW && !buttonPressed && !alarmeActivee) {
buttonPressed = true; // Set the button pressed flag
alarmeActivee = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarme activee");
delay(1000); // Delay to avoid button bounce
}
// Check if the button is released
if (buttonState == HIGH) {
buttonPressed = false; // Reset the button pressed flag
}
// If the alarm is active
if (alarmeActivee) {
potValue = analogRead(potPin);
ldrState = digitalRead(ldrPin);
pirState = digitalRead(pirPin);
if ((ldrState == LOW) && (pirState == HIGH)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mouvement detecte!");
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(90); // Delay for LED and buzzer
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(900); // Delay before checking again
} else if (ldrState == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pas de mouvement");
}
} else {
// If the alarm is not active, display a message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarme desactivee");
}
}