#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 motionSensorPin = 2;
int feeding_times = 0;
const int Number_of_Feedings = 15;
void setup() {
// Initialize the servo motor
myservo.attach(servoPin);
// Close the pet feeder
myservo.write(0);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize the LED and motion sensor
pinMode(ledPin, OUTPUT);
pinMode(motionSensorPin, INPUT);
}
void loop() {
// Check if motion is detected
if (digitalRead(motionSensorPin) == HIGH) {
// Turn on the LED
digitalWrite(ledPin, HIGH);
// Display message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Feeding pet...");
feeding_times++;
// Open the pet feeder
myservo.write(90);
// Wait for 5 seconds
delay(5000);
// Close the pet feeder
myservo.write(0);
// Clear the LCD
lcd.clear();
// Turn off the LED
digitalWrite(ledPin, LOW);
}
// display how many times there was a feeding
lcd.setCursor(0, 1);
lcd.print("Feeding times:");
lcd.setCursor(14,1);
lcd.print(feeding_times);
// warning if to many feedings and lack of food
if (feeding_times > Number_of_Feedings) {
lcd.setCursor(0, 0);
lcd.print("Low food warning");
}
}