Cannot get out of a while loop.

Hello. I think the answer to my question is fairly easy. Starting from a " SwitchCase sketch " example found on the Arduino website, I'm trying to make one change to the code and cant make it work as wishes. This code simple turn on and off a LED by sending key presses thru serial. When I press the number 2 key, I would like the LED to blink until I press another valid key. I'm able to achieve this when pressing number 2 key but once the blink2's while loop, I cant get out of there. It's an infintite loop. I dont understands why. Can anybody help me please. Regards.

/* * SwitchCase sketch * example showing switch statement by switching on chars from the serial port * * sending the character 1 blinks the LED once, sending 2 blinks twice * sending + turns the LED on, sending - turns it off * any other character prints a message to the Serial Monitor */

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

void setup() { Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud pinMode(ledPin, OUTPUT); }

void loop() { if ( Serial.available()) // Check to see if at least one character is available { char ch = Serial.read(); switch(ch) { case '1': blink(); break; case '2': blink2(ch); break; case '+': digitalWrite(ledPin,HIGH); break; case '-': digitalWrite(ledPin,LOW); break; default : Serial.print(ch); Serial.println(" was received but not expected"); break; } } }

void blink() { digitalWrite(ledPin,HIGH); delay(500); digitalWrite(ledPin,LOW); delay(500); }

void blink2(char ch) { while(ch == '2'){ Serial.println(ch); digitalWrite(ledPin,HIGH); delay(100); digitalWrite(ledPin,LOW); delay(100); } }

void blink2(char ch)
{
 while(ch == '2'){
   Serial.println(carac);
   digitalWrite(ledPin,HIGH);
   delay(100);
   digitalWrite(ledPin,LOW);
   delay(100);
 }
}

You never re-read "ch" so you stay in that loop.

I'm sorry AWOL. Can you be a little more clearer please. Actually, English is not my first language and I cant understand what you mean by re-tead "ch"? And how can I re-tead "ch"? Thank you again for your help and your understanding. Regards.

I cant understand what you mean by re-tead “ch”.

You pass a value to the function, in the argument ch. What will make that value change? Once the while loop starts, there is nothing to make it end.

If you were to detect that there was serial data to read, and were to break; out of the while loop, and read the pending data, then you could change modes.

Sorry "re-read" (fat fingers on small tablet)

Use:

void blink2(ch) { if(Serial.available()>0&&Serial.read()== '1' || '2' || '+' || '-') { } Serial.println(ch); digitalWrite(ledPin,HIGH); delay(100); digitalWrite(ledPin,LOW); delay(100); }

The byte c variable is for "count".

I hope I helpt you

Ask and I will explain it to you

Leg_Ale:
Use:

void blink2(ch)
{
for(byte c=0;c<2;c++)
{
Serial.println(ch);
digitalWrite(ledPin,HIGH);
delay(100);
digitalWrite(ledPin,LOW);
delay(100);
}
}

The byte c variable is for “count”.

I hope I helpt you

Ask and I will explain it to you

There are several problems with that code. First, ch is not typed. Second, ch is never used. Third, that does NOT do what OP’s function did.

There was nothing in OP’s post that suggested that the pin should be toggled on and off just twice.

your right, Sorry Now the code is right

When I press the number 2 key, I would like the LED to blink until I press another valid key.
I'm able to achieve this when pressing number 2 key but once the blink2's while loop, I cant get out of there.
It's an infintite loop.
I dont understands why.
Can anybody help me please.
Regards.


/*
* SwitchCase sketch
* example showing switch statement by switching on chars from the serial port
*
* sending the character 1 blinks the LED once, sending 2 blinks twice
* sending + turns the LED on, sending - turns it off
* any other character prints a message to the Serial Monitor

do you want do let it blink to times or do you want to let it blink until annother keys is pressed??? Because you said both

try (not great code, but it will illustrate why you have a problem):

void blink2(char ch)
{
 do {
   Serial.println(ch);
   digitalWrite(ledPin,HIGH);
   delay(100);
   digitalWrite(ledPin,LOW);
   delay(100);
   if (Serial.available()) // Check to see if at least one character is available
   {
     ch = Serial.read();
   }
  } while(ch == '2');
}

You need an if statement in your code because he said that he wants an "valid" key.