Due analogRead/delay related problems -- presumably no sample-hold-issue!

hi

i hope i'm not adressing an issue that has been discussed thoroughly already -- so far i haven't found anything yet.

what i'm trying to do is very basic:
reading data from multiple analog input ports and displaying the values on the PC (transfer via serial connection).

the problem that appears:
everything works fine, when putting NO delay between the analogRead statements, but when i have a delay in between, random values (mostly the same on 2 or more ports) are displayed.

i'm aware of the fact that the capacitor of the sample-hold needs to charge when using high-impedance inputs, but first of all, i am not using more than max 10k input impedance (potentiometers) on the inputs and secondly, two readings on the same input à la

analogRead(A0);
delay(10);
val0 = analogRead(A0);
analogRead(A1);
delay(10);
val1 = analogRead(A1);

will not work either.

to make things a little clearer:

i used the hardware setup as depicted in the attached png. (i haven't used fritzing before and didnt want to spend too much time on it now, so please be gentle ^^). the voltage source is actually not a battery, but a power supply, delivering constant 3.3 V. the resistors of the voltage divider are R1 = 8.05k and R2 = 1.56k, so the voltage on pin0 should be 3.3*1.067/(8.056+1.067) = 0.385 V.

now, this code works just fine:

//declare ports
const int analogPin0 = A0;
const int analogPin1 = A1;
const int analogPin2 = A2;

int pin0Val = 0, pin1Val = 0, pin2Val = 0;
int pin0mV, pin1mV, pin2mV = 0;

void setup() {
  Serial.begin(19200);
}

void loop() {

  pin0Val = analogRead(analogPin0);
  pin1Val = analogRead(analogPin1);
  pin2Val = analogRead(analogPin2);

  pin0mV = pin0Val*3300/1023;
  pin1mV = pin1Val*3300/1023;
  pin2mV = pin2Val*3300/1023;
  
  Serial.print(millis());
  Serial.print(',');
  Serial.print(pin0mV);
  Serial.print(',');
  Serial.print(pin1mV);
  Serial.print(',');
  Serial.println(pin2mV);
  delay(500);
}

whereas this doesn't:

//declare ports
const int analogPin0 = A0;
const int analogPin1 = A1;
const int analogPin2 = A2;

int pin0Val = 0, pin1Val = 0, pin2Val = 0;
int pin0mV, pin1mV, pin2mV = 0;

void setup() {
  Serial.begin(19200);
}

void loop() {
   pin0Val = analogRead(analogPin0);
  delay(10);
  pin0mV = pin0Val*3300/1023;
  
  pin1Val = analogRead(analogPin1);
  delay(10);
  pin1mV = pin1Val*3300/1023;
  
  pin2Val = analogRead(analogPin2);
  delay(10);
  pin2mV = pin2Val*3300/1023;
  
  Serial.print(millis());
  Serial.print(',');
  Serial.print(pin0mV);
  Serial.print(',');
  Serial.print(pin1mV);
  Serial.print(',');
  Serial.println(pin2mV);
  delay(500);
}

this second code delivers the millis correctly and three equal values for the printouts of the ports.

interestingly, both codes work with the arduino UNO however... ...and as mentioned above: if i change the code to read the same port twice, with a short delay, it doesn't work either.

by the way: i'm using Arduino 1.5.5.-r2.

thx in advance for your help! :slight_smile:

Sure you're not seeing this issue?
http://forum.arduino.cc/index.php?topic=203322.0

I'm not sure if r2 has the fix.

MarkT -- thanks a lot -- must've missed that thread.

exchanging the c-file did the job and everything works just fine now. :slight_smile:

...great work on the fix with the conversion times btw!