Unwanted looping in Arduino code!!

Thread Starter

Mozee

Joined Jul 23, 2016
87
Hello AACs ..

I am totally new to Arduino, just bear with me please ...
I was trying to code a simple arduino game that can read the resultant number from the player's mind, simple and pure mathematics which will always yield the number "3". Problem is, I would like to hang the code until the user types "ok" then presses ENTER in the serial monitor window, but after the program reaches the line :
Code:
Serial.println("Type ""ok"" and press ENTER when you've already choosen a number . . . ");
and the user types "ok" and presses enter, the program keeps running and the
Code:
while (Serial.available()==0) {  }
code is not functional anymore and the program wont hang to wait for an entry and the program keeps looping afterwards!

Code:
void loop() {

Serial.println("Choose any number from 1 to 10");
delay(7000);
Serial.println(" ");
Serial.println("Type ""ok"" and press ENTER when you've already choosen a number . . . ");
while (Serial.available()==0) {  }

Serial.println("Now .. Multiply your number by 5");
delay(5000);
Serial.println("Type ""ok"" and press ENTER when you know the result");
while (Serial.available()==0) ; {

}

Serial.println("Ok .. Divide your current number by the original number then type ""ok"" and press ENTER when ready");
while (Serial.available()==0) {

}
Serial.println("Now subtract 2 from the current number then type ""ok"" and press ENTER");
while(Serial.available()==0) { 

}

delay(3000);
Serial.println("You've got a number right now");
delay(2000);
Serial.println("But .. It is inside your mind, in the Damagh el Kachaa of yours");
delay(2000);
Serial.println("Reading your mind now!! ... ");
delay(1000);
Serial.println("PROCESSING . . . . . . . . . . .");

for (int i=1; i<=3; i=i+1) // i= the for loop execution times
{
digitalWrite(RLEDpin,HIGH);
delay(250);
digitalWrite(RLEDpin,LOW);
digitalWrite(BLEDpin,HIGH);
delay(250);
digitalWrite(BLEDpin,LOW);
digitalWrite(RLEDpin,HIGH);
delay(250);
} // End of "for" code

digitalWrite(BLEDpin,LOW);
digitalWrite(RLEDpin,LOW);


Serial.println("Ok .. Now I know it ya khabeel");
delay(2000);
Serial.print("It is ");
delay(2000);
Serial.print("The number 3 ");
Serial.print(" ");
Serial.print(" ");
Serial.print(" ");
Serial.print(" ");

}

What's going on? what did i do wrong?
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,415
Nothing seems wrong with these two individual lines of code. Can you paste a larger section of the code so the whole section is visible?
 

djsfantasi

Joined Apr 11, 2010
9,237
I don't see anywhere that you read in the typed characters. So, Serial.available is forevermore true, and will never pause again.
 

Thread Starter

Mozee

Joined Jul 23, 2016
87
I don't see anywhere that you read in the typed characters. So, Serial.available is forevermore true, and will never pause again.
So how do I solve that?
You know, what I want is to have the program hanged until the user presses ENTER so that the next code line is executed but when pressing ENTER without typing anything in the serial monitor box a red error message appears in the bottom saying "You've pressed send but nothing was sent ....", I couldn't figure out how to do that so I prompted the user to type "ok" then pressing ENTER...

Any Ideas on how to solve it without the user needing to enter a specific value? if not then how to fix the current issue?
 

djsfantasi

Joined Apr 11, 2010
9,237
After you exit the whole loop that waits for characters to be typed, perform a Serial.read until there are no characters available. That is the simple answer.

Not checking for a specific value does become problematic. While during the time a user is typing "ok" <enter>, the Arduino can do many checks. Hence, you may end up in a situation where several pauses are skipped. For example, once for "o", once for "k" and once for <enter>. Programming around this behavior is not simple, but is a common situation that you'll encounter. It'll take me more time than I have now to provide an example.
 

Thread Starter

Mozee

