// Include the required libraries
#include <LiquidCrystal_I2C.h>
// Define the pins for the LED, light sensor, and motion sensor
const int LED_PIN = 13;
const int SENSOR_PIN = A0;
const int MOTION_PIN = 2;
// Define the threshold values for the light sensor and motion sensor
const int LIGHT_THRESHOLD = 500;
const int MOTION_THRESHOLD = 1000; // in milliseconds
// Define the break interval in minutes
const int BREAK_INTERVAL = 30;
// Initialize the LCD screen
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Set the LED pin and motion sensor pin as an output
pinMode(LED_PIN, OUTPUT);
pinMode(MOTION_PIN, INPUT);
// Initialize the LCD screen
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
}
void loop() {
// Read the value from the light sensor and motion sensor
int lightValue = analogRead(SENSOR_PIN);
bool motionDetected = detectMotion(MOTION_PIN, MOTION_THRESHOLD);
// If the light value is below a threshold and motion is detected for threshold time, turn on LED and display break message
if (lightValue < LIGHT_THRESHOLD && motionDetected) {
// Wait for the break interval to be reached
delay(BREAK_INTERVAL * 60 * 1000);
// Turn on the LED and display a message on the LCD screen
digitalWrite(LED_PIN, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Take a break now!");
delay(60 * 1000);
}
else if (motionDetected) {
// Display a message on the LCD screen if motion is detected
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detected!");
}
else {
// Turn off the LED and clear the LCD screen
digitalWrite(LED_PIN, LOW);
lcd.clear();
}
}
bool detectMotion(int pin, int threshold) {
unsigned long startTime = millis();
bool motionDetected = false;
while (millis() - startTime < threshold) {
if (digitalRead(pin) == HIGH) {
motionDetected = true;
break;
}
}
return motionDetected;
}