while(true);

I'm trying to get a feel for using Strings and the Serial port and the Serial Monitor. I've been running variations of this short program:

void setup() { Serial.begin(9600); }

void loop() { String stringOne ="Hello";

while (Serial.available() > 0) { Serial.print(stringOne); while(true); }

//while(true); }

It works as written. I type something in the Send box of the Serial Monitor, hit Enter, and one Hello is printed out.

When both while(true) statements are commented out, the program loops out a continuous stream of Hellos as expected.

Now, when the first while(true) is commented out and the second while(true) is not commented out, whatever I type in the Send box of the Serial Monitor disappears when I hit the Enter key and there is no output. I don't understand what is going on here. If someone could please explain. Thanks.

You're too slow with typing. The Arduino checks once if a character is there and then goes into the endless loop #2. If you were fast enough to transmit one full character before the first check occurs (eg by adding delay(10000) to the setup() function) you will get one Hello and end up in endless loop #1.

Korman

You're too slow with typing.

80000000 words per minute won't help. Nothing is sent from the serial monitor until the Send button is pressed.

while(true); makes no sense. 'true' is a reserved keyword. See http://www.arduino.cc/en/Reference/Constants. Hope this helps.

What about it doesn't make sense, selby96? Maybe we can help you understand it.

'true' is a reserved keyword.

So?

while(true); makes no sense.

Sure it does. It creates an infinite loop that halts everything else, except interrupts, from happening.

It makes no sense in the context of the OPs sketch.

But what has that got to do with 'true' being a reserved word?

Besides, the construct is a useful debug aid - a sort of programmer's full-stop

Hey Guys - Don't beat me up! >:( I'm trying to help the OP, rather than arguing with you. He's already said he's trying to learn. I'm pretty sure he doesn't want to create an infinite loop. Just telling him that he can't type fast enough is not going to get him any further forward. He is obviously already aware that the problem is with the while(true) statement, so I was trying to point him to something that would help him understand why.

Hi bka,

Here is a fast run down why your program behaves like is dos:

This will keep saying "Hallo" once you send anything though serial

String stringOne ="Hello";

void setup(){
 Serial.begin(9600);
}

void loop(){
 while (Serial.available() > 0){
   Serial.println(stringOne);
   delay(500);
 }
}

Why? You are only checked if there is anything in serial and since you are not reading it it will never go away and therfor "Serial.available() > 0" will keep being true.

Try this:

String stringOne ="Hello";

void setup(){
 Serial.begin(9600);
}

void loop(){
 while (Serial.available() > 0){
   Serial.println(stringOne);
   Serial.flush();
 }
}

Why dos this only print "Hello" one time? I realy think this was the behavour you wantet from your program.

Serial.flush() removes everything you have sendt and therefor "Serial.available() > 0" will be false next time the program loops.

Now - what is wrong with this?

String stringOne ="Hello";

void setup(){
 Serial.begin(9600);
}

void loop(){
 while (Serial.available() > 0){
   Serial.println(stringOne);
   Serial.flush();
 }
 while(true){
   Serial.println("Forever is a loooooong time");
   delay(500);
 }
}

The program strats the loop. Is there anything in Serial? Nope. Is true true? Yep - and then you have created an infinite loop since true will never be false.

I hope this clears things up a bit.

-Fletcher

Thanks fellas...I'm slowly getting a handle on it. I was using while(true) to halt execution BTW. One more snippet I've been working on:

//String inByte = "Hello"; //String inByte; //byte inByte; int inByte; //char inByte; //byte inByte; //long inByte;

void setup() { Serial.begin(9600); }

void loop() { while(Serial.available()>0) { inByte = Serial.read(); Serial.write(inByte); //Serial.print(inByte); //Serial.print(Serial.read()); } }

As is, when I type an "a" in the Send box in Serial Monitor, it prints out "a" whether I declare inByte as an int, char, byte, long, etc. I'm not seeing how a variable declared as a number can print out a letter. Yet when I declare inByte as a String, I get a compile error!

Now, when I use the Serial.print() function instead of the .write(), now when I type "a" in the Send box, a "97" is printed out. ????? But String doesn't give a complie error!?!

I guess I'm asking:

Why do all the data types except String work Serial.write()?

How is the serial port, or whatever interprets what is on the serial port, "seeing" the data? As a number? Is that why String doesn't work?

Why does Serial.print() return the same meaningless value no matter what is input? There's obviously a difference between .write() and .print()...how do each interpret the serial data?

How do I get Serial.print() to echo what I type into the Send box?

Sorry if these questions are silly! I do appreciate the info.

Oh, one thing I forgot. When inByte is declared as a String and Serial.print(inByte) is used alone (no read), it works. But if i declare inByte as a String, as before, but assign its value through Serial.read(inByte) using the Serial Monitor I get gibberish. If I could understand this, I think I’d be good to go. Thanks.

Let the Help system be your friend. e.g.

write() Description Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.

Syntax Serial.write(val) Serial.write(str) Serial.write(buf, len)

Arduino Mega also supports: Serial1, Serial2, Serial3 (in place of Serial)

Parameters val: a value to send as a single byte

str: a string to send as a series of bytes

buf: an array to send as a series of bytes

len: the length of the buffer

See also ?read() ?print() ?println()