I did more testing and the junk you get is sent to serial before the reset done by opening the serial monitor.
If you run this sketch and open a serial monitor at 115200, you will see some junk printed from loop() before the HI printed in setup().
void setup () {
Serial.begin(115200);
Serial.println();
Serial.println("HI");
}
int n = 0;
void loop() {
if (Serial.available()) {
Serial.println();
Serial.print(++n);
Serial.write("c=");
Serial.print(Serial.read(), HEX);
}
}
output:
öþ
5c=0
6c=0
HI
This seems to indicate that when the serial monitor is opened, some junk is sent to serial, and then the reset restarts the program which prints "HI".
This can cause big problems with a sketch that tries to wait for input like this:
void setup() {
Serial.begin(115200);
Serial.println("Type any character to start");
while (Serial.available() == 0) {}
}
int n = 0;
void loop() {
Serial.println(++n);
}
The output is:
Êa
F??
1
42
j
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
ÿType any character to start