#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#define PIN_SYS_SWITCH_VALVE 1
#define PIN_SYS_INDOOR_MODE 2
#define PIN_SYS_OPTION_TOGGLE 3
#define PIN_SYS_OPTION_SELECT 4
#define PIN_DETECT_LOW_FOOD 5
#define PIN_DETECT_MOVEMENT 6
#define NOTIF_BELOW_FOOD 1
#define NOTIF_BOWL_EMPTY 2
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
boolean is_indoor = false;
byte app_state = 1;
short food_gram_set = 0, food_grams[4] = {25, 30, 35, 40};
void setup() {
lcd.begin(16,2);
for(byte i=2; i<=6; i++)
pinMode(i, INPUT_PULLUP);
pinMode(PIN_SYS_SWITCH_VALVE, OUTPUT);
delay(1000);
if (digitalRead(PIN_SYS_INDOOR_MODE) == HIGH)
is_indoor = true;
}
void loop() {
if (app_state == 4)
waitProcess();
else
dispHome();
}
void dispHome() {
if (app_state == 1) {
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Welcome! Yay");
delay(1000);
app_state = 2;
}
dispFoodOption();
}
void dispFoodOption() {
if (app_state == 2) {
byte is_set = LOW, i = 0, j = 0;
do {
if (digitalRead(PIN_SYS_OPTION_TOGGLE) == HIGH) {
i++;
if (i > 3) i = 0;
}
lcd.clear();
if (j == 0) {
lcd.setCursor(0,0);
lcd.print("25g 30g");
lcd.setCursor(0,1);
lcd.print("35g 40g");
j = 1;
} else {
switch(i) {
case 0:
break;
lcd.print(" 30g");
case 1:
lcd.print("25g ");
break;
case 2:
lcd.print(" 40g");
break;
case 3:
lcd.print("35g ");
break;
}
j = 0;
}
is_set = digitalRead(PIN_SYS_OPTION_SELECT);
if (is_set == HIGH)
food_gram_set = food_grams[i];
delay(250);
} while (is_set != HIGH);
app_state = 3;
}
dispTimeDelay();
}
void dispTimeDelay() {
if (app_state == 3) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("food="+ String(food_grams[food_gram_set]) +"g");
lcd.setCursor(0,1);
String text = "outdoor [8h]";
if (is_indoor) {
text = "indoor [8h]";
}
lcd.print(text);
app_state = 4;
}
}
void waitProcess() {
/**
* If there is food, IR sensor will send HIGH signal
* If there is no obstacle in its LOS (line-of-sight)
* send notif since no food left
*/
if (digitalRead(PIN_DETECT_LOW_FOOD) == LOW) {
sendNotify(NOTIF_BELOW_FOOD);
}
}
void sendNotify(byte notif_type) {
String text = "";
switch(notif_type) {
case NOTIF_BELOW_FOOD:
text = "Low food remaining";
break;
case NOTIF_BOWL_EMPTY:
text = "Dog emptied bowl";
break;
}
}