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 ?
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.
}
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.
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.
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?
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.
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?
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.
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.
}