Most Basic While loop problem

I am trying to make a servo move continuously, until I send a different command via serial

#include <Servo.h>
Servo servo1_UD; Servo servo1_LR; Servo servo2_UD; Servo servo2_LR;

void setup() {
  servo1_UD.attach(9); servo1_LR.attach(10); servo2_UD.attach(11); servo2_LR.attach(12);   //may need diff pins for Sev2


}

void loop() {
  static int servoAngle = 0;

  if ( Serial.available()) {
    char incomingChar = Serial.read();  //read incoming byte

    switch (incomingChar) {

      case '0'...'9':
        servoAngle = servoAngle * 10 + incomingChar - '0';


        break;

    }


    while (incomingChar == 's') {

      servo1_UD.write(180); delay(650); servo1_UD.write(0); delay(650);

      char incomingChar = Serial.read();

    }



  }

}

...and what happens instead?

Error checking with serial shows that While it does enter the loop,

And it outputs Various incomingChar (For example the '\r' and '\n' that happen when you send serial)

ANY incomingChar should end the loop, but the loop never ends, as though it never checks the condition ever again

change

      servo1_UD.write(180); delay(650); servo1_UD.write(0); delay(650);
      char incomingChar = Serial.read();

to

      servo1_UD.write(180); delay(650); servo1_UD.write(0); delay(650);
      incomingChar = Serial.read();

AHhhhh! always ! thank you!!!!

I copy pasted that from up top

It was creating another instance of that variable rather than changing the value on the existing one?

And then as long as I have you, easy way to make it ignore the /r /n incoming?

was thinking have it change a separate variable that only changes a few specific ways (Like a switch case, but I couldn't get that to work either)

Jack777:
It was creating another instance of that variable rather than changing the value on the existing one?

+1 karma for grasping it.

Jack777:
easy way to make it ignore the /r /n incoming?

was thinking have it change a separate variable that only changes a few specific ways (Like a switch case, but I couldn't get that to work either)

That's not very clear, but I think you probably have the right idea. You just need to make your code that parses the Serial input ignore those characters. That will be easiest to accomplish if you get rid of that while loop thing you have. Your code should only have one call to Serial.read().

Maybe have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R