Uploaded sketch not working after power off

I am setting up a burglar alarm using Arduino Uno and Wifly Shield. When the switch triggers, it will send alert to my phone. It is working fine when you upload it for the first time from IDE. But after I power off the Arduino and power on it again it will not work anymore.

Although I am not sure, I guess the setup function is not being called where connection to the router is made. I am new to Arduino and this is my first project. Did I miss something? Could anyone one guide me on how to debug it?

My Arduino board serial is greater than 500000 and it is Uno so I think it is not the Smd edition issue(http://arduino.cc/blog/2011/01/20/issues-with-the-new-arduino-uno-smd-edition/). And I have the same optiboot_atmega328.hex as mentioned in the above link.

Edited: After a few more searches on Arduino, I found that I need understanding on bootloader. My understanding now is if you want to have the sketch running on Arduino even after power cycled then I need to buy an ISP to burn the sketch into Arduino. Please correct me if I am wrong.

Please correct me if I am wrong.

You are.

Although I am not sure, I guess the setup function is not being called where connection to the router is made.

I think it is more likely that the setup() function is called, but that it is not initializing the wifi shield correctly. Add an LED, with appropriate resistor, and some code to setup() and flash the LED in setup(), to see when that happens.

It sounds like a timing issue. Once the sketch is uploaded it should stay there.

One thing people do (as PaulS suggested) is to flash an LED a few times to indicate "startup" progress. eg.

1 flash - booted 2 flashes - found Wifly shield 3 flashes - connected

and so on.

PaulS, thanks for the guidance.

PaulS: I think it is more likely that the setup() function is called, but that it is not initializing the wifi shield correctly. Add an LED, with appropriate resistor, and some code to setup() and flash the LED in setup(), to see when that happens.

In that case of wifi shield not initializing correctly, why would it work when I uploaded for the first time? It only happen on power cycle.

I get it running by pressing the reset button after power cycle. But why do I need to press the reset button? I don't need to do it while running the Button Example to blink LED.

Could you point me to some reference URL that a beginner should read? I have read the foundation section from Arduino but I did not find some thing like the above case.

Thanks PaulS and Nick for taking your time to help me.

Like I said, it’s a timing issue. When you upload, or if you press reset, there has been time after power-up. Adding something like:

delay(1000);

… at the start of setup might be all that is required.

[quote author=Nick Gammon link=topic=100116.msg751815#msg751815 date=1333772039] ... at the start of setup might be all that is required. [/quote]

This my setup function and where should I put the delay? I tried to put delay at 1,2 and 3 but still need to press the reset button. And also tried with delay(2000);

void setup() {
  
// (1)
      pinMode(buttonPin, INPUT);
      pinMode(ledPin, OUTPUT);
  
      WiFly.begin();
 // (2)
      if (!WiFly.join(ssid, passphrase)) {
       
        while (1) {
          // Hang on failure.
        }
      }  
// (3)
}

Try larger delays - 10,000ms at all three places simultaneously. If it helps, scale them back as necessary. Confirm that the issue is there; put some Serial.Print statements in setup to show you where it goes.

If you're still stuck, you may need to take a look at the library code and add some debugging output there too.