Joined Jul 23, 2016
87
Still the same problem after adding the Serial.read code :(
the variables v1 v2 v3 v4 are globally declared.

Code:
void loop() {
  // put your main code here, to run repeatedly:

Serial.println("Choose any number from 1 to 10");
delay(7000);
Serial.println(" ");
Serial.println("Type ""ok"" and press ENTER when you've already choosen a number . . . ");
while (Serial.available()==0) {  }
v1=Serial.read();

Serial.println("Now .. Multiply your number by 5");
delay(5000);
Serial.println("Type ""ok"" and press ENTER when you know the result");
while (Serial.available()==0) ; {  }
v2=Serial.read();

Serial.println("Ok .. Divide your current number by the original number then type ""ok"" and press ENTER when ready");
while (Serial.available()==0) { }
v3=Serial.read();

Serial.println("Now subtract 2 from the current number then type ""ok"" and press ENTER");
while(Serial.available()==0) {  }
v4=Serial.read();
 

SLK001

Joined Nov 29, 2011
1,549
It looks like you are waiting for a "0" (zero) instead of an "o" or "O". You should also make your code case insensitive by making the answer uppercase only.
 

Thread Starter

Mozee

Joined Jul 23, 2016
87
Thank you guys.. I found it now and your comments brought this idea to my mind :D

I declared the variables v1 v2 v3 and v4 as strings and used <v1=Serial.readString();> code because the <while (Serial.available()==0) { }> loop will be valid only if the user entry is zero (Number) as in no number entry(nothing is entered) I think and please correct me if I'm wrong. I then used the <Serial.readString();> code so when the user types "ok" <enter> the entry is recognized.

Again, thank you guys and If someone could explain better or correct me if I misunderstood any of the above code functions I would be very thankful.
 

djsfantasi

Joined Apr 11, 2010
9,237
Are you familiar with functions? This pause code is an excellent candidate for a function, as it is used repeatedly throughout your sketch.

The code waits for a character to become available, reads it off the buffer and returns to the main loop if it is an enter character. It ignores any other characters and continues looping (pausing) until the <enter> key is pressed.

Code:
void pause() {

int inputChar;
const int untilDone=1;

while (Serial.available()==0) {}

while(untilDone) {
  while (Serial.available()) {
  inputChar=Serial.read();
  if (inputChar==13) return;
  }
  }
}
Is this exercise part of your homework?
 

Thread Starter

Mozee

Joined Jul 23, 2016
87
Thanks @djsfantasi .. This is not a Homework at all, In fact, I'm a Petroleum Engineer but I really love electronics and can't live without it to be honest :D, however I am totally new to coding!
To be honest I couldn't understand the code you posted, I would appreciate it if you could explain it in more details line by line if possible.
also why would I want to check if the entered value is 13?
 

djsfantasi

Joined Apr 11, 2010
9,237
Glad to hear back from you.

First, a function is a group of code that performs a common action; one that is repeated several times in your sketch, In your program, your are prompting the user weith various messages, and then want to pause and wait for them to reply. That pause and wait can be grouped together, and easily referred to in your program. For example, I have copied a few lines of your last program and modified it to use a function - like the one I provided before.
Code:
void loop() {
  // put your main code here, to run repeatedly:

Serial.println("Choose any number from 1 to 10");
delay(7000);
pause();

Serial.println("Now .. Multiply your number by 5");
delay(5000);
pause();

Serial.println("Ok .. Divide your current number by the original number");
pause();

Serial.println("Now subtract 2 from the current number");
pause();

// The rest of your program has been omitted in this example
}

void pause() {

int inputChar;
const int untilDone=1;
Serial.println("Press <Enter> when you are ready");

while (Serial.available()==0) {}

while(untilDone) {
  while (Serial.available()) {
  inputChar=Serial.read();
  if (inputChar==13) return;
  }
  }
}
I believe that main code in the example is simpler and easier to read than your original. I did modify your code slightly, Making the prompt to confirm the same in all cases to illustrate what common code in a function can do for you. It is not necessary.

Line 25 defines a variable for you to read into.
Line 26 defines a constant, giving it a name that means something to the sketch.

Line 29 should look familiar. It waits for a character to be input into the Arduino.

Lines 31-36 loop forever (not quite) reading characters. This way, if the user accidentally hits the keyboard or types in extra characters, they will be ignored and not affect the flow of your program. We only want to respond to the enter key...

And that is where the mysterious number 13 comes from. Characters are stored in memory as integers, in a coding scheme developed a long time ago. It is called ASCII (Google it!). In this coding scheme, the number 13 has been assigned to the enter key.

Hope this helps.
 
Top