I got something that i don't understand. I write a really small program which is bellow :
int i;
void setup() {
Serial.begin(9600);
Serial.println("Begin of the program");
}
void loop() {
Serial.println(i);
i++;
delay(1000);
}
So i expect when i open an close the serial monitor to see to counter going up and up but for some reason if i close it and re-open it see my loop starting from the beginning again ( See printscreen bellow ).
Does anyone know how to avoid this behavior (what should i add to my program) ?
Performing a reset is the standard behavior of the Arduino IDE when opening Serial Monitor. There is nothing you can do within your program to prevent that.
vdiallonort:
Does anyone know how to avoid this behavior
After you upload your program to the Arduino, disable auto reset upon serial connection by putting a 10uF or larger capacitor with positive side to RST and negative or striped side to GND. Remove the capacitor the next time you want to upload.
ATmega32U4-based boards do not auto reset upon serial connection. So if you get a Micro, Leonardo, Pro Micro, Beetle, etc. it will not auto reset when you open serial monitor.
Thanks guy ! So if it's a behavior from the IDE if i watch the Serial through a program i should not have this behavior too ? I also gonna try with the capacitor.
It is not behavior of the IDE. It is opening the serial connection from any program which causes auto reset. Arduino Uno and Mega have a jumper trace you can cut called RESET-EN. If you cut the trace between the pads it will no longer auto reset. You can solder in place jumper pins or a switch, or just put a solder blob across the pads to re-enable it. When auto reset is disabled, you would have to press and release the reset button during upload for uploading to work. The capacitor is the way to disable auto reset without modifying the board.