Hello, I am trying to make a guessing game that interacts through the user through the Serial monitor. Here is the code:
long Ran;
int guess = 0;
int guesslast;
void setup() {
// put your setup code here, to run once:
Ran = random(0, 100);
Serial.begin(9600);
Serial.println ("Guess a number between 1 and 100");
}
void loop(){
for (int i = 0; i <= 5; i++) {
guesslast = guess;
while (Serial.available() <= 0) {
guess = Serial.read();
}
if (guess == Ran) {
Serial.println ("you guessed correctly!");
break;
}
else if (guess < Ran) {
Serial.println ("Your guess is too low");
}
else if (guess > Ran) {
Serial.println ("Your guess is too high");
}
else if (i == 5) {
Serial.println ("You're out of guesses");
}
}
while(1==1){}
}
When i run the code, it waits fir the first input, but then speeds through the rest of the guessing rounds without waiting for user input. I have been trying to trouble shoot this for a couple days, and cannot see anything that is wrong. Anyone see any potential issues? thank you for your help.
Try something like, and take a look at my tutorial on various ways to read serial Arduino Software Solutions
long Ran;
int guess = 0;
int guesslast;
String input;
void setup() {
// put your setup code here, to run once:
Ran = random(0, 100);
Serial.begin(9600);
Serial.println("Guess a number between 1 and 100");
// Serial.println(Ran); for testing ...
input.reserve(30);
Serial.setTimeout(5000); // readStringUntil timeouts out in 5sec if nothing entered
}
void loop() {
for (int i = 0; i <= 4; i++) { // 0 to 4 is 5 goes
guesslast = guess;
// loop here for number input
while (1) {
input = Serial.readStringUntil('\n');
input.trim();
if (input.length() > 0) { // got a word handle it
Serial.print(F("You entered:")); Serial.println(input); // added in edit
guess = input.toInt();
if (input == String(guess)) { // valid number
break;
} else {
Serial.print(input); Serial.println(F(" is not a valid number"));
}
} //else
Serial.println(F("Guess a number between 1 and 100"));
}
if (guess == Ran) {
Serial.println ("you guessed correctly!");
break;
}
else if (guess < Ran) {
Serial.println ("Your guess is too low");
}
else if (guess > Ran) {
Serial.println ("Your guess is too high");
}
}
Serial.println ("You're out of guesses");
while (1) {}
}
Serial.read only reads a single character, not a complete text that you send.
You might also want to pay attention to the line ending in Serial monitor. With the below setting (No line ending), it only sends the text that you typed; other settings will add one or two additional characters. Note that @drmpf's code in reply #4 relies on line ending being set to New line.
Actually not, if you do nothing readStringUntil('\n') just returns every 5sec, as set by Serial.setTimeout(5000); in setup, and prompt the user again. If you do not set NL you just have to wait 5sec until the number is picked up.
BUT.. setting NL or Both CR & NL is a good idea.
Or you can adjust the Serial.setTimeout( ) if you want the input to be more response (and the prompt more annoying)
Guess a number between 1 and 100
You entered:5
Your guess is too low
You entered:66
Your guess is too high
You entered:4
Your guess is too low
You entered:7
you guessed correctly!
You're out of guesses
Slight logic problem since is should not print out of guesses if you guess correctly. I will leave that to you to fix.