#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h>
#include <Servo.h> // Include the Servo library
#define LDR_PIN A0 // Change LDR_PIN to the appropriate analog pin for your LDR
#define SERVO_PIN 9 // Define the pin for the servo
#define LED1_PIN 5 // Define the pin for the first LED
#define LED2_PIN 7 // Define the pin for the second LED
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myServo; // Create a Servo object
void setup() {
pinMode(LDR_PIN, INPUT);
pinMode(LED1_PIN, OUTPUT); // Set LED1_PIN as output
pinMode(LED2_PIN, OUTPUT); // Set LED2_PIN as output
lcd.init();
lcd.backlight();
lcd.begin(20, 4); // Initialize LCD screen for 20x4 display
myServo.attach(SERVO_PIN); // Attach the servo to the defined pin
Serial.begin(9600); // Start the serial communication
}
void loop() {
int ldrValue = analogRead(LDR_PIN); // Read LDR value
// Print the LDR value to the Serial Monitor
Serial.print("LDR Value: ");
Serial.println(ldrValue);
lcd.clear(); // Clear the LCD display before printing new values
lcd.setCursor(0, 0);
lcd.print("LDR Value: ");
lcd.print(ldrValue);
lcd.setCursor(0, 1);
lcd.print("Room: ");
if (ldrValue < 500) { // Adjust threshold based on your LDR
lcd.print("Dark ");
// Move servo like a worm
for (int pos = 0; pos <= 90; pos += 1) { // Sweep from 0 to 90 degrees
myServo.write(pos); // Move the servo to the current position
delay(3); // Reduced delay for faster movement
}
for (int pos = 90; pos >= 0; pos -= 1) { // Sweep from 90 to 0 degrees
myServo.write(pos); // Move the servo to the current position
delay(3); // Reduced delay for faster movement
}
// Turn LEDs on
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
} else {
lcd.print("Light!");
myServo.write(0); // Reset servo to 0 degrees when it's light
// Turn LEDs off
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, HIGH);
}
delay(100); // Adjust delay as necessary
}