//This is a clock program. The clock in this code counts up from 00:00 to 99:59. There is a RESET, PAUSE and LOAD functionality incorporated into the clock. //The range of the clock( the time it should count before it resets can be altered in the code. Also it can very easily be turned into a down counter by changing lines 101 and 105 to decrement. #include #define CLK 2//Pins for TM1637 #define DIO 3 #define PAUSE 4 #define RESET 5 #define LOAD 6 #define ANALOG_INPUT A0 int min0, min1, min2, sec0, sec1, sec2; int ctrl_pause= 0, ctrl_reset= 0, ctrl_load = 0, one_second_count = 0; int pause_clock = 0; int initial_value = 0; int load_clock = 0; SevenSegmentTM1637 game_clock(CLK,DIO); void setup(){ Serial.begin(9600); game_clock.init(); game_clock.on(); //BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7; pinMode(PAUSE,INPUT); pinMode(RESET,INPUT); pinMode(LOAD,INPUT); game_clock.setCursor(0,0); game_clock.print(1); game_clock.setCursor(0,1); game_clock.print(2); game_clock.setCursor(0,2); game_clock.print(3); game_clock.setCursor(0,3); game_clock.print(4); game_clock.setColonOn(true); delay(1500);//Delay to let system boot }//end "setup()" void loop() { if(digitalRead(RESET) == HIGH) { ctrl_reset = HIGH; } if(digitalRead(RESET) == LOW and ctrl_reset == HIGH) { min0 = 0; sec0 = 0; pause_clock = 0; ctrl_reset = LOW; } if(digitalRead(LOAD) == HIGH) { ctrl_load = HIGH; } if(digitalRead(LOAD) == LOW and ctrl_load == HIGH) { initial_value = analogRead(ANALOG_INPUT); load_clock = 5.858 * initial_value; sec0 = load_clock%60; min0 = load_clock/60; ctrl_load = LOW; pause_clock = 0; } if(digitalRead(PAUSE) == HIGH) { ctrl_pause = HIGH; } if(digitalRead(PAUSE) == LOW and ctrl_pause == HIGH) { if(pause_clock == 0) { pause_clock = 1; } else { pause_clock = 0; } ctrl_pause = LOW; } if(pause_clock == 1) { one_second_count++; if(one_second_count == 90) { sec0++; if(sec0 == 60) { sec0 = 0; min0++; } one_second_count = 0; } } sec1 = sec0%10; sec2 = (sec0/10)%10; min1 = min0%10; min2 = (min0/10)%10; game_clock.setCursor(0,0); game_clock.print(min2); game_clock.setCursor(0,1); game_clock.print(min1); game_clock.setCursor(0,2); game_clock.print(sec2); game_clock.setCursor(0,3); game_clock.print(sec1); //Serial.print("one_second_count = "); //Serial.println(one_second_count); //Serial.print("sec0 = "); //Serial.println(sec0); //Serial.print("sec1 = "); //Serial.println(sec1); //Serial.print("sec2 = "); //Serial.println(sec2); //Serial.print("min0 = "); //Serial.println(min0); //Serial.print("min1 = "); //Serial.println(min1); //Serial.print("min2 = "); //Serial.println(min2); delay(10); }