#include "HX711_ADC.h"
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo myservo;
HX711_ADC LoadCell(9, 8);
HX711_ADC LoadCell2(3, 2);
LiquidCrystal_I2C lcd(0x27,16,2);
int taree = 6; //tare
int num = 1;
int pos = 0;
bool InUse = false;
unsigned long prevTime = millis();
long Time = 1000;
void setup()
{
pinMode (taree, INPUT_PULLUP);
Serial.begin(9600);
myservo.attach(7);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Smart Litter Box");
LoadCell.begin();
LoadCell.start(1000);
LoadCell.setCalFactor(0.42);
LoadCell2.begin();
LoadCell2.start(1000);
LoadCell2.setCalFactor(0.42);
lcd.clear();
}
void loop() {
unsigned long currentTime = millis();
static unsigned long currentTime1 = millis();
LoadCell.update();
LoadCell2.update();
// i = stock weight
float i = LoadCell.getData();
// j is used to check whether cat litter box is in use
// when cat enter the box it must step on a board(pressure sensor built in) in front of the box
// after the use cat will step on that board again
// we can use the weight change of j to judge whether the cat leave the box
float j = LoadCell2.getData();
switch(num){
// show stock weight
case 1:{
lcd.setCursor(0, 0);
lcd.print("Cat Litter Stock");
lcd.setCursor(0,1);
lcd.print("Stock: ");
lcd.print(i, 0);
lcd.print("g ");
break;
}
// low stock reminder
case 2:{
lcd.clear();
delay(500);
lcd.setCursor(0, 0);
lcd.print(" Low Stock ");
delay(500);
lcd.clear();
break;
}
// clean box
case 3:{
lcd.clear();
delay(200);
lcd.setCursor(0, 0);
lcd.print(" Sweeping");
delay(200);
lcd.setCursor(10, 0);
lcd.print(".");
delay(200);
lcd.setCursor(11, 0);
lcd.print(".");
delay(200);
lcd.setCursor(12, 0);
lcd.print(".");
delay(200);
lcd.setCursor(13, 0);
lcd.print(".");
delay(200);
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
num=1;
lcd.clear();
break;
}
// too much cat litter reminder
case 4:{
lcd.clear();
delay(500);
lcd.setCursor(0, 0);
lcd.print(" Over Loaded ");
delay(500);
lcd.clear();
break;
}
}
//before add cat litter press tare to reset the stock scale
//tare
if (digitalRead (taree) == LOW)
{
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Taring");
delay(200);
lcd.setCursor(9, 0);
lcd.print(".");
delay(200);
lcd.setCursor(10, 0);
lcd.print(".");
delay(200);
lcd.setCursor(11, 0);
lcd.print(".");
delay(200);
lcd.setCursor(12, 0);
lcd.print(".");
delay(200);
delay(500);
LoadCell.start(1000);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.clear();
}
// check if stock is low or overload or cat is in use every 1 second
if(currentTime - prevTime > Time){
// if not low stock
if(i>1000){
num=1;
}
// if low stock
if (i <=1000){
num=2;
}
// if cat is in use
if (j>0 && num==1){
InUse=true;
delay(5000);
}
// after use
if (j>0 && InUse){
InUse=false;
num=3;
}
// if overload
if (i>=20000 && num<4){
num=4;
}
// if not overload
if(i<20000 && num==4){
num=1;
lcd.clear();
delay(1000);
}
prevTime = currentTime;
}
}