Serial Commnuication ERASES variables on startup

Hello.
I’m having “problem” while starting serial monitor.
I have created sample to simulate my problem. And video.
The problem is:

  • I start arduino - loop()
  • Every 4 seconds the variables are set to random number and printed over serial.
  • If I start SERIAL MONITOR the values of variables gets DEFAULT (not values assigned in loop())

Is this normal behavior?
Does the START of serial monitor reset all the variable values?

In future I attend to use modbus and I’m worried that this behavior will write 0 (zeros) to R/W variables.

Please help…

arduino-com-start.mpg (527 KB)

Clears_On_Serial_Connect.ino (1.32 KB)

Serial communication does not erase variables. Bugs in your code do.
But you didn’t post your code, so there’s no way to help you.

Pieter

I had problems attaching video...
It seems like on serial monitor opening, the arduino resets / restarts..

Yes, Arduino boards without native USB support reset when the serial port is opened, because the DTR pin of the USB-to-UART converter is connected to the reset pin of the main microcontroller. This is how new code is uploaded.

If you don't want it to reset, either leave the port open on the computer, or connect a capacitor (10µF or so) between the reset pin of the Arduino and ground, to prevent it from automatically resetting. You'll have to remove it when uploading new code.
IIRC, there's a trace on the bottom top of the Arduino that you can cut to disable auto-reset, but it's quite permanent.

Another way to avoid resetting the Arduino when you open the serial port is to use a USB-TTL cable directly connected to Rx Tx and GND rather than the regular USB connection. Of course this requires an appropriate power supply for the Arduino.

...R

In final version I will use RS485 board.
If I will use RS485 and pins 1 & 2 + DE/RE pin will the arduino also RESET?

Thanks.

farcry1:
In final version I will use RS485 board.
If I will use RS485 and pins 1 & 2 + DE/RE pin will the arduino also RESET?

Please provide a diagram showing how you intend to connect things - I don't know what DE/RE means.

What Arduino board will you be using?

...R