Electronic – Arduino sketch not running startup method

arduinowifiwifly

I am trying to use the WiFly shield ( http://www.sparkfun.com/commerce/product_info.php?products_id=9367) with Aduino Mega board.

I am making modifications to the built in examples that ship with the WiFly. I have modified the sketch to make an http request to my personal server.

When I run the sketch through the USB port attached to my laptop, everything works fine. I can see the requests arriving at my remote machine.

When I try to run the board independently using batteries, I run into issues. Basically, the code in the "setup()" doesn't seem to be running. If I do a reset on the board by pressing and holding the push button on the Arduino Mega board, it also works fine. This proves that there is no error in the Sketch or back end server and that the hardware is also not broken.

So basically, when I start up the board via the first start up from a battery power source, it doesn't work. But subsequently resetting of the board using the power button on the mega seems to work.

Does anyone know why this is caused and how I can start running my sketch as soon as the battery pack is turned on (rather than having to do a subsequent reset?)

I had a look at the Arduino trouble shooting guide here http://arduino.cc/en/Guide/Troubleshooting2 and tried connecting the RX pin to the TX pin as well as connecting the Rx pin to GND via a 10k resistor. This doesn't help either.

Edit

I am running the sample WiFly sketch provided by Sparkfun from here. I changed the URL in the sketch from google.com to myserver.com and it works fine when running via USB connection to the laptop or after a reset. I have since added a 2000ms delay as suggested in the answer by kmort and that seems to work fine now. So it would seem there are "settling down issues" with the shield.

Best Answer

When power is first applied, any values you read could be crazy, and are certainly suspect.

Are you making any decisions based on reading values in setup()? If so, it could it be that the rest of your circuit has not settled down yet, and you get interesting bugs because of it.

Try a delay() in your startup() and see if the problem goes away.