So I'll try to answer the questions I know before going onto the things I'm more unsure of. To begin, let's outline the differences between Vin, Vcc, and AVcc. Looking at this image http://i.imgur.com/Ap12L.png
, you can see that vin leads into the ncp1117 voltage regulator, which drops the Vin down to 5V. So any battery you plug into the barrel jack connector or Vin pin will be fed into the voltage regulator and be converted into 5V. Looking at page 3 of the atmega328 data sheet (http://www.atmel.com/Images/doc8161.pdf
), you can see that Vcc is the digital supply voltage pin for the 328 chip itself. The 5V from the voltage regulator output will be connected to this pin to provide the Arduino with power. Looking at page for of the atmega328 data sheet, you can also see that AVcc is the supply voltage pin for the analog components of the 328 chips such as the A/D converter. The data sheet also says that AVcc should be connected to Vcc through a low pass filter. This allows for more accurate A/D conversions.
Now let's break down retrolefty's code, focusing on the following groups of code:
const long InternalReferenceVoltage = 1115L; // Adjust this value to your boards specific internal BG voltage x1000
The internal bandgap voltage should nominally be 1.1V, but is rarely ever perfectly 1.1V. Therefore you should measure your bandgap voltage (let me know if you need to know how), multiply it by a thousand, and insert it where the "1115" is. Retrolefty's bandgap must have been 1.115 volts.
// For 168/328 boards
const long InternalReferenceVoltage = 1056L; // Adjust this value to your boards specific internal BG voltage x1000
// REFS1 REFS0 --> 0 1, AVcc internal ref. -Selects AVcc external reference
// MUX3 MUX2 MUX1 MUX0 --> 1110 1.1V (VBG) -Selects channel 14, bandgap voltage, to measure
ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);
This chunk of code essentially sets up an analogRead() with some custom parameters. It sets the analog reference to the AVcc pin, and then tells the arduino to read the ADC channel connected to the internal bandgap voltage. The next few lines in the code start the reading and waits for it to finish.
Unfortunately, I don't exactly understand what is happening in the following line of code where all the magic happens:
int results = (((InternalReferenceVoltage * 1023L) / ADC) + 5L) / 10L; // calculates for straight line value
However, retrolefty says it computes the voltage at the AVcc pin, which is simply the filtered, regulated five volts discussed above.
Looking at your usb serial monitor output, it seems like you are getting Vcc voltages of 4.74 volts. Looking at the voltages you pasted in your original post, the USB gives you a Vcc of 4.72V, a dead 9V battery gives you a Vcc of 4.60 volts, and a fresh 9V gives you a VCC of 4.74 volts. What you may have to do is find a correlation between Vcc an Vin. Then you can put your cutoff point in the code. Alternatively, the TMP36 is an analog temperature sensor, correct? You can run the sensor off of the 5V pin, and correct for the deviation in low power by measuring Vcc. Let me know if you want to know more about that.
I'm sorry if you knew most of this stuff, it's a bit of a long post. Hopefully it helps!