#include <LiquidCrystal.h>
#include <LiquidMenu.h>
#include "RTClib.h"
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
RTC_DS1307 rtc;
//ENCODER
const int leftButtonPin = 14;
const int rightButtonPin = 15;
const int enterButtonPin = 16;
#define PIN_TRIG 4
#define PIN_ECHO 3
String petOptions[] = {"Dog", "Cat", "Hamster", "Bird"};
int weightOptions[] = {1, 2, 5, 10, 15, 20};
int feedTimesOptions[] = {1, 2, 3, 4, 5, 6};
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
String Mylabel ="";
long int LastMeal=0;
int Dif=10;
int _min;
int _seg;
int dist;
int n=0;
char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int Day;
int Month;
int Year;
int Secs;
int Minutes;
int Hours;
String dofweek; // day of week
String myDate;
String myTime;
//lineas pantalla 0
LiquidLine linea1_pant0(1, 0, "Real time");
LiquidLine linea2_pant0(1, 1, "Food Now");
LiquidLine linea3_pant0(1, 0, "Last meal");
LiquidLine linea4_pant0(1, 1, "Box Load");
LiquidLine linea5_pant0(1, 0, "Setup");
//lineas pantalla 5
LiquidLine linea1_pant5(1, 0, "Pet");
LiquidLine linea2_pant5(1, 1, "Weight");
LiquidLine linea3_pant5(1, 0, "Times a day");
LiquidLine linea4_pant5(1, 1, "< Back");
//declaracion de pantallas
LiquidScreen pantalla0;
LiquidScreen pantalla1;
LiquidScreen pantalla2;
LiquidScreen pantalla3;
LiquidScreen pantalla4;
LiquidScreen pantalla5;
LiquidMenu menu(lcd);
void setup() {
pinMode(leftButtonPin,INPUT_PULLUP);
pinMode(rightButtonPin,INPUT_PULLUP);
pinMode(enterButtonPin, INPUT_PULLUP);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.print(" Welcome to ");
lcd.setCursor(0, 1);
lcd.print(" Your Pet Feader ");
delay(3000);
lcd.clear();
lcd.print(" START SETUP ");
lcd.setCursor(0, 1);
lcd.print("Press Enter to->");
//añadir lineas a pantalla 0
pantalla0.add_line(linea1_pant0);
pantalla0.add_line(linea2_pant0);
pantalla0.add_line(linea3_pant0);
pantalla0.add_line(linea4_pant0);
pantalla0.add_line(linea5_pant0);
//añadir lineas a pantalla 5
pantalla5.add_line(linea1_pant5);
pantalla5.add_line(linea2_pant5);
pantalla5.add_line(linea3_pant5);
pantalla5.add_line(linea4_pant5);
//definir posicion del selector lineas de patalla 0
linea1_pant0.set_focusPosition(Position::LEFT);
linea2_pant0.set_focusPosition(Position::LEFT);
linea3_pant0.set_focusPosition(Position::LEFT);
linea4_pant0.set_focusPosition(Position::LEFT);
linea5_pant0.set_focusPosition(Position::LEFT);
//definir posicion del selector lineas de patalla 5
linea1_pant5.set_focusPosition(Position::LEFT);
linea2_pant5.set_focusPosition(Position::LEFT);
linea3_pant5.set_focusPosition(Position::LEFT);
linea4_pant5.set_focusPosition(Position::LEFT);
linea1_pant0.attach_function(1, fn_time);
linea2_pant0.attach_function(1, fn_fnow);
linea3_pant0.attach_function(1, fn_last);
linea4_pant0.attach_function(1, fn_dist);
linea5_pant0.attach_function(1, fn_ir_a_pantalla5);
linea1_pant5.attach_function(1, fn_pet);
linea2_pant5.attach_function(1, fn_W);
linea3_pant5.attach_function(1, fn_tad);
linea4_pant5.attach_function(1, fn_back);
menu.add_screen(pantalla0);
menu.add_screen(pantalla1);
menu.add_screen(pantalla2);
menu.add_screen(pantalla3);
menu.add_screen(pantalla4);
menu.add_screen(pantalla5);
pantalla0.set_displayLineCount(2);
pantalla1.set_displayLineCount(2);
pantalla2.set_displayLineCount(2);
pantalla3.set_displayLineCount(2);
pantalla4.set_displayLineCount(2);
pantalla5.set_displayLineCount(2);
delay(100);
while (digitalRead(enterButtonPin)==HIGH){
}
fn_ir_a_pantalla5();
menu.init();
menu.set_focusedLine(0);
menu.update();
}
void loop() {
selectOption();
if (digitalRead(rightButtonPin)==LOW){
menu.switch_focus(true);
}
if (digitalRead(leftButtonPin)==LOW){
menu.switch_focus(false);
}
delay(200);
if (millis()==8640000){ n=0; }
}
//Funciones:::::
void selectOption(){
if(digitalRead(enterButtonPin) == LOW){
delay(150);
menu.call_function(1);
}
}
void fn_ir_a_pantalla5(){
delay(200);
menu.change_screen(6);
menu.set_focusedLine(0);
}
void fn_back(){
menu.change_screen(1);
menu.set_focusedLine(0);
}
void fn_fnow(){
LastMeal=0;
delay(200);
lcd.clear();
lcd.setCursor(0,0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Meal Serving");
lcd.setCursor(0,1);
lcd.print("...");
fn_serve();
lcd.clear();
lcd.print("Meal Served");
LastMeal=millis();
//delay(3000);
}
void fn_last(){
delay(300);
lcd.clear();
while (digitalRead(enterButtonPin)==HIGH){
Dif=(millis()-LastMeal)/1000;
_min=Dif/60;
_seg=Dif%60;
lcd.setCursor(0,0);
lcd.print("Have passed");
lcd.setCursor(0,1);
lcd.print(String(_min)+"min "+String(_seg)+"seg ");
delay(300);
_min=0;
_seg=0;
}
}
void fn_time(){
rtc.begin();
while ((digitalRead(enterButtonPin) ==HIGH)){
DateTime now = rtc.now();
lcd.clear();
Day = now.day();
Month = now.month();
Year = now.year();
Secs = now.second();
Hours = now.hour();
Minutes = now.minute();
dofweek = daysOfTheWeek[now.dayOfTheWeek()];
myDate = myDate +dofweek+ " "+ Day + "/" + Month + "/" + Year ;
myTime = myTime + Hours +":"+ Minutes +":" + Secs ;
//Print on lcd
lcd.setCursor(0,0);
lcd.print(myDate);
lcd.setCursor(0,1);
lcd.print("Time:");
lcd.setCursor(6,1);
lcd.print(myTime);
myDate = "";
myTime = "";
delay(1000);
}
fn_back();
}
void fn_dist(){
lcd.clear();
while(digitalRead(enterButtonPin)==HIGH){
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
dist = pulseIn(PIN_ECHO, HIGH);
dist=dist* 0.034 / 2;
int per=0;
if (dist>36){per=0;}
if (dist>26 && dist<=36){per=25;}
if (dist>16 && dist<=26){per=50;}
if (dist>6 && dist<=16){per=75;}
if (dist<6){per=100;}
lcd.setCursor(0,0);
lcd.print("LEFT");
lcd.setCursor(0,1);
lcd.print(String(per)+"% ");
delay(200);
}
}
void fn_pet(){
lcd.clear();
while ((digitalRead(enterButtonPin)==HIGH)){
Mylabel="";
if(digitalRead(rightButtonPin)==LOW){
if(counter1==3){
counter1=0;
}else{
counter1++;
}
}
if(digitalRead(leftButtonPin)==LOW){
if(counter1==0){
counter1=3;
}else{
counter1--;
}
}
lcd.setCursor(0,0);
lcd.print("PETS");
Mylabel="<"+String(petOptions[counter1])+"> ";
lcd.setCursor(0,1);
lcd.print(Mylabel);
Mylabel="";
delay(200);
}
}
void fn_W(){
lcd.clear();
while ((digitalRead(enterButtonPin)==HIGH)){
Mylabel="";
if(digitalRead(rightButtonPin)==LOW){
if(counter2==5){
counter2=0;
}else{
counter2++;
}
}
if(digitalRead(leftButtonPin)==LOW){
if(counter2==0){
counter2=5;
}else{
counter2--;
}
}
lcd.setCursor(0,0);
lcd.print("WEIGHT");
Mylabel="<"+String(weightOptions[counter2])+" Kg> ";
lcd.setCursor(0,1);
lcd.print(Mylabel);
Mylabel="";
delay(200);
}
}
void fn_tad(){
lcd.clear();
while ((digitalRead(enterButtonPin)==HIGH)){
Mylabel="";
if(digitalRead(rightButtonPin)==LOW){
if(counter3==5){
counter3=0;
}else{
counter3++;
}
}
if(digitalRead(leftButtonPin)==LOW){
if(counter3==0){
counter3=5;
}else{
counter3--;
}
}
lcd.setCursor(0,0);
lcd.print("TIMES A DAY");
Mylabel="<"+String(feedTimesOptions[counter3])+" /per day> ";
lcd.setCursor(0,1);
lcd.print(Mylabel);
Mylabel="";
delay(200);
}
n=0;
}
void fn_serve(){
int duration;
int quant;
if (counter1==2 || counter1==3){ quant=10 ; }
if (counter1==0){ quant=21*counter2 - 11,25 ; }
if (counter1==1){ quant=55 ; }
duration=quant/counter3;
if (n<feedTimesOptions[counter3] && Dif>=10){
n++;
//atuar motor
delay(3000);
}else{
while ((digitalRead(enterButtonPin)==HIGH)){
lcd.clear();
delay(500);
lcd.setCursor(0,1);
lcd.print(" >Meal Denied< ");
delay(500);
}
fn_back();
}
}