Pause and wait for keyboard entery

I have several procedures to go through and require my sketch to pause until I press g and enter. The following is what I've done so far:

Serial.println("Press G and Enter to continue"); while(Serial.available() == 0) { }

This works for the first occurrence of the pause, but doesn't for the subsequent ones. Why is this so?

Grateful for any assistance.

(deleted)

(deleted)

Spycather,

This works. Sketch show 3 stages where g has to be entered. I had to add a 100ms delay. Without it, the thing just goes straight through without waiting on the 2nd and 3rd inputs. I discovered the delay was necessary when I was trying to track why it just didn’t wait at the 2nd and 3rd stages and I placed Serial.print statements to check what was happening. It worked when I put the Serial.print statements; so I introduced delay. Why it works, I still cannot understand.

I really didn’t need to check for “G” because I just wanted to pause to do something before proceeding.

Thanks for your assistance.

{
Serial.begin(9600);
Serial.println(“Pass 1, Enter g”);
while (Serial.available() < 1)
{
delay(100);
}
while (Serial.available() > 0)
{
byte dummyread = Serial.read();
}

Serial.println(“Pass 2, Enter g”);
while (Serial.available() < 1)
{
delay(100);
}
while (Serial.available() > 0)
{
byte dummyread = Serial.read();
}

Serial.println(“Pass 3, Enter g”);
while (Serial.available() < 1)
{
delay(100);
}
while (Serial.available() > 0)
{
byte dummyread = Serial.read();
}

}

Or simply

while(true) // remain here until told to break
{
  if(Serial.available() > 0) // did something come in?
    if(Serial.read() == 'G') // is that something the char G?
      break;
}

Or just wait for more characters?

while (Serial.available() > 0) change to >2 so it doesn't proceed until 3 characters are received.

The Thread planning and implementing a program has functions for user input.

If you have a variable that records the fact that the input has been received you can use that to permit or prevent other functions from working.

...R

The delay is necessary because Serial is really really slow compared to the speed of the processor. When you hit “G” and on the serial monitor, each character takes milliseconds to be transmitted. Once you’ve read the “G” and processed it, the new-line character is still being transmitted and can’t be ‘read’ because it hasn’t completely arrived.

HazardsMind: Or simply

while(true) // remain here until told to break
{
  if(Serial.available() > 0) // did something come in?
    if(Serial.read() == 'G') // is that something the char G?
      break;
}

That works. But when it starts back at the top of the loop in just just runs on and on without waiting on an input. How do I correct that?

MorganS: The delay is necessary because Serial is really really slow compared to the speed of the processor. ....

That makes sense.

donovanpl123: That works. But when it starts back at the top of the loop in just just runs on and on without waiting on an input. How do I correct that?

Hmm? I don't have an arduino to test it.

It seems to work just fine here.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  Serial.println("Some text HERE");
  WaitForIt('A');
  Serial.println("IT'S SO FLUFFY!!!!!");
  WaitForIt('B');
}

void WaitForIt(char C)
{
  while (true)
  {
    if (Serial.available() > 0)
      if (Serial.read() == C)
        break;
  }
}

The delay is necessary because Serial is really really slow compared to the speed of the processor.

With all due respect, a delay() is only necessary if the program is not coded properly.

…R

HazardsMind: Hmm? I don't have an arduino to test it.

It seems to work just fine here.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {  // put your main code here, to run repeatedly:  Serial.println("Some text HERE");  WaitForIt('A');  Serial.println("IT'S SO FLUFFY!!!!!");  WaitForIt('B'); }

void WaitForIt(char C) {  while (true)  {    if (Serial.available() > 0)      if (Serial.read() == C)        break;  } }

This should suffice for WaitForIt:

void WaitForIt(char C)
{
  while (Serial.read() != (unsigned char)C)
    ;
}

There's no need to check if data is available because Serial.read() returns -1 when data is not available, and a character cannot equal -1.

I know that's what it does, but it's not documented. There's no guarantee that the next Arduino board won't just return the most-recent char when there's no new Serial data available.

@Christop
Are you telling me or the OP? Because I know about that, but I just stuck with what I wrote in my earlier reply.

There’s no guarantee that the next Arduino board won’t just return the most-recent char when there’s no new Serial data available.

If you are using the same IDE version, then it should.

MorganS: I know that's what it does, but it's not documented. There's no guarantee that the next Arduino board won't just return the most-recent char when there's no new Serial data available.

Yes, it's documented: https://www.arduino.cc/en/Serial/Read

Returns

the first byte of incoming serial data available (or -1 if no data is available)

There is no way they would get away with changing that.

hey, i have a probleem with my code:

void setup() { // put your setup code here, to run once:

Serial.begin(9600);

Serial.println("Loan amount"); while(Serial.available()==0){} loan= Serial.parseFloat(); //Serial.println(loan);

Serial.println("Anual interest rate"); while(Serial.available()==0){} myrate=Serial.parseFloat(); //Serial.println(myrate);

Serial.println("Years of loan"); while(Serial.available()==0){} years=Serial.parseFloat(); //Serial.println(years); }

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

} The problem is that when i run the code, "Loan amount" is printed and the monitor waits for the user to input a number, but i input a number and click enter, "Anual interest rate" is printed but it doesnt wait for the user to input something and prints "Years of loan" right away. I've been trying to fix this but i haven't been abe to fix it, not even my teacher. what's wrong? and what can i do? I reallyapprecite the help. Thanks.

@Andresvxx

What is the setting in serial monitor for the line ending? Correct setting for your scenario will probably be 'no line ending'.

Please read https://forum.arduino.cc/index.php?topic=148850.0, specifically point #7 about posting code.