//SSD is Seven-Segment Display

int signal1[] = {46, 45, 44};
int signal2[] = {43, 42, 41};
int signal3[] = {40, 39, 38};
int signal4[] = {47, 48, 49};

int sidewalk1[] = {25,24,37,36,33,32,29,28};
int sidewalk2[] = {23,22,35,34,31,30,27,26};

int redDelay = 5000;
int yellowDelay = 2000;


const int START = A0;
const int STOP = A1;

/*
#define LED1 22
#define LED2 23
#define LED3 24
#define LED4 25
#define LED5 26
#define LED6 27
#define LED7 28
#define LED8 29
#define LED9 30
#define LED10 31
#define LED11 32
#define LED12 33
#define LED13 34
#define LED14 35
#define LED15 36 
#define LED16 37
#define LED17 38
#define LED18 39
#define LED19 40
#define LED20 41
#define LED21 42 
#define LED22 43 
#define LED23 44
#define LED24 45
#define LED25 46
#define LED26 47
#define LED27 48
#define LED28 49
*/

void(* resetFunc) (void) = 0;

void setup()
{



  pinMode(START,INPUT);
  pinMode(STOP,INPUT);


  for (int i = 0; i < 8; i++) {
    pinMode(signal1[i], OUTPUT);
    pinMode(signal2[i], OUTPUT);
    pinMode(signal3[i], OUTPUT);
    pinMode(signal4[i], OUTPUT);
    pinMode(sidewalk1[i], OUTPUT);
    pinMode(sidewalk2[i], OUTPUT);
  }



  for (int i = 0; i <= 13; i++)
  {
    pinMode(i, OUTPUT); 
  }
   for (int z = 0; z <= 13; z++)
  {
    digitalWrite(z,HIGH);
  }

  digitalWrite(START, LOW);  
  digitalWrite(STOP, HIGH);  

}

const int number[11] = {0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010, 0b0000010, 0b1111000, 0b0000000, 0b0010000};



void loop()
{
  if(digitalRead(START) == LOW && digitalRead(STOP) == HIGH)
  {
    start();
    delay(1);
    second();
    delay(1);
    resetFunc();
  }
  
  if(digitalRead(START) == LOW && digitalRead(STOP) == LOW)
  {
    resetFunc();
  }

}


void start()
{
  for (int tens = 5; tens > 0; tens--)
  {
    digitalWrite(signal2[2], HIGH);  
    digitalWrite(signal4[2], HIGH);  
    digitalWrite(signal1[0], HIGH);  
    digitalWrite(signal3[0], HIGH);   
    
    digitalWrite(sidewalk1[1], HIGH);  
    digitalWrite(sidewalk1[3], HIGH);  
    digitalWrite(sidewalk1[5], HIGH);  
    digitalWrite(sidewalk1[7], HIGH); 
    digitalWrite(sidewalk2[1], HIGH);  
    digitalWrite(sidewalk2[3], HIGH);  
    digitalWrite(sidewalk2[4], HIGH);  
    digitalWrite(sidewalk2[7], HIGH); 
    display_tens(tens);
  }

  for (int i = 0; i <= 6; i++)
  {
    pinMode(i, LOW); 
  }

  for (int nine = 9; nine > 5; nine--)
  {
    display_ones(nine);
    delay(1000);
  }


  for (int tens = 5; tens > 0; tens--)
  {
    digitalWrite(signal2[2], LOW);  
    digitalWrite(signal4[2], LOW);  
    digitalWrite(signal1[0], LOW);  
    digitalWrite(signal3[0], LOW); 
    digitalWrite(signal2[1], HIGH);  
    digitalWrite(signal4[1], HIGH);  
    digitalWrite(signal1[1], HIGH);  
    digitalWrite(signal3[1], HIGH);  
    display_ones(tens);
    delay(1000);
  }

}


