# Unable to print text on the serial monitor using the Teensy 3.2/3.6

#### Sashvat

##### Member
Hi guys, I just ran a code I made long time back to test it on my teensy, a very simple program, the user inputs the number of times a green LED and a red LED has to blink and the LED blinks that many number of times, but when I open my serial monitor when using my teensy's, nothing comes, no prompt message, only when I give a value, the next prompt comes for the other LED and blinks the correct number of LED's. Only when it goes through the loop for the second time then it says "how many times do you want to blink the green LED?".

I tried it on my Arduino Uno, and it works just right, what could be the problem? The Arduino file is here, let me know if I have done anything wrong.

#### Attachments

• 1.9 KB Views: 7
Last edited:

#### Pommie

##### Well-Known Member
I am guessing that the serial needs time to stabilize.

Code:
Serial.begin(9600);
while(!Serial);
Mike.

#### Sashvat

##### Member
I am guessing that the serial needs time to stabilize.

Code:
Serial.begin(9600);
while(!Serial);
Mike.
Thanks a lot it works now! but why does it need to stabilize? Whats the reason behind serial needing to stabilise?

#### Pommie

##### Well-Known Member
Because the chip does the USB to serial conversion itself and after a reset it needs time to reestablish the connection.

Mike.

#### Sashvat

##### Member
Because the chip does the USB to serial conversion itself and after a reset it needs time to reestablish the connection.

Mike.
ok so unlike Arduino having an onboard USB to serial, this has it inside the Microcontroller, and that's what's taking time for it to register on the serial monitor.
ok so what is this while(!Serial)? shouldn't while be like while(){ }?

#### Pommie

##### Well-Known Member
Guess you don't do C.

Mike.

#### Sashvat

##### Member
Guess you don't do C.

Mike.
then I wouldn't have been able to write the Arduino file I have uploaded

#### be80be

##### Well-Known Member
Then you wouldn't of ask this while(){ }?
And would see why this is done
while(!Serial);
! makes it wait till serial starts up
() nothing {} and that has nothing so while would do nothing LOL

! Logical NOT results in a true if the operand is false and vice versa.
while (condition) so it has to have a condition (!Serial) which is say in while loop till serial starts
while (boolean expression) { // any statement(s) }

Last edited:

#### Pommie

##### Well-Known Member
In C, any conditional instruction can only have one other instruction following it. Or, you can group instructions together by surrounding then with {}. In the instruction above no additional instruction is required hence the single ;. I wish they'd start teaching this in colleges and universities.

Mike.

#### DrG

##### Active Member
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.

Mike.

#### be80be

##### Well-Known Member
I wonder where nothing would be at

#### DrG

##### Active Member

Mike.
I believe you. But, while I quoted your clearly incorrect guess and could have included the equally incorrect elaboration on your guess quoted below:

Because the chip does the USB to serial conversion itself and after a reset it needs time to reestablish the connection.
I was more interested in explaining the reason why the code statement "works". I admit that I was wordy and maybe I simply should have said that you need to assert the DTR line for the Teensy and left in a few links, but I distinctly disliked your attitude where you first tell the OP that

Guess you don't do C.
I wish they'd start teaching this in colleges and universities.
When you talk down to a user AND you are completely wrong, I am inclined to point it out, but I'm sure that you are ok with that.

#### be80be

##### Well-Known Member
I just have one thing to say about this I looked at hardware serial for both uno and everything else I cant see why
while (! Serial); will not work the same on the Teensy
And I see the dtr
Read the DTR signal state. By default, DTR is low when no software has the serial device open, and it goes high when a program opens the port. Some programs override this behavior, but for normal software you can use DTR to know when a program is using the serial port.
The teensy has both dtr and rts but from what i see it still can
Use while (!Serial) ; // wait for serial port to connect. Needed for native USB

It has
Native USB
If you look at the files it adds it doesn't add Serial it's using the arduino Serial libraries it does add to them the dtr and the rts

This kind of clearly tells it worked
Thanks a lot it works now! but why does it need to stabilize? Whats the reason behind serial needing to stabilise?
Only thing I see is maybe stabilize maybe the wrong word
Maybe wait to connect

Last edited:

#### Mickster

##### Well-Known Member
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.
This is IMHO a pretty reasonable statement initially DrG, and understandable when the first part, in bold, is taken solely into consideration, but the latter part , in bold, is in contradiction to the former. If there is no shame in being a beginner, why should there be shame if someone chooses to stay there?

I wonder whether you may have intended to say something different, please clarify if so.

#### DrG

##### Active Member
I just have one thing to say about this I looked at hardware serial for both uno and everything else I cant see why
while (! Serial); will not work the same on the Teensy
And I see the dtr

The teensy has both dtr and rts but from what i see it still can
Use while (!Serial) ; // wait for serial port to connect. Needed for native USB

It has
IT DOES WORK ON THE TEENSY that has been spelled out and it works on the Teensy because it forces a wait until DTR is asserted and that is done by opening the serial monitor. Until DTR is asserted, the Teensy will not use Serial - and you will not miss anything that has just come in at the beginning of execution. As I stated I prefer while (!Serial.dtr()) ; because it is more informative. Also remember, using that statement on a Teensy is their board code.

It has absolutely nothing to do with stabilizing, waiting, resting or any other such nonsense. As far as I can tell, the Teensy is ready to go and waiting for the DTR signal when it completes booting. If it need time, then you could simply insert a delay. But inserting a delay, no matter how long, will not work, because it needs the DTR signal, which occurs when you open the serial monitor.

On the UNO, as far as I can tell it does nothing at all and is not needed. In fact, when you see the statement "while (! Serial); " in Arduino code it often times has the comment // only needed for Leonardo.

Now, as an aside that is separate from the discussion, when you open the serial monitor on an UNO it does a kind of reset and you can see that in the board lights, but again, that is a complete different situation and it is on the UNO.

This is not a new situation. In addition to the previously included Teensy links, even the Arduino documentation, which does not apply to the Teensy except as the Teensy people strive to make it compatible, here spells it out - but you have to understand that the port is 'open' or 'ready' after a DTR. If you want to see the explanation elsewhere look here and probably a bunch of other places.

Now, I will admit as much if I am wrong, but I don't think I am and I also think that claiming that it needs to stabilize etc... is just plain wrong and that should also be admitted.

#### DrG

##### Active Member
This is IMHO a pretty reasonable statement initially DrG, and understandable when the first part, in bold, is taken solely into consideration, but the latter part , in bold, is in contradiction to the former. If there is no shame in being a beginner, why should there be shame if someone chooses to stay there?

I wonder whether you may have intended to say something different, please clarify if so.
All I meant was that, as a beginner, you frequently want to know why...and that is so often acceptable and encouraged and so on - and the "shame" is in when you stop asking why, regardless of your experience and skill level.

#### be80be

##### Well-Known Member
Uno doesnt have Native USB

#### DrG

##### Active Member
Uno doesnt have Native USB
Yes, you are correct. But, remember, the OP wanted to know why his code worked fine on the UNO but would not work the same way on the Teensy....