#define Aa 0 // x0:00:00 //range 0-1 //use Chars1

#define Ba 1 // 0x:00:00 //range 0-9 //use Chars4
#define Bb 2
#define Bc 3
#define Bd 4

#define Ca 5 // 00:x0:00 //range 0-7 //use Chars3
#define Cb 6
#define Cc 7

#define Da 8 // 00:0x:00 //range 0-9 //use Chars4
#define Db 9
#define Dc 10
#define Dd 11

#define Ea 12 // 00:00:x0  //range 0-7 //use Chars 3
#define Eb 13
#define Ec 14                                        //A0pin

#define Fa 15 // 00:00:0x  //range 0-9 //use Chars 4 //A1pin
#define Fb 16                                        //A2pin
#define Fc 17                                        //A3pin
#define Fd 18                                        //A4pin

#define Key_sc 19                                    //A5pin
#define Key_mn 20                                    //A6pin
#define Key_hr 21                                    //A7pin



#define common_cathode 0
#define common_anode 1

bool segMode = common_cathode;

unsigned long currentTime;

unsigned long long hr12 = 0; //will store value;how many "12hours" millis has

int hr = 0;
int mn = 0;
int sc = 0;

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;

int hr_add = 0;
int mn_add = 0;
int sc_add = 0;

int hrx0[] {Aa};             // x0:00:00
int hr0x[] {Ba, Bb, Bc, Bd}; // 0x:00:00
int mnx0[] {Ca, Cb, Cc};     // 00:x0:00
int mn0x[] {Da, Db, Dc, Dd}; // 00:0x:00
int scx0[] {Ea, Eb, Ec};     // 00:00:x0
int sc0x[] {Fa, Fb, Fc, Fd}; // 00:00:0x

byte chars4 = 11;
byte Chars4[11][5] {
  //{input,D,C,B,A}
  {'0', 0, 0, 0, 0},
  {'1', 0, 0, 0, 1},
  {'2', 0, 0, 1, 0},
  {'3', 0, 0, 1, 1},
  {'4', 0, 1, 0, 0},
  {'5', 0, 1, 0, 1},
  {'6', 0, 1, 1, 0},
  {'7', 0, 1, 1, 1},
  {'8', 1, 0, 0, 0},
  {'9', 1, 0, 0, 1},
};

byte chars3 = 6;
byte Chars3[6][4] {
  //{input,C,B,A}
  {'0', 0, 0, 0},
  {'1', 0, 0, 1},
  {'2', 0, 1, 0},
  {'3', 0, 1, 1},
  {'4', 1, 0, 0},
  {'5', 1, 0, 1},
};

byte chars1 = 2;
byte Chars1[2][2] {
  //{input,A}
  {'0', 0},
  {'1', 1}
};



void setup() {
  pinMode(hrx0[0], OUTPUT);
  pinMode(hr0x[0], OUTPUT);
  pinMode(hr0x[1], OUTPUT);
  pinMode(hr0x[2], OUTPUT);
  pinMode(hr0x[3], OUTPUT);
  pinMode(mnx0[0], OUTPUT);
  pinMode(mnx0[1], OUTPUT);
  pinMode(mnx0[2], OUTPUT);
  pinMode(mn0x[0], OUTPUT);
  pinMode(mn0x[1], OUTPUT);
  pinMode(mn0x[2], OUTPUT);
  pinMode(mn0x[3], OUTPUT);
  pinMode(scx0[0], OUTPUT);
  pinMode(scx0[1], OUTPUT);
  pinMode(scx0[2], OUTPUT);
  pinMode(sc0x[0], OUTPUT);
  pinMode(sc0x[1], OUTPUT);
  pinMode(sc0x[2], OUTPUT);
  pinMode(sc0x[3], OUTPUT);
  pinMode(Key_sc, INPUT);
  pinMode(Key_mn, INPUT);
  pinMode(Key_hr, INPUT);
}

void loop() {
  //keeping millis value within 4320000;12hours
  currentTime = millis();
  if (currentTime > 4320000) {
    hr12 = floor(currentTime / 4320000);
    for (; hr12 == 0; hr12--) {
      currentTime = currentTime - 4320000;
    }
  } else {
    currentTime = millis();
  }
  
  //button input to adjust time prior setting hr:mn:sc value
  //to add those adjustment when producing hr:mn:sc value
  if(digitalRead(Key_sc)==HIGH){
    //saving currentTime sc value, will be substrated on currentTime
    sc_add = (floor(currentTime / 1000) - floor(currentTime / 60 / 1000) * 60)*1000;
    delay(100);
  }
  if(digitalRead(Key_mn)==HIGH){
    mn_add++;
    delay(100);
  }
  if(digitalRead(Key_hr)==HIGH){
    hr_add++;
    delay(100);
  }

  //using floor(), deleting any value below it's own time unit
  sc = floor((currentTime - sc_add) / 1000) - floor((currentTime-sc_add) / 60 / 1000) * 60;
  mn = floor((currentTime - sc_add + mn_add*60000) / 60 / 1000) - floor((currentTime - sc_add + mn_add*60000) / 60 / 60 / 1000) * 60;
  hr = floor((currentTime - sc_add + mn_add*60000 + hr_add*36000000)/ 60 / 60 / 1000) - floor((currentTime - sc_add + mn_add*60000 + hr_add*36000000) / 12 / 60 / 60 / 1000) * 12;


  //ab:cd:ef //00:00:00
  a = floor(hr / 10);
  b = hr - a * 10;
  c = floor(mn / 10);
  d = mn - c * 10;
  e = floor(sc / 10);
  f = sc - e * 10;

  for(int i=0;i<4;i++){
    digitalWrite(mn0x[i],Chars4[d][i+1]);
    digitalWrite(hr0x[i],Chars4[b][i+1]);
    digitalWrite(sc0x[i],Chars4[f][i+1]);
  }
  for(int i=0;i<3;i++){
    digitalWrite(mnx0[i],Chars3[c][i+1]);
    digitalWrite(scx0[i],Chars3[e][i+1]);
  }
  digitalWrite(hrx0[0],Chars1[a][1]);

  a = 0;
  b = 0;
  c = 0;
  d = 0;
  e = 0;
  f = 0;
  currentTime = 0;
  sc = 0;
  mn = 0;
  hr = 0;
}