Clear Serial available

I made a program for a simple game, but I have a problem in intruding the wrong answer, and being able to use the 2 attempts that remain. Because if you enter a wrong answer, after it returns the message that it is wrong and the number of attempts is not expected by new data, it responds immediately with a new wrong answer and attempts that are still missing, as soon as it runs out of attempts, it does the expected and ends the game.
Can anyone help me and explain how to solve it?

This is my program:

int valorLido = 0; //Variavel para armazenar leituras do ADC
String dadosRecebidos = "" ; //Configuraçao para resposta de jogo
byte contador = 0;

void setup() {
Serial.begin(9600);// Configuração da velociade de transmissão
pinMode(8, OUTPUT); // Configuraçao do LED como vencedor
pinMode(9 ,OUTPUT); // Configuraçao do LED como perdeu
pinMode(10 ,OUTPUT); // Configuraçao do LED Inicio de Jogo
pinMode(7, OUTPUT); // Configuração do LED Tentativa Errada
pinMode(11, INPUT_PULLUP); //BOTÃO
digitalWrite (8, LOW);//Desligar LEDs
digitalWrite (9, LOW);
digitalWrite (10, LOW);

}

void loop(){
label1:
Serial.println("Pressione o botão e escolha um numero"); //Envio de texto unico
while(digitalRead(11) == HIGH){ //Se o botão for pressionado
delay(20);
}
digitalWrite(10,HIGH); // Liga o Led de Inicio de jogo
valorLido = analogRead(A5);

contador = 3 ; //Declaração variavel

label2:
if(contador == 0){
Serial.println("Tentativas esgotadas, perdeu o jogo!");
digitalWrite(9, HIGH); // Acende LED Resposta Errada
delay(3000);
digitalWrite(9, LOW);
digitalWrite(10,LOW);
goto label1;
}

label3:
Serial.begin(9600);// Configuração da velociade de transmissão
while(Serial.available() == 0) { // Se o Arduino receber resposta
delay(20);
}
dadosRecebidos = Serial.read(); //Lê os dados recebidos e guarda na própria variavel

if (valorLido >= dadosRecebidos-50 and valorLido <= dadosRecebidos +50){// Se a resposta for certa
digitalWrite(8, HIGH);
Serial.println("Resposta correta");
delay(3000);
digitalWrite(8, LOW);
digitalWrite(10, LOW);
Serial.println("Terminou o Jogo, Venceu"); //Envio de texto unico
goto label1;
} else { // Se a resposta for errada
digitalWrite(7, HIGH);
contador = contador - 1; //Subtrair 1 ao valor do contador
Serial.print ("Resposta errada, tente novamente, tem ");
Serial.print (contador, DEC);
Serial.println (" tentativas");
Serial.available() == 0;
delay(3000);
digitalWrite(7,LOW);
goto label2;
}
}

Uh-oh.

Please remember to use code tags when posting code.

code tags as? i am new in this

Please read and follow the instructions in the "How to get the best out of this forum" post, linked at the head of every forum category.

What is it you want the program to do that is different from what you described?

Perchance read the instructions. :grin:

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.