ArduinoDuemila - Understand Setup (for rookie :) )

Hi,
I'm looking for informations relative to the globalfunction setup. I'm understanding the purpose of this function at high level. But I'm seeing strange behavior (behavior that I can't explain) : during program loading on the card some pins act "randomly". Imagine a led or a motor connected to the pins then they switch on/of "randomly". I'm curious and I've made some test : to try to avoid this (for easy usage during testing) I've move my setup code in my loop function but making attention that this code only execute one time and only one. But here, pin are not setted correctly (strangle voltage output taht can set on a LED).

So my questions :

  • what is the exact actions done during setup ? and pin positionning mode (Input/output) ;
  • how to avoid random action during my code downloading in my Arduino.

Imagine my card with a motor, I don't want my motor act during code load.

Sorry if I ask totally studid question here : I'm only play with the Arduino since a month (and I like it :slight_smile: ).

[Edit] : maybe I've post in the wrong place !!! It'll be better in the Troubleshooting.

The Arduino IDE performs some "magic" before it starts the gcc compiler, among which is adding this:

#include <WProgram.h>
int main(void)
{
   init();
   setup();    
   for (;;)
      loop();        
   return 0;
}

So don't let you become fooled by fancy ideas. What's inside init() you can find by looking into:
Arduino/Hardware/Arduino/Cores/Arduino/Wiring.c

The digital pin 13 may blink as the bootloader flashes it everytime the atmega is reseted, and the digital pins 0 and 1 will vary a lot as they are used to upload the sketch to the board using the usb-serial chip.

Thanks for theses replies I'll check this as soon possible to verify that my components are connected to given pins.

All the IOs are floating at power up (or at reset).
Inside Init() digital IOs are programmed as input by default so they are floating until you fixe their state as output in setup().
If you want your IOs put in some known state at power up, you should put pullup or pulldown resistors on each pin.

The startup is a very awkward situation.

This is a little bit OT but nevertheless should be discussed from time to time.. In one of my designs I needed a relay to switch on the power for the external devices after initialisation of the ports.
This has not only to do with undefined outputs but also with inputs. When external devices are already powered up and the Arduino is not, a much higher voltage (=5V) than Vdd (=0V) is seen at the inputs, which can easily destroy those pins...

When external devices are already powered up and the Arduino is not, a much higher voltage (=5V) than Vdd (=0V) is seen at the inputs, which can easily destroy those pins...

Should use buffers with hot plug capability on that pins

Can you give an example?
I think you like 4049/4050?
What with analog input?

I once considered 4066... In fact I dont remember why I discarded that. It looks like an excellent idea at the moment....