#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BUTTON_PIN 12
#define LED_PIN 13
#define BUZZER_PIN 8
#define LED_ALARM_PIN 9
#define LDR_PIN A0
#define LIGHT_THRESHOLD 500
#define BAR_MAX 16 // Number of characters on LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust address if necessary
bool ledState = false;
bool lastButtonState = LOW;
bool currentButtonState;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_ALARM_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_ALARM_PIN, LOW);
}
void loop() {
int sensorValue = analogRead(LDR_PIN);
int barLength = map(sensorValue, 0, 1023, 0, BAR_MAX);
currentButtonState = digitalRead(BUTTON_PIN);
if (currentButtonState == HIGH && lastButtonState == LOW) {
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
delay(50); // Debounce delay
}
lastButtonState = currentButtonState;
if (sensorValue < LIGHT_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED_ALARM_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_ALARM_PIN, LOW);
}
// Clear LCD and print the bar graph
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LDR Value: ");
lcd.print(sensorValue);
lcd.setCursor(0, 1);
for (int i = 0; i < BAR_MAX; i++) {
if (i < barLength) {
lcd.write('*'); // Use '*' to represent the bar graph
} else {
lcd.write(' '); // Empty space for the rest
}
}
delay(1000); // Update every second
}