I am guessing that the serial needs time to stabilize.
Sorry
Pommie , but that is simply not true in my view. In fact, I am not even sure what it means. If it needed to "stabilize", you could just put a delay at that point and whether it was two minutes or two hours, it would make no difference.
On the UNO, that line (while(!Serial) ) basically does nothing at all.
On the Teensy and boards like a Leonardo, it will cause a wait until the port has been opened by the user or a program on the other end (I am being a little simplistic) but that is the gist of it.
Sashvat Try this:
ON your UNO, run your program (with or without the line while(!Serial) ).
When you first run the program, watch the onboard TX and RX lines and do NOT open the serial monitor (do not click the square in the upper right hand corner of your screen). Notice that there is no action on the lights at all...UNTIL you open the serial monitor, then you see them flash away (and your program transmits strings).
So, on the UNO, your program does not need code to wait for the serial monitor to be opened, the IDE is going to do that on its own....whether you want it to or not.
Now, go to the Teensy, instead of using the line while(!Serial);, use this line:
while (!Serial.dtr()) ; // wait for the remote or user to raise the DTR line by opening the port on the remote end
Put the line in setup() after the line Serial.begin(9600); Your program will not use Serial UNTIL you open the serial monitor. Doing so will raise the dtr (Data Terminal Ready) line to "say" ok, I am ready to go. That is a normal and inherited way for that kind of communication to take place - software can override that, but it is the normal way it works. Using that code is, IMO, better because it specifically describes what is going on.
Two other points...
There is no shame in being a beginner, I have been programming for many years and will, on occasion, still feel like a beginner. The shame can come in when you stop there.
Go read about how Teensy does the
USB Serial object and you can see that it is VERY different from how Arduino UNO does it (hardware and software differences), but it aims to be software compatible with the Arduino Serial. It is also very different from the hardware serial that Teensy also has -
see here. I would advise you to take some time and read about that and think about the hardware differences as well.
The second point is simply this: If you understand the above, you will understand why your lines:
while (Serial.available() == 0) { } //wait for the user to give an input
are not a good way to do what you want to do. They are not illegal and it is NOT just because you did not write it as while (Serial.available() == 0); It is because it is not a strong way of doing what you want to do. As suggested in the Teensy links, use Serial.available as though it is returning a boolean...."ya got any data or not?" .
Hope this helps.