HardwareSerial and analogRead suggestions

Good idea!

void analogRBegin(uint8_t pin)
{
// set the analog reference (high two bits of ADMUX) and select the
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
// to 0 (the default).
ADMUX = (analog_reference << 6) | (pin & 0x07);

#if defined(AVR_ATmega1280)
// the MUX5 bit of ADCSRB selects whether we're reading from channels
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
#endif

// without a delay, we seem to read from the wrong channel
//delay(1);

// start the conversion
sbi(ADCSRA, ADSC);
}

int analogRRead()
{
uint8_t low, high;

// ADSC is cleared when the conversion finishes
if (bit_is_set(ADCSRA, ADSC)) return -1;

// we have to read ADCL first; doing so locks both ADCL
// and ADCH until ADCH is read. reading ADCL second would
// cause the results of each conversion to be discarded,
// as ADCL and ADCH would be locked when it completed.
low = ADCL;
high = ADCH;

// combine the two bytes
return (high << 8) | low;
}

Done in 2 minutes, there might be errors ^^