AnalogRead reading different numbers when adding another analogRead in the loop

Im having trouble with analogRead on the Due where the reading changes when adding another analog read in the loop.

#define BATTERY_SENSE 59
#define BATTERY_READING_OFFSET 0.011
#define BATTERY_RESISTOR_MULTIPLIER 9.7736625514

#define VMOTOR_SENSE 58
#define VMOTOR_READING_OFFSET 0.014
#define VMOTOR_RESISTOR_MULTIPLIER 11.1111111111


void setup() {
  pinMode(BATTERY_SENSE, INPUT);
  pinMode(VMOTOR_SENSE, INPUT);
}

void loop() {
  SerialUSB.print("Motor Voltage: ");
  SerialUSB.print(readMotorVoltage(), 10);
  // SerialUSB.print('\t');
  // SerialUSB.print("Battery Voltage: ");
  // SerialUSB.print(readBatteryVoltage(), 10);

  SerialUSB.println();
}

double readBatteryVoltage() {
  analogReadResolution(12);
  return ((analogRead(BATTERY_SENSE) * (3.288 / 4095)) - BATTERY_READING_OFFSET) * BATTERY_RESISTOR_MULTIPLIER;
}

double readMotorVoltage() {
  analogReadResolution(12);
  return ((analogRead(VMOTOR_SENSE) * (3.288 / 4095)) - VMOTOR_READING_OFFSET) * VMOTOR_RESISTOR_MULTIPLIER;
}

with the code above serial monitor prints:

Motor Voltage: 6.0002442002

which is the correct voltage on that pin. But if i upload again the code above but with the commented code uncommented the serial monitor now prints this:

Motor Voltage: 5.3935856736 Battery Voltage: 4.2165012285

And no the actual voltage did not change, even in the function the only variable that changes is the analogRead, so something is happening there. I also could not simply offset it again because i do not know the situations when it can happen or not happen .

Any ideas?

I don't know about the Due, but successive analogRead()s on different pins on AVR based boards needs settling time between the reads. The usual solution suggested is to read the value from the same pin twice and discard the first value.

UKHeliBob:
I don't know about the Due, but successive analogRead()s on different pins on AVR based boards needs settling time between the reads. The usual solution suggested is to read the value from the same pin twice and discard the first value.

Reading the pin twice worked ! but not when using a delay (even 1 second delay).

Is there something wrong with how analogRead function is handled that arduino never got the chance to fix?

Is there something wrong with how analogRead function is handled that arduino never got the chance to fix?

On AVR based boards there is only one ADC which is switched between inputs as required, hence the time needed to do it. My bet is that the ADC is not connected to the pin until required hence a delay() between reads on different pins has no effect

UKHeliBob:
On AVR based boards there is only one ADC which is switched between inputs as required, hence the time needed to do it. My bet is that the ADC is not connected to the pin until required hence a delay() between reads on different pins has no effect

It would seems that the arm chip that the uses is the same way.

UKHeliBob:
On AVR based boards there is only one ADC which is switched between inputs as required, hence the time needed to do it. My bet is that the ADC is not connected to the pin until required hence a delay() between reads on different pins has no effect

Also i have am loosing some accuracy (though this can be fix by adjusting the offset) i am loosing 0.06v of accuracy when i am double reading.

John41234:
Also i have am loosing some accuracy (though this can be fix by adjusting the offset) i am loosing 0.06v of accuracy when i am double reading.

How do you know?

"Also i have am loosing some accuracy (though this can be fix by adjusting the offset) i am loosing 0.06v of accuracy when i am double reading."

So what is the mathematical accuracy of the below operation in your code?

(3.288 / 4095)

SerialUSB.print(readBatteryVoltage(), 10); // wishful thinking
You're not even getting three decimal places with a 12-bit A/D and a voltage divider.

What are the resistor values of those dividers.
A >= 10k impedance causes a longer settling time after switching analogue inputs.

Also note that a Due has no (accurate) internal reference voltages.
It uses the potentially dirty supply voltage of the MCU as reference.
Leo..