Need help Arduino Guessing number

Hi, I made a code for guess a random number but there is a pb...
When I type a number like 2, there is a 0 who will be type automaticaly after my number and i don't know why. I also tryied to add 2 leds for if the random number is higher or lower.( But i can't turn off these leds.)
There is the code :

int randNumber;
int player1_number;
int LedR = 2;
int LedB = 3;

void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
Serial.println("");
Serial.println("
");
Serial.println("
Numerical guessing game ");
Serial.println("
");
Serial.println("
");
Serial.print("Devinez le nombre aléatoire:");
randNumber = random(0, 10);

while(Serial.read()>= 0){}

}

void loop() {

while (Serial.available() > 0 )  {
  delay( 10 );  

  player1_number = Serial.parseInt();
  Serial.println ( player1_number ); 


  if(player1_number==randNumber){     
  Serial.println("Bravo !");
  digitalWrite(LedR,HIGH);
  digitalWrite(LedB,HIGH);
  randNumber = random(0, 10);
  Serial.print("Devinez le nombre aléatoire:");
  }
  else if( player1_number < randNumber){
   Serial.println("C'est plus grand");
   digitalWrite(LedR,HIGH);
  }
  else if( player1_number > randNumber){
   Serial.println("C'est plus petit");
   digitalWrite(LedB,HIGH);
  }

}

}

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

1 Like

If you have the Line ending set to anything other than "No line ending" in the Serial monitor then you will have this problem because of the extra line ending characters

What is stopping you ?

1 Like

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

1 Like

Thx all is good now, i just forgot how to switch off lights but i remember it xD

What is the intention with this line of code in setup: while(Serial.read()>= 0){}

Also you do not set the LED pins to be outputs, this will invert the way you can get a a weak light from the LED as you turn the pull up resistor on or off.

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