Using external power source

I have an Arduino Uno, a 9V battery and an adapter that is connected to GND and Vin. It immediately turned the lights on and is clearly giving power, but how do I program it to run my code with the battery as a power source and not my computer?

In other words, with a 9V battery and the appropriate accessories, how do I manipulate my code so my program runs while disconnected from the computer?

You don't need to change anything in the program, because it doesn't matter what you use as power source (PC, battery or potatoes).
But as I know Arduino is powered by 5.0v so you need a converter (L7805) or you can use 3 1.5v batteries (3*1.5 = 4.5)

Once the sketch (program) is uploaded to the Arduino, it will run stand alone as long as you provide power. There is no adjustment needed to make it run without the PC unless you're communicating between the two using serial. Even then, if you're just sending debugging output using serial it doesn't matter to the arduino whether there is a PC there listening.

Thanks! I ended up figuring out from some experimentation that I needed two batteries because just one wasn't strong enough.

I needed two batteries because just one wasn't strong enough.

One 9V battery should be plenty to power the Arduino. Whether it is enough to power all the stuff attached to the Arduino, too, depends on what that stuff is.

Keep in mind that 9V rectangular batteries belong in smoke detectors, not Arduino. The current per $ ratings are way too low to be cost effective.

4 AA batteries in a holder are about the same size as an Arduino, and will last a lot longer.

Wilfa:
Arduino is powered by 5.0v so you need a converter (L7805) or you can use 3 1.5v batteries (3*1.5 = 4.5)

I have concerns about this information.
The official website says that you can power up 6v-20v, but is recommended 7v-12v. Powering with VIN pin.
We just need to connect the 9v battery. Like this:

Am i right?
Oficial site http://arduino.cc/en/Main/ArduinoBoardUno:

"The board can be supplied with power either from the DC power jack (7 - 12V), the USB connector (5V), or the VIN pin of the board (7-12V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can damage your board. We don't advise it."

"If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may be unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts."

Yes, you're right. I was just thinking that Arduino is using the ATmega328P and ATmega328P need just 5v

Operating Voltage:
– 1.8 - 5.5V for ATmega48PA/88PA/168PA/328P. Sorry :relaxed:

you have to buy a lm7805 attach the input of the lm7805 to your batery, the ground to comond groun and the output to vin, and the comond groun to ground on arduino, an there u are, but no idea how is the consumition of the regulator xD

here u have a data sheet

http://www.fairchildsemi.com/ds/LM/LM7805.pdf

and u can power it from the power jack without anything xD 9volt batt -------- arduino jack