#include <Servo.h> // include all library
#include <LiquidCrystal.h>
#include "HX711.h"
#define calibration_factor 7050.0 //defined symbolic constant with a value of 7050.0.
const int rs = 13, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7; //declare for LCD pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//declare pins used in arduino
const int LOADCELL_DOUT_PIN = 14;
const int LOADCELL_SCK_PIN = 15;
float val = 0;
const int ledPin = 6;
const int trigPin = 2;
const int echoPin = 5;
const int servoPin = 3;
const int button = 4;
int status = false;
int buttonState;
Servo servo; //declare servo name
HX711 scale; //Declare HX711 scale to connect to the load sensor
// Declare every variable's input and output in this setup
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(button, INPUT_PULLUP);
servo.attach(servoPin);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); //load sensor begin to intialize its function
scale.set_scale(calibration_factor); //Calibrate the load sensor
scale.tare(); //Present values of scale read from the load sensor
lcd.begin(16,2); //initialize LCD
lcd.print("Hello Master!"); //LCD display
lcd.setCursor(0, 1); //set LCD position
lcd.print("food: ");
lcd.print(val); //get value
lcd.print("lbs");
Serial.begin(9600); //Serial monitor initialize
Serial.println("Readings:");
}
void loop() {
// Measure the distance using the ultrasonic sensor
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// Check if the distance is within the feeding range
if (distance <= 10) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Feeding cat . . .");
lcd.setCursor(0,1);
// Call the function to dispense food
dispenseFood();
}
else {
servo.write(0); // servo off
delay(1000);
lcd.clear();
lcd.print("Hello Master!");
lcd.setCursor(0, 1);
lcd.print("food: ");
lcd.print(val);
lcd.print("lbs");
digitalWrite(ledPin, LOW);
}
buttonState = digitalRead(button); // set button to feed the cat using button
if(buttonState == LOW)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Feeding cat . . .");
lcd.setCursor(0,1);
// Call the function to dispense food
dispenseFood();
}
else {
servo.write(0); // servo off
delay(1000);
lcd.clear(); // Clear LCD
lcd.print("Hello Master!");
lcd.setCursor(0, 1); // set LCD position
lcd.print("food: ");
lcd.print(val); // value of load
lcd.print("lbs");
digitalWrite(ledPin, LOW);
}
Serial.print("Reading: ");
val=scale.get_units(); // set variable for load sensor to get units from scale
Serial.print(val);
Serial.print(" lbs");
Serial.println();
if (val < 0.1) // set if value of load sensor below 0.1, led blinking
{
ledblinking();
}
else
{
digitalWrite(ledPin, LOW);
}
}
void dispenseFood() { // set function to dispense food
// Rotate the servo to dispense the food
servo.write(180);
delay(1000);
}
void ledblinking () { //set function for ledblinking function
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}