Serial monitor not giving correct variable value

Hi all, I Need to understand the serial monitor operation. I have this before setup. int n=18. In my main program I vary the value of n. Then as soon as I use the serial monitor the value of n is returned to 18. So the serial monitor must work like turning off the power because it starts the program from scratch.
This is annoying because the serial monitor is useful checking for bugs in the program. Is there a way around this by labelling n as something else. I am using 'int n' ?

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Post your full sketch, using code tags when you do

Which Arduino IDE do you use and which Operating System ?

When the Serial Monitor is opened, the board is reset. If the Serial Monitor is already open and the sketch is uploaded, then everything is normal.

To detect a reset, I sometimes do this:

Serial.begin(115200);
Serial.println("The sketch has started");

Try any other Serial Terminal program to connect to a running Arduino board without resetting it. In linux are a number of good programs already in the repositories.

Scope?

That is because you have defined at least two variables with the name "n". The serial monitor will reset the Arduino board so as to synchronize the monitor and the Arduino program. Of course when that happens, your original definition of n is restored.

@petercl14 - any update?

@petercl14 - any resolution?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.