Ask for the setup() function!!!

Dear all,

I am coding for Arduino. I read in here setup() - Arduino Reference
They said that the setup() function will only run once, after each powerup or reset of the Arduino board. However, in my code, the setup() function is always called when I open the serial port. I want to this function is called one time when I upload to board.
This is the code example:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(“Setup Function”);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(“Loop Function”);
delay(1000);
}

You can see more details in the video:

Do you know why the setup() function is always called when I open Serial Port? And How do we call this function one time?

Thank you so much for your help!

Do you know why the setup() function is always called when I open Serial Port?

Because that is the expected behavior - opening the serial monitor resets the processor.

Hi AWOL,

Do you know can we run the setup() function one time? It is really different with PIC processor. It always calls the setup() function when I run a program. Thus, it has some problems for me.

Do you know can we run the setup() function one time?

One way is to disable the auto reset - check the documentation for your board, but be aware that this will make reprogramming a little trickier.

Thank you so much, I will check my board!

diamondvu:
Do you know can we run the setup() function one time? It is really different with PIC processor. It always calls the setup() function when I run a program. Thus, it has some problems for me.

If you use a USB-TTL cable to communicate between the PC and the Arduino rather than the regular USB cable then the RESET won't be triggered. Just connect the USB-TTL cable to Rx Tx and GND.

...R

Maybe you can have a function that writes a variable to PROGMEM after it has run once.
Whenever the Arduino starts, you check the value of this variable, and if value = true (or whatever), then make it so that the function does not run.

I think this is possible.

ArduinoBasics:
Maybe you can have a function that writes a variable to PROGMEM after it has run once.
Whenever the Arduino starts, you check the value of this variable, and if value = true (or whatever), then make it so that the function does not run.

I think this is possible.

EEPROM yes, PROGMEM no.

One method I use is by maintaining a version number in EEPROM. Keep a version const defined in your code, when you upload new, with new version, you check on startup and compare stored version with current version and if different, run your code. Otherwise, each time you power up the unit, the version check will be true and you can skip the 'run once' segment.

Also there's a difference between power-up reset and the reset you see caused by opening the Serial Monitor. Opening a terminal that utilizes DTR will cause Arduino to reset. If you use a terminal program where you can switch DTR off, iy will have no effect and you can open/close without resetting.

Dear all,

I tried to connect the Arduino board with PC through the UART to USB cable as the photo below. But I have not read any data from COM port. Do you know why?

Photo:
https://drive.google.com/open?id=1X-hC8O46kphwtKY-hn5zrTYC9oxkp56X

Thank you so much!

Rx and Tx swapped?

Looks like you have 5V connected from Arduino to adapter; isn't the adapter powered from the USB port?

Dear sterretje,

It is working now. The wire is connected correctly. The problem is that I connected the UART to USB cable to USB 1.0 port, and it is still with USB 2.0 port in PC.

Thank you so much for your help!

Best Regards