#include #include #include #include #include // Defines #define HOURS_PIN (9) #define MINUTES_PIN (10) #define SECONDS_PIN (11) #define UP_PIN #define DOWN_PIN #define PIXEL_PIN (5) #define NUMPIXELS (8) #define HOUR_LED (0) #define AM_LED (1) #define MIN_LED (2) #define SEC_LED (3) #define PWR_LED (4) #define HOUR_SW (6) #define MIN_SW (7) #define SEC_SW (8) #define SET_POT (A0) #define RUN_STATE (0x7) // based on switches, they are active low 111b #define VERIFY_STATE (0x20) #define SET_HOUR_STATE (0x03) // based on switches, they are active low 011b #define SET_MIN_STATE (0x05) // based on switches, they are active low 101b #define SET_SEC_STATE (0x06) // based on switches, they are active low 010b #define STEPS_PER_HOUR (1024/24) #define STEPS_PER_MIN ((1024/59)-1) #define STEPS_PER_SEC (STEPS_PER_MIN) unsigned int hour_segments[24] = {43,85,128,171,213,256,300,341,384,427,469,512,554,597,640,683,725,768,811,853,896,939,981,1024}; unsigned int min_sec_segments[60] = {17,34,51,68,85,102,119,136,153,170,187,204,221,238,256,273,290,307,324,341,358,375,392,409,426,443,460,477,494,512,529,546,563,580,597,614,631,648,665,682,699,716,733,750,768,785,802,819,836,853,870,887,904,921,938,955,972,989,1006,1024}; Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800); tmElements_t tm; bool AM = false; int set_pot_value = 0; int fsm_state = RUN_STATE; /* -------------------------------------------- Setup function -------------------------------------------- */ void setup() { Serial.begin(57600); while (!Serial) ; // wait for serial delay(200); Serial.println("Dave's old school clock"); Serial.println("-------------------"); pinMode(HOURS_PIN, OUTPUT); // sets the pin as output pinMode(MINUTES_PIN, OUTPUT); // sets the pin as output pinMode(SECONDS_PIN, OUTPUT); // sets the pin as output pinMode(HOUR_SW, INPUT_PULLUP); // sets the pin as output pinMode(MIN_SW, INPUT_PULLUP); // sets the pin as output pinMode(SEC_SW, INPUT_PULLUP); // sets the pin as output pixels.begin(); // This initializes the NeoPixel library. // DNK HACK HACK // remove this later Serial.println("Setting Time in setup"); set_default_time(tm); } /* --------------------------------------------- main loop The whole thing is run off of a finite state machine. RUN_STATE = This is the normal operation mode Check if setting switch is not in run mode if not switch to VERIFY_STATE Check RTC for time Update meters Update LEDs VERIFY_STATE = Checks to see if the set switch has stayed the same for 1 second If so Change the power led to RED, goto set state associated with switch settings SET_HOUR_STATE = read switch to make sure still in the setting mode, if not save hour and move to next state read POT, set time to reading SET_MIN_STATE = same as above SET_SEC_STATE = same as above --------------------------------------------- */ void loop() { static int count = 0; switch (fsm_state) { case RUN_STATE: fsm_state = run_state(fsm_state); break; case VERIFY_STATE: fsm_state = verify_state(fsm_state); break; case SET_HOUR_STATE: case SET_MIN_STATE: case SET_SEC_STATE: fsm_state = set_state(fsm_state, tm); break; default: // whoa! why did we get here? reset the state machine fsm_state = RUN_STATE; break; } // update pixels everytime through the loop updatePixels( fsm_state, is_am()); delay(100); } /* ---------------------------------------------------------------------- All other fun stuff ---------------------------------------------------------------------- */