#include <LiquidCrystal.h>
#include "RTClib.h"
#define UP 34
#define DOWN 35
#define OK 32
#define maxIndex 3
#define minIndex 0
#define DEBUG 1
#if DEBUG==1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
RTC_DS1307 rtc;
const int rs = 13, en = 12, d4 = 14, d5 = 27, d6 = 26, d7 = 25;
String namaBulan[12] = {"JAN","FEB","MAR","APR","MEI","JUN","JUL","AGS","SEP","OKT","NOV","DES"};
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte jam=0;
byte menit=0;
byte tanggal=0;
byte bulan=0;
byte detik=0;
byte tempDetik=0;
int tahun=0;
bool print=false;
bool inMenu=false;
byte indexMenu=0;
bool up();
bool down();
bool ok();
float readTemp();
float readHum();
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(UP, INPUT);
pinMode(DOWN, INPUT);
pinMode(OK, INPUT);
pinMode(19,INPUT);
lcd.begin(16,2);
if (! rtc.begin()) {
debugln("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
debugln("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
lcd.setCursor(0,0);
lcd.print("Irigasi");
delay(2000);
lcd.clear();
}
void loop() {
DateTime now = rtc.now();
jam=now.hour();
menit= now.minute();
tanggal=now.day();
detik=now.second();
String printBulan = namaBulan[now.month()-1];
tahun = now.year()%2000;
char buf[33];
if(detik>=tempDetik){
print=!print;
tempDetik=detik+1;
}
if(tempDetik>=59){tempDetik=0;}
if(print){
sprintf(buf,"%2d %02d %d-%s-%d", jam, menit, tanggal, printBulan, tahun);
}else{
sprintf(buf,"%2d:%02d %d-%s-%d", jam, menit, tanggal, printBulan, tahun);
}
lcd.setCursor(0,0);
lcd.print(buf);
//=============================MENU====================================
// if(ok() && !inMenu){
// lcd.clear();
// inMenu=true;
// }
while(inMenu){
if(up()){
if(indexMenu<maxIndex){indexMenu++;}
lcd.clear();
}
if(down()){
if(indexMenu>minIndex){indexMenu--;}
lcd.clear();
}
if(indexMenu==0){lcd.setCursor(0,0); lcd.print("1.Set Jadwal");}
if(indexMenu==1){lcd.setCursor(0,0); lcd.print("2.Set Jam");}
if(indexMenu==2){lcd.setCursor(0,0); lcd.print("3.Tes Output");}
if(indexMenu==3){lcd.setCursor(0,0); lcd.print("4.EXIT");}
// if(indexMenu==3 && ok()){
// delay(100);
// lcd.clear();
// indexMenu=0;
// inMenu=false;
// }
}
}
bool up(){
if(digitalRead(UP)==LOW){
// while(digitalRead(UP)==LOW){}
debugln("up true");
return true;
}
delay(50);
debugln("up false");
return false;
}
bool down(){
if(digitalRead(DOWN)==LOW){
// while(digitalRead(DOWN)==LOW){}
debugln("down true");
return true;
}
delay(50);
debugln("down true");
return false;
}
// bool ok(){
// if(digitalRead(OK)==LOW){
// // while(digitalRead(OK)==LOW){}
// debugln("OK true");
// return true;
// }
// delay(50);
// debugln("OK false");
// return false;
// }