#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
#include "HX711.h"
HX711 scale;
uint8_t dataPin = 25;
uint8_t clockPin = 26;
LiquidCrystal_I2C lcd(0x27,16,2);
Servo myservo;
int servoPin = 17;
int btn_feed = 35;
int btn_camera = 34;
int led_camera = 32;
int led_feed = 33;
int buttonState1 = LOW; // Current state of the first button (Camera)
int lastButtonState1 = LOW; // Previous state of the first button
int buttonState2 = LOW; // Current state of the second button (Feeder)
int lastButtonState2 = LOW; // Previous state of the second button
// The debounce time; increase if the output flickers
unsigned long debounceDelay = 50;
// Timers for debouncing
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
float previous_berat = 0;
float berat_semasa;
void setup() {
// put your setup code here, to run once:
// Init
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("CatFeeder-System");
lcd.setCursor(0, 1);
lcd.print("Load: 0.00 Kg");
scale.begin(dataPin, clockPin);
// load cell factor 5 KG
scale.set_scale(420.0983); // TODO you need to calibrate this yourself.
Serial.begin(9600);
Serial.println("Welcome to Cat Feeder System");
pinMode(btn_feed, INPUT_PULLUP);
pinMode(btn_camera, INPUT_PULLUP);
pinMode(led_camera, OUTPUT);
pinMode(led_feed, OUTPUT);
myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 17 to the servo object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
myservo.write(0); //move to original state (close food door)
}
void loop() {
// put your main code here, to run repeatedly:
//---------------Papar berat--------------
if (scale.is_ready())
{
berat_semasa = scale.get_units(1);
if(berat_semasa != previous_berat){
lcd.setCursor(0, 1);
//lcd.print(" "); //clear lcd line 2
lcd.print("Load:");
lcd.print(berat_semasa);
lcd.print(" Kg ");
Serial.println(berat_semasa);
previous_berat = berat_semasa;
}
}
//##############Camera button debounce########################
// Read the state of the first button
int reading1 = digitalRead(btn_camera);
// Check if the button state has changed
if (reading1 != lastButtonState1) {
lastDebounceTime1 = millis();
}
// If the button state has been stable for longer than the debounce delay, update the button state
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (reading1 != buttonState1) {
buttonState1 = reading1;
if (buttonState1 == HIGH) {
Serial.println("Button camera pressed");
digitalWrite(led_camera, HIGH);
lcd.setCursor(0, 0);
lcd.print("Taking Picture ");
lcd.setCursor(0, 1);
lcd.print("Send 2 Telegram ");
delay(5000);
digitalWrite(led_camera, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CatFeeder-System");
lcd.setCursor(0, 1);
lcd.print("Load:");
lcd.print(berat_semasa);
lcd.print(" Kg ");
}
}
}
lastButtonState1 = reading1;
//############################################################
//##############Feeder button debounce########################
// Read the state of the second button
int reading2 = digitalRead(btn_feed);
// Check if the button state has changed
if (reading2 != lastButtonState2) {
lastDebounceTime2 = millis();
}
// If the button state has been stable for longer than the debounce delay, update the button state
if ((millis() - lastDebounceTime2) > debounceDelay) {
if (reading2 != buttonState2) {
buttonState2 = reading2;
if (buttonState2 == HIGH) {
Serial.println("Button Feeding pressed");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Feeding Cat ");
digitalWrite(led_feed, HIGH);
myservo.write(180); //drop food - open door
delay(1000);
myservo.write(0); //close door
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CatFeeder-System");
lcd.setCursor(0, 1);
lcd.print("Load:");
lcd.print(berat_semasa);
lcd.print(" Kg ");
digitalWrite(led_feed, LOW);
}
}
}
lastButtonState2 = reading2;
//############################################################
}