#include <RTClib.h>
#include <Servo.h>
#include "HX711.h"
#include <LiquidCrystal_I2C.h>
void printTime(DateTime time);
void calculateDistance();
void displayLCD(DateTime time);
void calculateWeight();
const int trigPin = 2;
const int echoPin = 3;
const int buzzer = 4;
const int servoFeedCtn = 5;
const int loadPinDT = A0;
const int loadPinSCK = A1;
// event from 13:50 to 14:10
uint8_t morningFeedStartH = 7; // event start time: hour
uint8_t morningFeedStartM = 0; // event start time: minute
uint8_t morningFeedEndH = 24; // event end time: hour
RTC_DS1307 rtc;
HX711 scale;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
Servo myservo1, myservo2;
char daysOfTheWeek[7][12] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
void setup() {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1);
}
// automatically sets the RTC to the date & time on PC this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// manually sets the RTC with an explicit date & time, for example to set
// January 21, 2021 at 3am you would call:
// rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0));
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
myservo1.attach(servoFeedCtn);
myservo2.attach(53);
scale.begin(loadPinDT, loadPinSCK);
scale.set_scale();
scale.tare();
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop() {
DateTime now = rtc.now();
calculateWeight();
if (now.hour() >= morningFeedStartH &&
now.minute() >= morningFeedStartM &&
now.hour() < morningFeedEndH) {
Serial.println("It is on scheduled time");
calculateDistance();
}
else {
Serial.println("It is NOT on scheduled time");
}
//printTime(now);
displayLCD(now);
delay(1000);
}
void printTime(DateTime time) {
Serial.print("TIME: ");
Serial.print(time.year(), DEC);
Serial.print('/');
Serial.print(time.month(), DEC);
Serial.print('/');
Serial.print(time.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[time.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(time.hour(), DEC);
Serial.print(':');
Serial.print(time.minute(), DEC);
Serial.print(':');
Serial.println(time.second(), DEC);
}
void calculateDistance(){
long duration, distanceCm, distanceInch;
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
distanceInch = (duration/2) / 74; // Divide by 74 or multiply by 0.0135
//distanceCm = 0.017 * duration;
if(distanceCm < 30){
tone(buzzer, 500);
}
else{ // If the feed container is not empty
//displayLCD(now);
noTone(buzzer);
myservo1.write(0);
myservo2.write(180);
}
}
void displayLCD(DateTime time){
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Date: ");
lcd.print(time.month()); // print message at (0, 0)
lcd.print("/");
lcd.print(time.day());
lcd.print("/");
lcd.print(time.year());
//lcd.print(" ");
//lcd.print(daysOfTheWeek[time.dayOfTheWeek()]);
lcd.setCursor(0, 1);
lcd.print("TIME: "); // move cursor to (2, 1)
lcd.print(time.hour()); // print message at (2, 1)
lcd.print(":");
lcd.print(time.minute());
lcd.print(":");
lcd.print(time.second());
//delay(2000); // display the above for two seconds
}
void calculateWeight(){
if (scale.is_ready()) {
long reading = scale.get_units(); // Get the weight reading in kg
Serial.print("Weight: ");
Serial.print(reading, 2); // Print weight with 2 decimal places
Serial.println(" kg");
}
}