How to break an infinite loop in serial communicaton?

Hi,

I have been struggling with this problem for an long time, I googled many documents but I was not able to get any useful information. Arduino forum is my last chance :( I use MEGA2560 and trying to write a simple code. I want a LED to blink always unless I press a letter on the keyboard:

void loop() { int inByte = Serial.read(); .... if(inByte == 'c'){ while(1){ digitalWrite(4,HIGH); delay(500); digitalWrite(4,LOW); delay(500); if(inByte == 'v') break; } } }

But it just can't leave the infinite loop. I can't break it. What could be the problem. Am I missing something very simple here?

I realy need help from you. Thank you in advance.

you need inByte = Serial.read(); inside the loop!

You also need to call Serial.available() to make sure that there is at least one byte to read before you read a byte.

You also need to call Serial.available() to make sure that there is at least one byte to read before you read a byte.

Not necessary; 'v' != -1

Not necessary; 'v' != -1

Not strictly necessary, but still a good habit to get into. Not all Serial.read()s are so trivial.

mmcp42, PaulS, AWOL Thank you so much, really! Sometimes it is very hard to see even the simplest fact.