I am learning Embedded C via tutorials. Current topic is IF and ELSE IF statements. Sketch is below. (Can I attach a screen shot here? If so how?) Most is commented out. At the bottom is the Serial.println text "Y is from 51 to 100". This displays as expected. However often it is pushed off my screen to the right preceded by an entire row of square filler characters. I don't see any syntax errors. If I press reset un my UNU R3 it goes away initially but then returns in subsequent executions? I am running W10. Processor AMD A6-4400M APU with Radeon(tm) HD Graphics, 2700 Mhz, 2 Core(s), 2 Logical Processor(s). It is an older machine (HP) but otherwise works fine. Could it be contention for the port COM4?
int x = 51;
int y = 53;
void setup() {
// THERE IS NO "THEN" STATEMENT. ONLY THE NEXT STATEMENT OR GROUP OF STATEMENTS FOLLOWING THE "IF" IS EXECUTED
// FOLLOWED BY THE NEXT LINE AFTER THE IF OR GROUP.
Serial.begin(9600);
/*
if (x == 50) {
Serial.println("The value is 50");
Serial.println("This is a 2nd line in the IF group.");
Serial.println("****");
} else {
Serial.println("The value is NOT 50");
Serial.println("This is a 2nd line in the ELSE group.");
Serial.println("////");
}
Serial.println("Back to sequential outside the IF / ELSE group.");
*/
// YOU CAN ALSO HAVE NESTED IFs VIA THE "ELSE IF" STATEMENT AS FOLLOWS:
if (y > 100)
Serial.println("Y is over 100");
else if (y > 50)
Serial.println("Y is from 51 to 100");
else if (y > 25)
Serial.println("Y is from 26 to 50");
else
Serial.println("Y is 25 or less");
Serial.println("+++ End of Sketch using SETUP only +++");
}
void loop() {
}
That happens to me too, and you probably just have to live with it.
Random unprintable characters (or zero bytes) somehow end up in the input buffer during startup, before the serial connection is finalized. It happens with a terminal program too, so it is not a fault with the serial monitor.
Start your program with
Serial.begin(rate);
while (!Serial);
Serial.println(); //push the junk up a line
Try adding 'delay(200);' after 'Serial.begin(9600);'. That will keep the sketch from sending text between the time it starts after an upload and the time it re-starts when Serial Monitor connects.
The standard Baud rate 115200 wastes much less time printing. Just make sure that the rate you use matches the rate set on the serial monitor (or terminal program).
Thanks to all: jremington (3 lines of code), red_car (match baud rate), johnwasser (delay). I have been away from this for a while. At time of replies I didn't know what matching baud rate meant once I set it in my code. Subsequently I discovered how to set it for the serial monitor. The DELAY statement alone seemed to have no effect but matching the rate settings and the 3 lines of code (//push the junk up a line) seem to have nailed it. Now can anyone tell me how to include this code in the default starter sketch (void setup and void loop) every time I begin a NEW sketch) . Not sure where to find it or if I can update it.
Thanks again.
Brewsterblock
Hi @brewsterblock. Please tell me which version of Arduino IDE you are using. That will allow me to provide you with the correct instructions for customizing the code of the sketch created via File > New in Arduino IDE.