#include <TM1637Display.h>
#include <Key.h>
#include <Keypad.h>

String str="";
String MIN,SEC;
int i=0,mins,secs,times1,times2,times3,times4,clear=0,g=0;
uint8_t num[]={0x3f,0x3f,0x3f,0x3f};
uint8_t number[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,};
byte rowPins[]={4,5,6,7};
byte colPins[]={8,9,10,11};
char KEYS[4][4]={{'1','2','3','A'},
                 {'4','5','6','B'},
                                  {'7','8','9','C'},
                                                   {'*','0','#','D'}
                                                                  };
                                                                  Keypad K_SW(makeKeymap(KEYS),rowPins,colPins,4,4);
                                                                  TM1637Display dis7(12, 13);

                                                                  void setup() {
                                                                    // put your setup code here, to run once:

                                                                        dis7.setBrightness(7);
                                                                          dis7.setSegments(num);

                                                                            
                                                                            }

                                                                            void loop() {
                                                                              // put your main code here, to run repeatedly:
                                                                                char keyPress = K_SW.getKey();
                                                                                  if(keyPress != NO_KEY && keyPress != '*' && keyPress != '#'){
                                                                                      str += keyPress;
                                                                                          num[i]=number[keyPress-48];
                                                                                              dis7.setSegments(num);
                                                                                                  i++;
                                                                                                    }
                                                                                                      if(keyPress=='*'){
                                                                                                          clear=0;
                                                                                                              for(int j=0;j<=4;j++){
                                                                                                                     num[j]=0x3f,0x3f,0x3f,0x3f;
                                                                                                                            dis7.setSegments(num);
                                                                                                                                   i=0;
                                                                                                                                          str="";
                                                                                                                                                
                                                                                                                                                       
                                                                                                                                                           }
                                                                                                                                                             }

                                                                                                                                                               if(keyPress=='#'){
                                                                                                                                                                   for(int t=0;t<2;t++){
                                                                                                                                                                         MIN += str[t];
                                                                                                                                                                             }
                                                                                                                                                                                 for(int t=2;t<=4;t++){
                                                                                                                                                                                       SEC += str[t];
                                                                                                                                                                                           }
                                                                                                                                                                                               
                                                                                                                                                                                                   mins = MIN.toInt();
                                                                                                                                                                                                       secs = SEC.toInt();
                                                                                                                                                                                                           for(int s=mins;s>=0;s--){
                                                                                                                                                                                                                 for(int k=secs;k>=0;k--){
                                                                                                                                                                                                                         times1 = k/10;
                                                                                                                                                                                                                                 times2 = k%10;
                                                                                                                                                                                                                                         times3 = s/10;
                                                                                                                                                                                                                                                 times4 = s%10;
                                                                                                                                                                                                                                                         num[2] = number[times1];
                                                                                                                                                                                                                                                                 num[3] = number[times2];
                                                                                                                                                                                                                                                                         num[0] = number[times3];
                                                                                                                                                                                                                                                                                 num[1] = number[times4];
                                                                                                                                                                                                                                                                                         dis7.setSegments(num);
                                                                                                                                                                                                                                                                                                 delay(500);
                                                                                                                                                                                                                                                                                                         secs = 59;
                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                                                                              clear = 1; 
                                                                                                                                                                                                                                                                                                                                  }
                                                                                                                                                                                                                                                                                                                                       

                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                              if(clear==1){
                                                                                                                                                                                                                                                                                                                                                      i=0;
                                                                                                                                                                                                                                                                                                                                                              str="";
                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                              MIN="";
                                                                                                                                                                                                                                                                                                                                                                                      SEC="";
                                                                                                                                                                                                                                                                                                                                                                                          dis7.setSegments(num); 
                                                                                                                                                                                                                                                                                                                                                                                                  num[0] = number[ 0 ];
                                                                                                                                                                                                                                                                                                                                                                                                          num[1] = number[0];
                                                                                                                                                                                                                                                                                                                                                                                                                  num[2] = number[0];
                                                                                                                                                                                                                                                                                                                                                                                                                          num[3] = number[0];
                                                                                                                                                                                                                                                                                                                                                                                                                                  delay(500);
                                                                                                                                                                                                                                                                                                                                                                                                                                          num[0] = 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                  num[1] = 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                          num[2] =0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                  num[3] =0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                          delay(500);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   dis7.setSegments(num); 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      }
4-Digit Display
4-Digit Display