#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h> // Include the Servo library
#define PIR_SENSOR_PIN 2
#define GREEN_LED_PIN 3
#define RED_LED_PIN 4
#define SERVO_PIN 9
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myServo; // Create a Servo object
void setup() {
pinMode(PIR_SENSOR_PIN, INPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
lcd.begin(16, 2);
lcd.backlight();
myServo.attach(SERVO_PIN); // Attach the servo to the specified pin
// Display initial message
lcd.print("Pet Feeder");
lcd.setCursor(0, 1);
lcd.print("Waiting...");
Serial.begin(9600);
}
void loop() {
if (checkMotion()) {
// Motion detected, pet is present
digitalWrite(GREEN_LED_PIN, HIGH);
lcd.clear();
lcd.print("Pet detected");
delay(2000);
// Dispense food using the servo
dispenseFood();
// Indicate the end of dispensing
digitalWrite(RED_LED_PIN, HIGH);
lcd.clear();
lcd.print("Feeding done");
delay(2000);
// Reset LEDs and LCD
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
lcd.clear();
lcd.print("Pet Feeder");
lcd.setCursor(0, 1);
lcd.print("Waiting...");
}
}
bool checkMotion() {
return digitalRead(PIR_SENSOR_PIN) == HIGH;
}
void dispenseFood() {
// Move the servo to dispense food
myServo.write(360); // Adjust the angle as needed
delay(2000); // Wait for food to be dispensed
myServo.write(0); // Move the servo back to its initial position
delay(1000); // Wait for the servo to return
}