/* Routines associated with the finite state machine */ /*********************************************** the normal state returns the state ************************************************/ int run_state(int fsm_state) { int ret_val; int select_switch; // Serial.print("state= RUN_STATE "); Serial.println(fsm_state); select_switch = check_set(); if (select_switch == 0x07) { // all switches high then in normal run mode update_time(); ret_val = RUN_STATE; } else { // switch was moved, move to verify state ret_val = VERIFY_STATE; } return ret_val; } /*********************************************** Verify that the switch has stayed the same for a second or more returns the state ************************************************/ int verify_state(int fsm_state) { #define CNT_TRIGGER (10) int ret_val; static int count = 0; // counts how many times we have entered static int old_set = 0x00; // keeps track of what the switch was, rudenmentary debounce int new_set = 0x00; // new switch setting // Serial.print("state= VERIFY_STATE "); Serial.println(fsm_state); new_set = check_set(); if (old_set != new_set) { // switch was moved old_set = new_set; count = 0x00; // reset counter ret_val = new_set; // states are based on the switch } else { // switch has stayed the same increment counter if (count++ > CNT_TRIGGER) { // stayed the same for long enough, move to set state ret_val = new_set; count = 0x00; // reset counter } } return ret_val; } /*********************************************** we are in set mode adjust the time ************************************************/ int set_state(int fsm_state, tmElements_t tm) { int ret_val; int analog_value; int index; // Serial.print("state= SET_STATE "); Serial.println(fsm_state); if (check_set() != fsm_state) { // if the states are not the same then we are done ret_val = RUN_STATE; } else { analog_value = analogRead(SET_POT); // Serial.print (analog_value); Serial.print(" "); switch (fsm_state) { case SET_HOUR_STATE: index = 0x00; while(hour_segments[index++] <= analog_value); tm.Hour = (index-1); // A/D is 10 bit, so max is 1024 // Serial.print("Hours: "); // Serial.println(tm.Hour); analogWrite(HOURS_PIN, analog_value / 2); break; case SET_MIN_STATE: index = 0x00; while(min_sec_segments[index++] <= analog_value); tm.Minute = (index-1); // Serial.print("Minutes: "); // Serial.println(tm.Minute); analogWrite(MINUTES_PIN, analog_value>>2); break; case SET_SEC_STATE: index = 0x00; while(min_sec_segments[index++] <= analog_value); tm.Second = (index-1); // Serial.print("Seconds: "); // Serial.println(tm.Second); analogWrite(SECONDS_PIN, analog_value>>2); break; default: break; } RTC.write(tm); ret_val = fsm_state; } return ret_val; } /*********************************************** Check to see if we are in set mode ************************************************/ int check_set() { int select_switch = 0; select_switch = (digitalRead(HOUR_SW) << 2) + (digitalRead(MIN_SW) << 1) + digitalRead(SEC_SW); // Serial.print("Switch: "); Serial.print(select_switch); // Serial.print("Pot : "); Serial.println(analogRead(SET_POT)); // Serial.print(" "); return select_switch; }