Detect the voltage

Can I try rewording this toyew?
You have a Mega with an input voltage connected to A0.
your code indicates you have a reference voltage of 2.8V
but you will be using the "default" which is Vcc.

The lowest voltage you can use to power the Mega is 5V.

The power pins are as follows:

Vin. The input voltage to the board when it's using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin.
5V. This pin outputs a regulated 5V from the regulator on the board. 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 you are connecting ANYTHING to the AREF pin you will most likely damage your board.

Can you provide a schematic please so we can see how you have connected the board up