Hello all,
I am unexperienced writing code, still learning from examples, but trying to broaden my horizon.
Chief amongst previous advice was "keep it simple". So i've build up a function to set and save 1 value to EEPROM. Now I want to set a time and duration and save that to EEPROM, so I've copied the code 3 times and added conditions to all of them so they don't mix eachother up. Over a 100 lines for something simple, and even worse I need to copy and paste the entire function 3 more times to get a total of 4 timers with duration.
I know about the TimeAlarm library but I want to save all values to EEPROM as a failsafe.
I feel like a monkey banging a wrench on the keyboard to make this work.
I'm am sure there must be beter or more elegant ways to write this.
Would anyone be willing to take a look and offer advice? Any help is greatly appreciated.
Here is what I have:
void setRain1Timer() {
int hour1Saved = 0, minute1Saved = 0, duration1Saved = 0, rain1Hr = 0, rain1Min = 0, rainTime1 = 0;
myScreen.fillScreen(BLACK);
myScreen.setCursor(0, 0);
myScreen.print("Rain Time 1:");
myScreen.setCursor(0, 10);
myScreen.print("ENTER ON TIME");
while (1) {
if (hour1Saved == 0 && minute1Saved == 0 && duration1Saved == 0 && joyButtonRead(pbUp) == HIGH) { // joystick up
rain1Hr++;
myScreen.setCursor(10, 20);
myScreen.print("hr ");
printDigits(rain1Hr);
delay(200);
if (rain1Hr == 24)
rain1Hr = 0;
}
if (hour1Saved == 0 && minute1Saved == 0 && duration1Saved == 0 && joyButtonRead(pbDown) == HIGH) { // joystick down
rain1Hr--;
myScreen.setCursor(10, 20);
myScreen.print("hr ");
printDigits(rain1Hr);
delay(200);
if (rain1Hr == 0)
rain1Hr = 23;
}
if (hour1Saved == 0 && minute1Saved == 0 && duration1Saved == 0 && joyButtonRead(pbRight) == HIGH) { // joystick right to save value
EEPROM.write(4, rain1Hr);
delay(1000);
hour1Saved = 1;
}
if (hour1Saved == 1 && minute1Saved == 0 && duration1Saved == 0 && joyButtonRead(pbUp) == HIGH) { // joystick up
rain1Min++;
myScreen.setCursor(40, 20);
myScreen.print(" min ");
printDigits(rain1Min);
delay(200);
if (rain1Min == 60)
rain1Min = 0;
}
if (hour1Saved == 1 && minute1Saved == 0 && duration1Saved == 0 && joyButtonRead(pbDown) == HIGH) { // joystick down
rain1Min--;
myScreen.setCursor(40, 20);
myScreen.print(" min ");
printDigits(rain1Min);
delay(200);
if (rain1Min == 0)
rain1Min = 59;
}
if (hour1Saved == 1 && minute1Saved == 0 && duration1Saved == 0 && joyButtonRead(pbRight) == HIGH) { // joystick right to save value
EEPROM.write(5, rain1Min);
delay(1000);
minute1Saved = 1;
}
if (hour1Saved == 1 && minute1Saved == 1 && duration1Saved == 0 && joyButtonRead(pbUp) == HIGH) { // joystick up
rainTime1++;
myScreen.setCursor(90, 20);
myScreen.print(" sec ");
printDigits(rainTime1);
delay(200);
if (rainTime1 == 60)
rainTime1 = 0;
}
if (hour1Saved == 1 && minute1Saved == 1 && duration1Saved == 0 && joyButtonRead(pbDown) == HIGH) { // joystick down
rainTime1--;
myScreen.setCursor(90, 20);
myScreen.print(" sec ");
printDigits(rainTime1);
delay(200);
if (rainTime1 == 0)
rainTime1 = 59;
}
if (hour1Saved == 1 && minute1Saved == 1 && duration1Saved == 0 && joyButtonRead(pbRight) == HIGH) { // joystick right to save value
EEPROM.write(6, rainTime1);
delay(1000);
duration1Saved = 1;
}
if (hour1Saved == 1 && minute1Saved == 1 && duration1Saved == 1) { // 3 values saved, print results
myScreen.setCursor(10, 30);
myScreen.print(" START TIME SET ");
myScreen.setCursor(10, 40);
myScreen.print("AT: ");
printDigits(EEPROM.read(5));
myScreen.print(":");
printDigits(EEPROM.read(6));
myScreen.print(":00");
myScreen.print(" ");
myScreen.setCursor(10, 50);
myScreen.print("DURATION: ");
printDigits(EEPROM.read(6));
myScreen.print(" SECONDS");
myScreen.setCursor(10, 60);
myScreen.print("SAVED");
delay(5000);
rain1Hr = 0;
rain1Min = 0;
hour1Saved = 0;
minute1Saved = 0;
duration1Saved = 0;
rainTime1 = 0;
break;
}
}
}
