Serial.println() truncation

I'm an infant here, a raw beginner. On Uno 3 Serial.println("Test string.") only prints the first two characters "Te" to the Serial Monitor. Running it a second time prints about half a dozen square box characters before "Te". All runs appear on the same line with no carriage return.

Whats' going on? Where should I look for a remedy ?

Welcome to the forum

Start by providing more detail

Which Arduino board are you using ?
Please post the sketch that you are testing, using code tags when you do

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Thanks for those hints about queries on this forum. Much appreciated. It's a short bt of code to clean up after other beginner coding attempts - turns off the pin 13 led and clear any interrupts. Other things may get added as I find more needs to be reset. It's a UNO 3 board - is this the correct way to nominate it?

boolean start = true;  //global variable

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (start == true) {  //which it will be the first time through
    Serial.write("Test string.");
    digitalWrite(LED_BUILTIN, LOW);
  }
  cli();
  start = false;  //toggle value of global "start" variable
                  //Next time around, the if test is sure to fail.
}

To print the string the Serial needs the interrupts, so your line cli() is a obvious cause of the issue. Remove it and try again.

As a general rule - do not use the language constructs if you don't understand it.

If you put a short delay of a few milliseconds inside the if loop, or just before the cli(), then you get an extra character printed for each millisecond of delay.

For delays of 10ms and above the complete message is printed.

But inserting delays is not a solution :slight_smile:
The cli() just haven't be there :slight_smile:

If you insert

Serial.flush();

after the Serial.write() then the sketch will wait until the output buffer is empty before moving on

See Serial.flush() - Arduino Reference

2 Likes

Be careful where you find things that need to be reset. I don't know of any need to

  cli();

Besides, cli() doesn't reset, it disables interrupts on a global basis and if you ever see it, there will be an sei() very soon along to turn them back on.

I recommend you just ignore interrupts, the concept and usage, for now. I've been at this for some time and almost never have to think about them.

It's def not Arduino 1.01 stuff.

a7

Thanks to all. Don't know where in th world you are on a Saturday night in Sydney, but thanks for your help.

Masking out cli or adding the 10 ms delay within the loop allowed the whole string to print. I now understand cli nulls all interrupts including serial output. So I added 100 ms delay before cli.

It now prints the whole string terminated with a cr, but still leads with an increased series of about 50 square box characters. Where do they come from?

why do you need the cli at all?

Sure, yes.

I have to point out that stuff that needs to be done once, on reset or powering up, could be and is usually just placed in setup().

a7

alto777
"It's def not Arduino 1.01 stuff"

I'm exploring this contemporary world of microprocessor usage, and can't help comparing it to near 30 & 40 years in the past. I'm not new to interrupts - programmed them on microprocessors in the 1980s & '90s and on IBM PCs before Windows. The 'user friendly' functions on Arduinos that hide raw processor operations is new for me. I really appreciate all these hints.

So… turns out not really. Here that might mean you don't know an if from a while. :expressionless:

Just look at some of the examples you can find in the IDE. Get creative and bring your prior knowledge out after you poke around a bit.

You might want to hunt down something like "Arduino for people who already know how to program".

There are some differences because Arduino.

There are some tricks because real time programming and sensors and buttons and stuff, things you may or may not have experience with.

Fortunately it's all still perfectly logical, and the language at the bottom of it all is C++.

All the source code that goes into anything you can compile and run is on your machine as such, and can make for fascinating reading.

a7

Is the series of square box characters before the println string due to output from the Uno 3 or due to something in the configuration of the Serial Monitor?

I think it is due to incorrect using of cli()
You haven't answered, why do you need to use it. Your code not dealing with interrupts.

As noted in the first response to assistence, this code was to clean up after other code. Not noted in that message was that some of the previous code attempts included interrupts. Since then on good advice I first tested it with a delayed cli then removed it.

You don't need something like this - the part of uploading a new code is to clean a controller completely

so please show your actual code

Why does it print a series of near 50 square box characters before the println string?

PS - In case it matters. C++ is something I've avoided in the past. Forth is my preference. So there's a few new things here for me "as an infant" to Arduino - C++ syntaxt, absence of interactive gradual development, a 'user friendly' shell that hides raw contact with the chip.

boolean start = true;  //global variable

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (start == true) {  //which it will be the first time through
    Serial.println("Test string.");
    Serial.flush();
    digitalWrite(LED_BUILTIN, LOW);
  }
  start = false;  //toggle value of global "start" variable
                  //Next time around, the if test is sure to fail.
}

It does not here, on an UNO, using Arduino 1.6.5 with the baud rate set to 9600 in the serial monitor window to match the call to Serial.begin().

a7

Appologies, I don't understand this at all. Why does baud rate 9600 not match Serial.begin()