#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the LCD display connections
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the servo motor
Servo myservo;
int servoPin = 10;
// Define the pin connections
int ledPin = 13;
int pirPin = 2;
int buttonPin = 9;
// Define the ultrasonic sensor pins
int trigPin = 6;
int echoPin = 7;
void setup() {
// Initialize the servo motor
myservo.attach(servoPin);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize the LED and motion sensor
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
// Initialize the ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Check if motion is detected
if (digitalRead(pirPin) == HIGH) {
// Turn on the LED
digitalWrite(ledPin, HIGH);
// Display message on LCD
lcd.setCursor(0, 0);
lcd.print("Feeding pet...");
myservo.write(90); // Open the pet feeder
delay(5000); // Wait for 5 seconds
myservo.write(0); // Close the pet feeder
lcd.clear(); // Clear the LCD
digitalWrite(ledPin, LOW); // Turn off the LED
}
if (digitalRead(buttonPin) == LOW) {
myservo.write(90); // Unlock the pet feeder
lcd.setCursor(0, 0);
lcd.print("Feeding pet...");
} else {
myservo.write(0); // Lock the pet feeder
lcd.clear(); // Clear the LCD
}
// Check the food level
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH);
int distance = (duration / 2) / 29.1;
if (distance < 5) {
lcd.setCursor(0, 1);
lcd.print("Food level: Full");
} else {
lcd.setCursor(0, 1);
lcd.print("Food level: Low");
}
delay(500); // Debounce delay
}