connect an arduino and PC through using USB. Then turn on the arduino sketch program and I can see the serial monitor printing the message "setup start" and "loop start".
I guess the “setup()” method is working whenever I open the serial communication.
What I want is to run only the “setup()” method when arduino’s first power is on and after that, whenever opening the serial communication I want only the “loop()” method to be run.
I searched everywhere through Googling but there is only way through hardware.(not for software) what I want is the software way, but If there is only way through hardware please tell me how. Thank you very much!
Ah so. What you don't want is for the Arduino to reset every time you open the console program. The Arduino is configured to reset on startup to enable a quick check to see if there is new code waiting to be uploaded. This is accomplished by the USB hardware monitoring DTR which is supplied by the monitor application. By default, the serial monitor within the Arduino IDE is configured with DTR on so every time you open this it will cause the unit to reset. You can get around this with a hardware fix that will disable the reset function. This also disables your ability to upload new sketches without having a hardware bypass circuit controlled say by a push button switch. The other solution is to use a terminal program (almost any will do) that allows you to switch DTR off, then you can connect/disconnect without triggering the unit to reset.
Witsie74:
I'm very interested to know how that works, since DTR in RS-232 is (was?) a hardware pin, and doesn't exist in the 2x data wire USB world. Is it mimicked in software?
DTR ... doesn't exist in the 2x data wire USB world.
Just like rx and tx are mimicked
Which is to point out that the two data wires of USB are NOT REMOTELY like rx/tx in the rs232 world, and a USB/Serial chip does a LOT of processing to make it seem like there is a serial port.
The arduino resets when you open a serial connection to it because of the "auto-reset" feature.
There are several ways to disable auto-reset:
Connect a relatively large (10uF) capacitor between the RESET and GROUND on the power header of the board.
Cut the RESET-EN solder jumper intended for this purpose (not all clones will have this jumper)
remove the auto-reset capacitor in between DTR (from the USB/Serial chip) and the RESET signal.
All of these will interfere with the automatic upload of new sketches; you'll need to either undo them, or manage to use the manual reset button at the right time during upload (you have about a 2s window)
connect to the Arduino using a USB-TTL cable that is connected to Rx Tx and GND
Now THAT'S a pretty neat idea, now that USB/TTL cables are less expensive than they used to be...