Arduino automatic power supply selection

I have cloned an Arduino with an ATMEGA328 IC and added a voltage regulator to control a robot arm. It works if I connect a 9v battery to the voltage regulator which is then connected to the AVCC pin as 5v. It also works if I connect a wall adaptor 5v output straight to the AVCC pin. How can I create a circuit to automatically determine which supply is being used so the other power circuit is cut? I want the user to be able to use a battery or a wall supply without having to use a switch. If I have to use a switch, could I use an SPDT toggle switch?

The UNO uses a MOSFET circuit to block USB power when power is supplied from VIN or the 5V pin, look at the schematic just above the "Rev3".

For the quickest and simplest solution you'll need to ditch the 5V adapter and go with a 9V or 12V adapter instead and just diode OR the two to the input for the onboard regulator. The diodes will isolate the two supplies so that one can't feed into the other. If you prefer to have one disable the other things become a bit more complicated.

There are many designs varying in complexity using mosfet's or more advanced regulators but quite honestly the easiest way would be to use a small SPDT relay. If your AC adapter is going to be your main supply the you would wire the positive battery terminal to the relay's common terminal and the normally closed terminal of the relay to the regulator input. You will still need to use the diodes for isolation. With the AC adapter as your dominant supply your relay's coil should be rated to handle the adapter's output. Just wire one side of the relay's coil to ground and the other to the connection between the AC adapters output and the diode. Now when you plug in the adapter it will energize the relay and disconnect the battery. I hope this helps.

I agree that blocking diodes would be the simplest and cheapest solution, trouble with diodes is they drop about 0.7 volts (0.3 with schottkys) so you end up with 4.3 or 4.7 volts. If you download a sketch that has analog functions expecting a 5 volt AREF you can spend a lot of time debugging and pulling your hair out.

Use 7--9V supplies and diodes into Vin? The diode voltage is dropped before the regulator then.

BTW you must never power Avcc if Vcc is not powered, nor Vcc if AVcc is not powered - they need to be
keep very close in voltage (back to back schottky would do this if not shorting them together).