// This are the states the program can be in enum State { STARTUP, DISPLAY_TIME, CHANGE_TIME, DISPLAY_ALARM, CHANGE_ALARM, DISPLAY_DURATION, CHANGE_DURATION }; // The first state it STARTUP State state = STARTUP; // In the program only the nextState variable should be set. // The loop function then sets the state and does some more stuff. State nextState = state; // This is set when a state was changed in the last loop bool stateChanged = false; // run the function of the current state void runState() { switch(state) { case STARTUP: startup(); break; case DISPLAY_TIME: displayTime(); break; case CHANGE_TIME: stateChangeTime(); break; case DISPLAY_ALARM: displayAlarm(); break; case CHANGE_ALARM: changeAlarm(); break; case DISPLAY_DURATION: displayDuration(); break; case CHANGE_DURATION: changeDuration(); break; }; } // do nothing here void startup() { nextState = DISPLAY_TIME; } // This state displays the time void displayTime() { // If the button right is clicked go to the state DISPLAY_ALARM if(buttonClicked[BTN_RIGHT]) nextState = DISPLAY_ALARM; // If the button down was pressed long go to the state CHANGE_TIME if(buttonLongPressed[BTN_DOWN]) nextState = CHANGE_TIME; // show the time showTime(now(), -1, 0); } void stateChangeTime() { if(stateChanged == true) // Time to change is set to the current time, when the user has finished changing // the time, the timeToChange variable changed. timeToChange = now(); // change time returns true when the user finished changing the time if(changeTime()) { // set the current time to the time the user just set setTime(timeToChange); // go back to DISPLAY_TIME nextState = DISPLAY_TIME; } } void displayAlarm() { if(buttonClicked[BTN_RIGHT]) nextState = DISPLAY_DURATION; if(buttonLongPressed[BTN_DOWN]) nextState = CHANGE_ALARM; if(stateChanged == true) { showTime(alarmTime, -1, 42); } } void changeAlarm() { if(stateChanged == true) timeToChange = alarmTime; if(changeTime(42)) { alarmTime = timeToChange; Alarm.free(alarmID); alarmID = Alarm.alarmRepeat(hour(alarmTime), minute(alarmTime), second(alarmTime), alarm); nextState = DISPLAY_ALARM; } } void displayDuration() { if(buttonClicked[BTN_RIGHT]) nextState = DISPLAY_TIME; if(buttonLongPressed[BTN_DOWN]) nextState = CHANGE_DURATION; display.setTextSize(1); display.setCursor(0, 4); display.print("Duration: "); display.setTextSize(2); display.setCursor(25, 18); display.print(String(sunriseDuration)); } void changeDuration() { if(buttonClicked[BTN_DOWN]) sunriseDuration--; if(buttonClicked[BTN_UP]) sunriseDuration++; if(buttonClicked[BTN_RIGHT]) nextState = DISPLAY_DURATION; display.clearDisplay(); display.setTextSize(1); display.setCursor(0, 4); display.print("Duration: "); if(blinkOn) { display.setTextSize(2); display.setCursor(25, 18); display.print(String(sunriseDuration)); } }