Sensing Voltage with a Digital Pin
You say you are using an Arduino Mega board which implies ATMega1280 or 2560. Both of these have an Analogue Comparator module which is built in. The Arduino boards route out the pin with the alternate function of "AIN1" to what Arduino call "digital pin 5".

If you enable the internal bandgap as the reference as the source for "AIN0" (something which is configured internally), then you can basically use a potential divider on D5 which is calculated so that when the battery voltage drops below a certain threshold, the centre of the divider (connected to the pin) drops below 1.1V. When this happens, the comparator inside the ATMega will change state and this event can be configured to generate an interrupt (ISR(ANALOG_COMP_vect)).

The comparator can be configured using the following C code (compilable with avr-gcc):

//Code to enable comparator
ADCSRB &amp;= ~(1 &lt;&lt; ACME); //Connect AIN1/D5 to the comparator
DIDR1 |= (1 &lt;&lt; AIN1D); //Disable digital functionality of AIN1 pin so we can use comparator
ACSR = (0 &lt;&lt; ACD) | (1 &lt;&lt; ACBG) | (1 &lt;&lt; ACIS1) | (1 &lt;&lt; ACIS0); //Enable comparator, connect bandgap, and set to rising edge sensitivity (AIN1 falls below AIN0)
//-------

//If you want to generate an interrupt, do this to enable it (make sure you have an ISR for ANALOG_COMP_vect):
ACSR |= (1 &lt;&lt; ACIE); //Enable interrupt.
...
ISR(ANALOG_COMP_vect) {
    //Do something when voltage drops too low
}
//-------

//If you want to do it via polling mode, do this to check the value
if (ACSR &amp; (1 &lt;&lt; ACO)) {
    //Do something if the voltage drops too low
}
//-------