//DEFINE PINS-------------------------------------
  #define push1 D0
  #define push2 D1

  #define led1 D2
  #define led2 D3
  #define led3 D4
  #define led4 D5
  #define led5 D6
  #define led6 D7
  #define led7 D8
  #define led8 D9
  #define led9 D10

//VARIABLES---------------------------------------
  int flag1 = 1;        //flag for the buttons
  int flag2 = 1;

  int cont = 0;         //main counter to turn on leds
  int cont_vel = 0;     //counter to determine speed
  int state = 1;        //counter increases(1) or decreases(0)
  int game = 0;         //start game
  int cont_red=0;       //count when you reach the color red



void setup() {

  Serial.begin(9600);          //comunication
  //PINMODE-------------------------------------------
    pinMode(push1,INPUT);        //buttons
    pinMode(push2,INPUT);

    pinMode(led1,OUTPUT);        //LEDS
    pinMode(led2,OUTPUT);
    pinMode(led3,OUTPUT);
    pinMode(led4,OUTPUT);
    pinMode(led5,OUTPUT);
    pinMode(led6,OUTPUT);
    pinMode(led7,OUTPUT);
    pinMode(led8,OUTPUT);
    pinMode(led9,OUTPUT);

}

void loop() {
  //START GAME----------------------------------------
    //press the two buttons to start, small blink and I enter 
    //the game loop
    if ((digitalRead(push1)==1) && (digitalRead(push2)==1)) {
      game=1;
      Serial.println("--------START----------");
      (digitalWrite(led1,1));
      (digitalWrite(led2,1));
      (digitalWrite(led3,1));
      (digitalWrite(led4,1));
      (digitalWrite(led5,1));
      (digitalWrite(led6,1));
      (digitalWrite(led7,1));
      (digitalWrite(led8,1));
      (digitalWrite(led9,1));
      delay(50);
      (digitalWrite(led1,0));
      (digitalWrite(led2,0));
      (digitalWrite(led3,0));
      (digitalWrite(led4,0));
      (digitalWrite(led5,0));
      (digitalWrite(led6,0));
      (digitalWrite(led7,0));
      (digitalWrite(led8,0));
      (digitalWrite(led9,0));
      delay(50);
      state=1;
    }
  
  //GAME LOOP-------------------------------------------------
    while(game==1){
      
      //game speed
      for (int x=100; x>=cont_vel; x--) {
      delay(2);
      }

      //first button with flag, counter increases and speed 
      //increases
      if ((digitalRead(push1)==1) && (flag1==1)) {
      state=1;
      cont_vel++;
      flag1=0;
      }
      if((digitalRead(push1)==0) && (flag1==0)){
      flag1=1;
      }

      //second button with flag, counter decreases  and speed 
      //increases
      if ((digitalRead(push2)==1) && (flag2==1)) {
      state=0;
      flag2=0;
      cont_vel++;
      }
      if((digitalRead(push2)==0) && (flag2==0)){
      flag2=1;
      }

      //counter increases or decreases
      if (state==1) {
      cont++;
      }
      if (state==0) {
      cont--;
      }

      //print counter
      Serial.println(cont);

      //negative limit of counter
      if (cont<=-1){
        state=1;
        cont=1;
      }
      
      //accountant cases
      switch (cont) {
        //blink and we give up the game
        case 0:
          for (int x=0; x<=5; x++) {
            (digitalWrite(led1,1));
            (digitalWrite(led2,1));
            (digitalWrite(led3,1));
            (digitalWrite(led4,1));
            (digitalWrite(led5,1));
            (digitalWrite(led6,1));
            (digitalWrite(led7,1));
            (digitalWrite(led8,1));
            (digitalWrite(led9,1));
            delay(100);
            (digitalWrite(led1,0));
            (digitalWrite(led2,0));
            (digitalWrite(led3,0));
            (digitalWrite(led4,0));
            (digitalWrite(led5,0));
            (digitalWrite(led6,0));
            (digitalWrite(led7,0));
            (digitalWrite(led8,0));
            (digitalWrite(led9,0));
            delay(100);
          }
            game=2;  
        break;

        //LED1 turns on and network increases
        case 1:
          (digitalWrite(led1,1));
          (digitalWrite(led2,0));
          (digitalWrite(led3,0));
          (digitalWrite(led4,0));
          (digitalWrite(led5,0));
          (digitalWrite(led6,0));
          (digitalWrite(led7,0));
          (digitalWrite(led8,0));
          (digitalWrite(led9,0));
          cont_red++;
          Serial.println("red");
        break;

        case 2:
          (digitalWrite(led1,0));
          (digitalWrite(led2,1));
          (digitalWrite(led3,0));
          (digitalWrite(led4,0));
          (digitalWrite(led5,0));
          (digitalWrite(led6,0));
          (digitalWrite(led7,0));
          (digitalWrite(led8,0));
          (digitalWrite(led9,0));
        break;

        case 3:
          (digitalWrite(led1,0));
          (digitalWrite(led2,0));
          (digitalWrite(led3,1));
          (digitalWrite(led4,0));
          (digitalWrite(led5,0));
          (digitalWrite(led6,0));
          (digitalWrite(led7,0));
          (digitalWrite(led8,0));
          (digitalWrite(led9,0));
        break;

        case 4:
          (digitalWrite(led1,0));
          (digitalWrite(led2,0));
          (digitalWrite(led3,0));
          (digitalWrite(led4,1));
          (digitalWrite(led5,0));
          (digitalWrite(led6,0));
          (digitalWrite(led7,0));
          (digitalWrite(led8,0));
          (digitalWrite(led9,0));
        break;

        case 5:
          (digitalWrite(led1,0));
          (digitalWrite(led2,0));
          (digitalWrite(led3,0));
          (digitalWrite(led4,0));
          (digitalWrite(led5,1));
          (digitalWrite(led6,0));
          (digitalWrite(led7,0));
          (digitalWrite(led8,0));
          (digitalWrite(led9,0));
        break;

        case 6:
          (digitalWrite(led1,0));
          (digitalWrite(led2,0));
          (digitalWrite(led3,0));
          (digitalWrite(led4,0));
          (digitalWrite(led5,0));
          (digitalWrite(led6,1));
          (digitalWrite(led7,0));
          (digitalWrite(led8,0));
          (digitalWrite(led9,0));
        break;

        case 7:
          (digitalWrite(led1,0));
          (digitalWrite(led2,0));
          (digitalWrite(led3,0));
          (digitalWrite(led4,0));
          (digitalWrite(led5,0));
          (digitalWrite(led6,0));
          (digitalWrite(led7,1));
          (digitalWrite(led8,0));
          (digitalWrite(led9,0));
        break;

        case 8:
          (digitalWrite(led1,0));
          (digitalWrite(led2,0));
          (digitalWrite(led3,0));
          (digitalWrite(led4,0));
          (digitalWrite(led5,0));
          (digitalWrite(led6,0));
          (digitalWrite(led7,0));
          (digitalWrite(led8,1));
          (digitalWrite(led9,0));
        break;

        //LED9 turns on and network increases  
        case 9:
          (digitalWrite(led1,0));
          (digitalWrite(led2,0));
          (digitalWrite(led3,0));
          (digitalWrite(led4,0));
          (digitalWrite(led5,0));
          (digitalWrite(led6,0));
          (digitalWrite(led7,0));
          (digitalWrite(led8,0));
          (digitalWrite(led9,1));
          cont_red++;
          Serial.println("red");
        break;

        //blink and we give up the game
        case 10:
          for (int x=0; x<=5; x++) {
            (digitalWrite(led1,0));
            (digitalWrite(led2,0));
            (digitalWrite(led3,0));
            (digitalWrite(led4,0));
            (digitalWrite(led5,1));
            (digitalWrite(led6,1));
            (digitalWrite(led7,1));
            (digitalWrite(led8,1));
            (digitalWrite(led9,1));
            delay(100);
            (digitalWrite(led1,0));
            (digitalWrite(led2,0));
            (digitalWrite(led3,0));
            (digitalWrite(led4,0));
            (digitalWrite(led5,0));
            (digitalWrite(led6,0));
            (digitalWrite(led7,0));
            (digitalWrite(led8,0));
            (digitalWrite(led9,0));
            delay(100);
          }
          game=2;
        break;
      }
    }

    //SCORE AND RESET---------------------------------
      //In this oart we show the results and reset the counters, 
      //we return to the beginning of the game
      if(game==2){
        cont_red=cont_red-2;
        Serial.println("-------GAME OVER--------");
        Serial.print("Speed: ");
        Serial.println(cont_vel);
        Serial.print("Red: ");
        Serial.println(cont_red);
        Serial.println(" ");
        Serial.print("SCORE: ");
        Serial.println(cont_red*cont_vel+cont_vel);
        game=0;
        cont=0;
        cont_vel=0;
        cont_red=0;
      }


}