#define UP_DOWN_MODE 1
#define ONE_WAY_MODE 0
#define START 0
#define END 9

int mode = UP_DOWN_MODE;
int state = 1;
int var_delay = 500;

void setup() 
{
  Serial.begin(115200);
  pinMode(2, OUTPUT);  //A
  pinMode(4, OUTPUT);  //B
  pinMode(15, OUTPUT); //C
  pinMode(5, OUTPUT);  //D
  pinMode(18, OUTPUT); //E
  pinMode(19, OUTPUT); //F
  pinMode(21, OUTPUT); //G
}

void loop() 
{
  check(state);
}
void check(int state)
{
  switch(state)
  {
    case 1 :
    UpCounter(mode,START);
      break;
    case 0 :
    DownCounter(mode,END);
      break;
  }
}
void display(int n)
{
  switch (n) 
  {
    case 0:
      digitalWrite(2, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(15, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(18, HIGH);
      digitalWrite(19, HIGH);
      digitalWrite(21, LOW);
      delay(var_delay);
        break;

      case 1:
      digitalWrite(2, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(15, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(18, LOW);
      digitalWrite(19, LOW);
      digitalWrite(21, LOW);
      delay(var_delay);
        break;

      case 2:
      digitalWrite(2, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(15, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(18, HIGH);
      digitalWrite(19, LOW);
      digitalWrite(21, HIGH);
      delay(var_delay);
        break;

      case 3:
      digitalWrite(2, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(15, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(18, LOW);
      digitalWrite(19, LOW);
      digitalWrite(21, HIGH);
      delay(var_delay);
        break;

      case 4:
      digitalWrite(2, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(15, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(18, LOW);
      digitalWrite(19, HIGH);
      digitalWrite(21, HIGH);
      delay(var_delay);
        break;

      case 5:
      digitalWrite(2, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(15, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(18, LOW);
      digitalWrite(19, HIGH);
      digitalWrite(21, HIGH);
      delay(var_delay);
        break;

      case 6:
      digitalWrite(2, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(15, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(18, HIGH);
      digitalWrite(19, HIGH);
      digitalWrite(21, HIGH);
      delay(var_delay);
        break;
            
      case 7:
      digitalWrite(2, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(15, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(18, LOW);
      digitalWrite(19, LOW);
      digitalWrite(21, LOW);
      delay(var_delay);
        break;

      case 8:
      digitalWrite(2, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(15, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(18, HIGH);
      digitalWrite(19, HIGH);
      digitalWrite(21, HIGH);
      delay(var_delay);
        break;

      case 9:
      digitalWrite(2, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(15, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(18, LOW);
      digitalWrite(19, HIGH);
      digitalWrite(21, HIGH);
      delay(var_delay);
        break;
  }
}
void UpCounter(int mode,int i)
{
  for( i ;i <= END; i++)
  {
    display(i);
  }
  if(mode == UP_DOWN_MODE)
  { // we 'll sent it with decrementation to avoid double delay in the last number (9)
    DownCounter(mode,--i);
  }                             
}
void DownCounter(int mode,int i)
{
  for( i;i >= START; i--)
  {
    display(i);
  }
  if(mode == UP_DOWN_MODE)
  {// we 'll sent it with incrementation to avoid double delay in the first number(0)
    UpCounter(mode,++i);
  }
}

void IncDelay()
{
  var_delay += 100;
}
void DecDelay()
{
  var_delay -= 100;
}