Help with While Loops within If statements

Hello everyone,

I don't understand what I'm doing wrong. I have a while loop within an if statement, however when I make that if statement false, the program continues to loop. Please look at my code and tell me why this is happening.

const int ledPin = 13; // the pin that the LED is attached to int incomingByte; // a variable to read incoming serial data into

void setup() { // initialize serial communication: Serial.begin(9600); // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); }

void loop() { // see if there's incoming serial data: if (Serial.available() > 0) { // read the oldest byte in the serial buffer: incomingByte = Serial.read(); // if it's a capital H (ASCII 72), turn on the LED: if (incomingByte == 'H') { digitalWrite(ledPin, HIGH); } // if it's an L (ASCII 76) turn off the LED: if (incomingByte == 'L') { digitalWrite(ledPin, LOW); } if (incomingByte== 'B'){ while (incomingByte=='B') { digitalWrite(ledPin, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(ledPin, LOW); // set the LED off delay(1000);

} } if (incomingByte == 'S') { digitalWrite(ledPin, LOW); } } }

So when I type B, the LED blinks but if I were to type in S or L the LED continues to blink. Any suggestions or help would be greatly appreciated! :)

Thanks!!!

while (incomingByte=='B') {

There's nothing to stop "incomingByte" from being 'B', so it just keeps flashing, forever.

Well thats the thing, when I type 'H' or 'L', the while (incomingByte=='B') should become false but for some reason the arduino is not recognizing that incomingByte is no longer B when I type in another letter. So how stop "incomingByte" from being 'B' ?

While you're in the 'while' loop there is no way that "incomingByte" can change.

but for some reason the arduino is not recognizing

That "some reason" is the fact that you're not reading a new value into "incomingByte".

Is there anyway that I write the code so that when the user types B the LED will blink but then when the user types L, the LED will turn off?

Yes, of course. The best way is read the "Blink without delay" tutorial, and adapt it to your sketch.

The quick and dirty* way is:while (incomingByte=='B' && Serial.available () == 0) {

  • and high latency

I'd modify it like this:

 if (Serial.available() > 0) {
   // read the oldest byte in the serial buffer:
   incomingByte = Serial.read();
   }

(and remove one of the curly brackets at the end) Though the blink without delay is indeed something you should look into, those delays will make it rather unresponsive to input.. there are several recent topics on millis aswell, in case the blink without delay example isn't enough to illustrate its purpose.

I'd modify it like this:

But that still won't break you out of the inner "while"

const int ledPin = 13; // the pin that the LED is attached to

void setup() 
{
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
}

void loop() 
{
  if (Serial.available() > 0) {
    int incomingByte = Serial.read();

    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    } 

    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  
    while (incomingByte== 'B' && Serial.available () == 0) {
      digitalWrite(ledPin, HIGH);   // set the LED on
      delay(1000);                  // wait for a second
      digitalWrite(ledPin, LOW);    // set the LED off
      delay(1000);
    }
    if (incomingByte == 'S') {
     digitalWrite(ledPin, LOW);
    }    
  }
}

But as AWOL noted, this will have an average one second latency, and it would be best to get rid of the "delay"s, and use a state-machine.

Sorry, yes, I meant it together with the while modification mentioned earlier :-X

Thank you!! :smiley: