#define RED_LED 4
#define GREEN_LED 5
#define YELLOW_LED 2
#define BLUE_LED 3

#define GREEN_BUTTON 8
#define YELLOW_BUTTON 9
#define BLUE_BUTTON 10
#define RED_BUTTON 11

int rodada = 0;
int sequencia[40];
boolean wating = true;
int indiceMedir = 0;
boolean digitando = false;

void setup() {
  Serial. begin(9800);

  aleatorizar();

  pinMode(RED_BUTTON, INPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_BUTTON, INPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_BUTTON, INPUT);
  pinMode(BLUE_LED, OUTPUT);
  pinMode(YELLOW_BUTTON, INPUT);
  pinMode(YELLOW_LED, OUTPUT);

  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(BLUE_LED, LOW);
  digitalWrite(YELLOW_LED, LOW);
}

void loop() {
  piscar();

  if(millis() % 5000 < 100 && digitando == false){
    wating = true;
  }

  if(digitalRead(RED_BUTTON)){
    if(sequencia[indiceMedir] == RED_LED){
      delay(200);
      indiceMedir++;
      digitando = true;
    } else {
      lose();
    }
  } else if(digitalRead(GREEN_BUTTON)){
    if(sequencia[indiceMedir] == GREEN_LED){
      delay(200);
      indiceMedir++;
      digitando = true;
    } else {
      lose();
    }
  } else if(digitalRead(BLUE_BUTTON)){
    if(sequencia[indiceMedir] == BLUE_LED){
      delay(200);
      indiceMedir++;
      digitando = true;
    } else {
      lose();
    }
  } else if(digitalRead(YELLOW_BUTTON)){
    if(sequencia[indiceMedir] == YELLOW_LED){
      delay(200);
      indiceMedir++;
      digitando = true;
    } else {
      lose();
    }
  }

  if(indiceMedir > rodada){
    rodada++;
    indiceMedir = 0;
    wating = true;
  }
}

void piscar(){
  if(wating == true) {
    wating = false;
    for (int i = 0; i <= rodada; i++) {
      digitalWrite(sequencia[i], HIGH);
      delay(200);
      digitalWrite(sequencia[i], LOW);
      delay(200);
    }
  }
}

void lose(){
  for(int i = 0; i < 4; i++){
    digitalWrite(RED_LED, HIGH);
    digitalWrite(GREEN_LED, HIGH);
    digitalWrite(BLUE_LED, HIGH);
    digitalWrite(YELLOW_LED, HIGH);
    delay(100);
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(BLUE_LED, LOW);
    digitalWrite(YELLOW_LED, LOW);
    delay(100);

    aleatorizar();
    rodada = 0;
    indiceMedir = 0;
    wating = true;
    digitando = false;
  }
  delay(500);
}

void aleatorizar(){
  randomSeed(analogRead(0));

  for (int i = 0; i < 40; i++) {
    sequencia[i] = random(2, 6);
    Serial.println(sequencia[i]);
  }
}


$abcdeabcde151015202530354045505560fghijfghij