Autorun the last upload when powered by 9V

So I'll be the first to admit I'm new at all of this, including coding in C, and perhaps this was answered somewhere else but I'll give it a shot anyway...

I'm looking to have my Ardunio Duemilanove automatically run the last upload I gave it when I power it up to a 9 V battery. I can achieve this if I simply strip my code of all functions and just run a simple loop, but I only want the the upload to run once (reason: I have an altimeter attached and it starts passing back values once it and the arduino are powered on). Is there a simple addition I can make to my code to achieve this?

Anything help, thanks!


Not sure I completely understand your question but code you place in the setup portion of a sketch will only execute once while the loop portion or the skectch can be a do nothing loop. Will that work for you?


Yeah sorry, probably not the greatest explanation. I'll try a little better...

Basically, my code consists of several functions, the primary one(s) consisting of one that reads and processes the altimeter data that is passed in and another one that does something based upon the processed values, in this case, it drives a motor.

So I want to be able to upload to the board via usb, power down, and then when I power it up again with the 9 V, it sits there reading the altimeter data and responds accordingly.

I suppose I could nest things inside the setup portion of the sketch, but I thought maybe there was a simple way to tell the ardunio to automatically begin reading the data that is passed into it from the altimeter and react accordingly.

Hope that makes sense. Thanks again.


Well I'm sure the Arduino can do what you wish. It sounds like you are struggling with what the program flow and structure should look like.

Perhaps if you first draw out a pictorial flow chart it might help you. Once you upload a program into a Arduino, it is always ready to start running from the beginning once power is applied or any time the reset switch is pushed, that part is automatic. It just up to you to figure out the flow and loops you require to perform your steps.


Would a toggle switch connected to an input pin work?

If the pin is high, run in "9V mode". If the pin is low, run in "USB mode".

  • Brian

The arduino handles power switching automatically. If there is sufficient voltage coming from the external power connector then it uses that power, otherwise it uses the +5vdc from the USB connector if that is plugged in, if neither is connected then the Arduino is powered off.

I think Brian was trying to say "9v mode" means don't gather the altitude in the setup() function. "USB mode" means do collect the altitude. The modes referred to processing modes, not power modes. So set up a simple switch on an input pin that an IF statement in the setup() part of the program can test and use to gather the altitude data or not.

Power the Arduino via USB and upload the sketch and run it once. Then disconnect the Arduino from USB, toggle the switch, connect the 9v. battery, and restart the sketch. Sounds to me like the way to go.

Exactly what TBAr said (thanks for the better wording). A toggle switch to indicate the run mode.

  • Brian

Sorry, there is one more part to this that I neglected to write down before. You will need to send the altitude data from the USB mode to the 9v mode through EEPROM.

So when the switch is one way, collect the altitude data and write it to EEPROM. When the switch is the other way, read it from EEPROM back into your variables. Your variables go away when you power down. EEPROM holds data without power.