void second()
{

   for (int i = 0; i <= 6; i++)
  {
    pinMode(i, HIGH); 
  }

  for (int tens = 5; tens > 0; tens--)
  {
    digitalWrite(signal2[0], HIGH);  
    digitalWrite(signal4[0], HIGH);  
    digitalWrite(signal1[2], HIGH);  
    digitalWrite(signal3[2], HIGH); 
    digitalWrite(signal2[1], LOW);  
    digitalWrite(signal4[1], LOW);  
    digitalWrite(signal1[1], LOW);  
    digitalWrite(signal3[1], LOW);  

    digitalWrite(sidewalk1[1], LOW);  
    digitalWrite(sidewalk1[3], LOW);  
    digitalWrite(sidewalk1[5], LOW);  
    digitalWrite(sidewalk1[7], LOW); 
    digitalWrite(sidewalk2[1], LOW);  
    digitalWrite(sidewalk2[3], LOW);  
    digitalWrite(sidewalk2[4], LOW);  
    digitalWrite(sidewalk2[7], LOW); 
    
    digitalWrite(sidewalk1[0], HIGH);  
    digitalWrite(sidewalk1[2], HIGH);  
    digitalWrite(sidewalk1[4], HIGH);  
    digitalWrite(sidewalk1[6], HIGH); 
    digitalWrite(sidewalk2[0], HIGH);  
    digitalWrite(sidewalk2[2], HIGH);  
    digitalWrite(sidewalk2[5], HIGH);  
    digitalWrite(sidewalk2[6], HIGH); 
    display_tens(tens);
  }

  for (int i = 0; i <= 6; i++)
  {
    pinMode(i, LOW); 
  }

  for (int nine = 9; nine > 5; nine--)
  {
    display_ones(nine);
    delay(1000);
  }


  for (int tens = 5; tens > 0; tens--)
  {
    digitalWrite(signal2[0], LOW);  
    digitalWrite(signal4[0], LOW);  
    digitalWrite(signal1[2], LOW);  
    digitalWrite(signal3[2], LOW);   
    digitalWrite(signal2[2], LOW);  
    digitalWrite(signal4[2], LOW);  
    digitalWrite(signal1[0], LOW);  
    digitalWrite(signal3[0], LOW); 
    digitalWrite(signal2[1], HIGH);  
    digitalWrite(signal4[1], HIGH);  
    digitalWrite(signal1[1], HIGH);  
    digitalWrite(signal3[1], HIGH);  
    display_ones(tens);
    delay(1000);
  }

  
  for (int i = 0; i < 8; i++) {
    pinMode(signal1[i], LOW);
    pinMode(signal2[i], LOW);
    pinMode(signal3[i], LOW);
    pinMode(signal4[i], LOW);
    pinMode(sidewalk1[i], LOW);
    pinMode(sidewalk2[i], LOW);
  }

}

void stop()
{
    for (int i = 0; i < 8; i++) {
    pinMode(signal1[i], LOW);
    pinMode(signal2[i], LOW);
    pinMode(signal3[i], LOW);
    pinMode(signal4[i], LOW);
    pinMode(sidewalk1[i], LOW);
    pinMode(sidewalk2[i], LOW);
  }
    resetFunc();
}

void display_tens(const int tens)
{
  int pin1, a, ones;     

  for (pin1 = 0, a = 0; pin1 < 7; pin1++, a++)
  {
    digitalWrite(pin1, bitRead(number[tens], a));
  }


  for (ones = 9; ones > -1; ones--)
  {
    if(digitalRead(STOP) == LOW)
    {
      stop();
    }
    display_ones(ones);
    delay(1000); 
  }

}

void display_ones(const int x)
{ int pin2, b;             

  for (pin2 = 7, b = 0; pin2 <= 13; pin2++, b++)
  {

    digitalWrite(pin2, bitRead(number[x], b));

        if(digitalRead(STOP) == LOW)
    {
      stop();
    }
  }

}