// By Nissanka MD COTM

  #define pinDp 2
  #define pinC  3
  #define pinD  4
  #define pinE  5
  #define pinG  6
  #define pinF  7
  #define pinA  8
  #define pinB  9
  #define pinComDig1  12
  #define pinComDig2  13
  #define pinComDig3  10
  #define pinComDig4  11

  #define num1DispDuration 1000
  #define num2DispDuration 2000
  #define num3DispDuration 500
  #define num4DispDuration 1500
  #define delayDigitDisplay 30


  bool commonCathod = true;
  bool enableDisplay;
  bool disableDisplay;

  bool A;
  bool B;
  bool C;
  bool D;
  bool E;
  bool F;
  bool G;
  bool Dp;
  bool ComDig1;
  bool ComDig2;
  bool ComDig3;
  bool ComDig4;
  
  unsigned long Dig1ticker;
  unsigned long Dig2ticker;
  unsigned long Dig3ticker;
  unsigned long Dig4ticker;

  //int delayDigitDisplay = 30;

  void writeDisplay(){
    digitalWrite(pinComDig1,commonCathod);
    digitalWrite(pinComDig2,commonCathod);
    digitalWrite(pinComDig3,commonCathod);
    digitalWrite(pinComDig4,commonCathod);
    digitalWrite(pinA,A);
    digitalWrite(pinB,B);
    digitalWrite(pinC,C);
    digitalWrite(pinD,D);
    digitalWrite(pinE,E);
    digitalWrite(pinF,F);
    digitalWrite(pinG,G);
    digitalWrite(pinDp,Dp);
    digitalWrite(pinComDig1,ComDig1);
    digitalWrite(pinComDig2,ComDig2);
    digitalWrite(pinComDig3,ComDig3);
    digitalWrite(pinComDig4,ComDig4);

  }


  void allOff(){
    A=LOW;
    B=LOW;
    C=LOW;
    D=LOW;
    E=LOW;
    F=LOW;
    G=LOW;
    Dp=LOW;
    //writeDisplay();
  }

  void allOn(){
    A=HIGH;
    B=HIGH;
    C=HIGH;
    D=HIGH;
    E=HIGH;
    F=HIGH;
    G=HIGH;
    //Dp=HIGH;
    //writeDisplay();
  }

  void allOnWrite(){
    allOn();

  }

  void allOffWrite(){
    allOff();

  }

  void n1(){
    allOff();
    B=HIGH;
    C=HIGH;

  }

  void n2(){
    allOn();
    C=LOW;
    F=LOW;

  }

  void n3(){
    allOn();
    E=LOW;
    F=LOW;

  }

  void n4(){
    allOn();
    A=LOW;
    D=LOW;
    E=LOW;

  }

  void n5(){
    allOn();
    B=LOW;
    E=LOW;

  }

  void n6(){
    allOn();
    B=LOW;

  }

  void n7(){
    allOff();
    A=HIGH;
    B=HIGH;
    C=HIGH;

  }

  void n8(){
    allOn();

  }

  void n9(){
    allOn();
    E=LOW;

  }

  void n0(){
    allOn();
    G=LOW;

  }

  void Err(){
    allOn();
    B=LOW;
    C=LOW;

  }



  void displayDigit(int digit){

    allOffWrite();

    switch(digit){
      
      case 0:
        n0();
      break;

      case 1:
        n1();
      break;

      case 2:
        n2();
      break;

      case 3:
        n3();
      break;

      case 4:
        n4();
      break;

      case 5:
        n5();
      break;

      case 6:
        n6();
      break;

      case 7:
        n7();
      break;

      case 8:
        n8();
      break;

      case 9:
        n9();
      break;

      default:
      Err();
      break;

    }

  }

  void displayNumber(int Dig1,int Dig2, int Dig3, int Dig4){

    displayDigit(Dig1);
    ComDig1 = enableDisplay; // on display1
    writeDisplay();
    delay(delayDigitDisplay);
    ComDig1 = disableDisplay; //off display1
    //digitalWrite(pinComDig1,ComDig1);

    displayDigit(Dig2);
    ComDig2 = enableDisplay; // on display2
    writeDisplay();
    delay(delayDigitDisplay);
    ComDig2 = disableDisplay; //off display2
    //digitalWrite(pinComDig2,ComDig2);

    displayDigit(Dig3);
    ComDig3 = enableDisplay; // on display3
    writeDisplay();
    delay(delayDigitDisplay);
    ComDig3 = disableDisplay; //off display3
    //digitalWrite(pinComDig3,ComDig3);
  
    displayDigit(Dig4);
    ComDig4 = enableDisplay; // on display4
    writeDisplay();
    delay(delayDigitDisplay);
    ComDig4 = disableDisplay; //off display4
    digitalWrite(pinComDig4,ComDig4);

  }
  
   
    
    
    
  



  void setup() {
  for (int i=2; i<14; i++){
          pinMode(i,OUTPUT);
      }
  enableDisplay = !commonCathod;
  disableDisplay = commonCathod;

  ComDig1 = disableDisplay;
  ComDig2 = disableDisplay;
  ComDig3 = disableDisplay;
  ComDig4 = disableDisplay;

  Dig1ticker = millis();
  Dig2ticker = Dig1ticker + num1DispDuration;
  Dig3ticker = Dig2ticker + num2DispDuration;
  Dig4ticker = Dig3ticker + num3DispDuration;

  }

  void loop() {
    // put your main code here, to run repeatedly:

    if(millis() - Dig1ticker < num1DispDuration){
      displayNumber(4,3,9,7);

    }
   
    if(millis() - Dig2ticker < num2DispDuration){
        displayNumber(1,5,0,8);
        
    }

    if(millis() - Dig3ticker < num3DispDuration){
      displayNumber(3,0,2,7);
    }

    if(millis() - Dig4ticker < num4DispDuration){
      displayNumber(8,1,3,4);
    }
   
    if(millis() > num4DispDuration + Dig4ticker){
      Dig1ticker = millis();
      Dig2ticker = Dig1ticker + num1DispDuration;
      Dig3ticker = Dig2ticker + num2DispDuration;
      Dig4ticker = Dig3ticker + num3DispDuration; 
    }

       
  
    
      

  }