How to make arduino distinguish which power source it is on

Hello I'm using an adapter for my UNO, but i want its millis() to work when power is gone So how can I make it use battery when power is gone, and switch back to adapter when it is available?

It would be much better and far more accurate to use a Real Time Clock module with a battery. The DS3231 module is the best available and runs for years on a single cell.

What is your plan for when the Arduino battery goes dead?

nimasa77: So how can I make it use battery when power is gone, and switch back to adapter when it is available?

Quick and dirty Arduino battery backup.

This does not address the distinguishing-which-supply-is-providing-power part, though. You could wire up an optocoupler with the main DC powering its LED and connect the open collecter output to a spare Arduino input (pullup enabled) and monitor that.

If you can derive 5V from the external supply source, simply hop that across toma spare digital input pin. When that pin goes low, the external supply has ‘gone’, and you can deal with it as desired.

The battery and external supply must be isolated from each other if they come in the same pin - try a schottky diode and sense the ext. volts before the diode.