#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonPin1 = 10;
const int buttonPin2 = 9;
const int buttonPin3 = 8;
int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if pressed
int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 500; // the debounce time; increase if the output flickers
int menuVar = 0;
int menuSel = 0;
bool aLarm = 0;
int aLarmhour = 12;
int aLarmmin = 1;
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.init();
lcd.backlight();
lcd.begin(16,2);
lcd.setCursor(0,0);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
lcd.print("COULDN'T FIND RTC");
while (1);
}
rtc.writeSqwPinMode(DS3231_OFF);
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(24,12,10,10,22,00));
pinMode(buttonPin1, INPUT); // these are wired with 1K PULL-DOWN resistors
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
lcd.print("GIANNA cats");
lcd.setCursor(0,1);
lcd.print("GENEVIEVE DAY");
delay(1000);
lcd.clear();
delay(500);
}
void loop() {
Serial.print("Loop ");
Serial.println(menuVar);
timeDisplay();
delay(500);
if (goatRope(digitalRead(buttonPin1))) {
delay(1000);
menuDisplay();
}
if (menuVar == 1) {alarmOnoff();}
if (menuVar == 2) {alarmSet();}
if (menuVar == 3) {timeSet();}
// if (menuVar == 4) {militarySet();}
}
void timeDisplay() {
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TIME: ");
if(now.hour()< 1) {lcd.print("24");}
else if (now.hour()>= 1) {lcd.print(now.hour(), DEC);}
lcd.print(":");
if (now.minute()< 10) {lcd.print("0");}
lcd.print(now.minute(), DEC);
lcd.print(":");
if (now.second()< 10) {lcd.print("0");}
lcd.print(now.second(), DEC);
if (aLarm) {
lcd.print(" *");
}
lcd.setCursor(0,1);
lcd.print("DATE: ");
lcd.print(now.month(),DEC);
lcd.print("/");
lcd.print(now.day(),DEC);
lcd.print("/");
lcd.print(now.year(),DEC);
}
void menuDisplay() {
int menuXit = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("***MAIN MENUyy**");
lcd.setCursor(0,1);
lcd.print("- 1 Time<*0>-");
delay(1000);
while (menuXit < 8) {
Serial.print("MenuDisp ");
Serial.println(menuVar);
if (goatRope(digitalRead(buttonPin2))){
menuVar+=1;
if (menuVar > 3) {menuVar = 0;}
secondMenu();
}
if (goatRope(digitalRead(buttonPin3))) {
menuVar-=1;
if (menuVar < 0) {menuVar = 3;}
secondMenu();
}
if (goatRope(digitalRead(buttonPin1))) {
menuXit = 9;
}
}
}
void secondMenu() {
if (menuVar == 0) {
lcd.setCursor(0,1);
lcd.print("- 1 Time -");
}
else if (menuVar == 1) {
lcd.setCursor(0,1);
lcd.print("-2 Alarm On/Off-");
}
else if (menuVar == 2) {
lcd.setCursor(0,1);
lcd.print("- 3 Alarm Set -");
}
else if (menuVar == 3) {
lcd.setCursor(0,1);
lcd.print("- 4 Time Set -");
}
delay(500);
}
void alarmOnoff() {
boolean oNofF = false;
while (!goatRope(digitalRead(buttonPin1))) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" ALARM STATUS: ");
delay(500);
if (goatRope(digitalRead(buttonPin2))) {
oNofF=(!oNofF);
lcd.setCursor(0,1);
lcd.print(" ");
if(oNofF==0) {
lcd.print("OFF");}
else if(oNofF==1){
lcd.print("ON ");}
delay(500);
}
if (goatRope(digitalRead(buttonPin3))) {
oNofF=(!oNofF);
lcd.setCursor(0,1);
lcd.print(" ");
if(oNofF==0) {
lcd.print("OFF");}
else if(oNofF==1){
lcd.print("ON ");}
delay(500);
}
lcd.setCursor(0,1);
lcd.print(" ");
if(oNofF==0) {
lcd.print("OFF");}
else if(oNofF==1){
lcd.print("ON ");}
delay(500);
}
delay(500);
aLarm = oNofF;
menuVar = 0;
}
void alarmSet() {
int locCounter = 1;
bool dispSwitch = 1;
int newhor = aLarmhour;
int newmin = aLarmmin;
while (locCounter < 4 ) {
if (goatRope(digitalRead(buttonPin1))) {locCounter+=1;}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" ALARM SET: ");
lcd.setCursor(0,1);
if (aLarmhour<1) {
aLarmhour = 12;
}
lcd.print(" ");
lcd.print(aLarmhour);
lcd.print(":");
if (aLarmmin<10) {lcd.print("0");}
lcd.print(aLarmmin);
switch (locCounter) {
case 2:
while (!goatRope(digitalRead(buttonPin1))) {
dispSwitch=(!dispSwitch);
if (goatRope(digitalRead(buttonPin2))) {newhor+=1;}
if (goatRope(digitalRead(buttonPin3))) {newhor-=1;}
if (newhor<0) {newhor = 23;}
if (newhor>23) {newhor = 0;}
lcd.setCursor(0,1);
if (dispSwitch) {
if (newhor < 10) {lcd.print(" ");}
lcd.print(" ");
lcd.print(newhor);
lcd.print(":");
if (aLarmmin<10) {lcd.print("0");}
lcd.print(aLarmmin);
}
else {
lcd.print(" ");
}
delay(500);
}
aLarmhour = newhor;
locCounter += 1;
break;
case 3:
while (!goatRope(digitalRead(buttonPin1))) {
dispSwitch=(!dispSwitch);
if (goatRope(digitalRead(buttonPin2))) {newmin+=1;}
if (goatRope(digitalRead(buttonPin3))) {newmin-=1;}
if (newmin > 59) {(newmin = 0);}
if (newmin < 0) {(newmin = 59);}
lcd.setCursor(0,1);
if (aLarmhour < 10) {lcd.print(" ");}
lcd.print(" ");
lcd.print(aLarmhour);
lcd.print(":");
if (dispSwitch) {
if (newmin<10) {lcd.print("0");}
lcd.print(newmin);
}
else {
lcd.print(" ");
}
delay(500);
}
aLarmmin = newmin;
locCounter +=1;
break;
}
delay(1000);
}
menuVar = 0;
}
void timeSet() {
DateTime now = rtc.now();
int locCounter = 1;
bool dispSwitch = 1;
int newhor = (now.hour());
int newmin = (now.minute());
int curyer = (now.year());
int curmon = (now.month());
int curday = (now.day());
while (locCounter < 4 ) {
if (goatRope(digitalRead(buttonPin1))) {locCounter+=1;}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" TIME SET: ");
lcd.setCursor(0,1);
if ((newhor) < 1) {
newhor = 12;
}
lcd.print(" ");
lcd.print(newhor);
lcd.print(":");
if (newmin<10) {lcd.print("0");}
lcd.print(newmin);
switch (locCounter) {
case 2:
while (!goatRope(digitalRead(buttonPin1))) {
dispSwitch=(!dispSwitch);
if (goatRope(digitalRead(buttonPin2))) {newhor+=1;}
if (goatRope(digitalRead(buttonPin3))) {newhor-=1;}
if (newhor<0) {newhor = 23;}
if (newhor>23) {newhor = 0;}
lcd.setCursor(0,1);
if (dispSwitch) {
if (newhor < 10) {lcd.print(" ");}
lcd.print(" ");
lcd.print(newhor);
lcd.print(":");
if (newmin<10) {lcd.print("0");}
lcd.print(newmin);
}
else {
lcd.print(" ");
}
delay(500);
}
locCounter += 1;
break;
case 3:
while (!goatRope(digitalRead(buttonPin1))) {
dispSwitch=(!dispSwitch);
if (goatRope(digitalRead(buttonPin2))) {newmin+=1;}
if (goatRope(digitalRead(buttonPin3))) {newmin-=1;}
if (newmin > 59) {(newmin = 0);}
if (newmin < 0) {(newmin = 59);}
lcd.setCursor(0,1);
if (newhor < 10) {lcd.print(" ");}
lcd.print(" ");
lcd.print(newhor);
lcd.print(":");
if (dispSwitch) {
if (newmin<10) {lcd.print("0");}
lcd.print(newmin);
}
else {
lcd.print(" ");
}
delay(500);
}
locCounter += 1;
break;
}
delay(1000);
}
rtc.adjust(DateTime(curyer,curmon,curday,newhor,newmin,00));
menuVar = 0;
}
bool goatRope(int buttonInput) { // Debounce function
bool goatrope = false;
int stateofgoat = -1;
//filter out any noise by setting a time buffer
if ( (millis() - lastDebounceTime) > debounceDelay) {
//if the button has been pressed, change the state
if ( (buttonInput == HIGH) && (stateofgoat < 0) ) {
goatrope = true;
stateofgoat = -stateofgoat;
lastDebounceTime = millis(); //set the current time
}
else if ( (buttonInput == HIGH) && (stateofgoat > 0) ) {
goatrope = false;
stateofgoat = -stateofgoat;
lastDebounceTime = millis(); //set the current time
}
} //Timing closeout
return goatrope; // Return true or false value of a button state
}