jumpy analog values [Solved]-thankyou

hi all,
quick questions, running a sensor on analog0, and a coin cell on analog1, and then on analog2. both sharing the same ground, when run by themselves i get correct values. when i run them in the same sketch i get jumpy and incorrect values? any ideas? serial monitor pics enclosed.

float battery;
float temperature;
float b;
float a;
void setup(){
  Serial.begin(115200);
}
void loop(){
  b=0;
  a=0;
  battery=0;
  temperature=0;

  battery=analogRead(0);
  b=battery/1023*5.0;
  Serial.println(b);
  delay(100);
  get();
}
void get(){
  temperature=analogRead(2);
  a=(5.0*temperature*100.0)/1023.0;
  Serial.println(a);
  delay(1000);
}

serial1.png

battery.png

temperature.png

Try two readings each time you do an analogRead, and chuck the first one away.

Try two readings each time you do an analogRead, and chuck the first one away.

Right... There is really only one analog-to-digital converter in the chip, and the analog inputs are multiplexed (switched). I haven't studied the timing-diagrams, but it takes some time to process & "settle".

Your using 0 and 2 in the combined code not 0 and 1!

Mark

holmes4:
Your using 0 and 2 in the combined code not 0 and 1!

Mark

yes i was trying it both ways right now the sensors are on 0, 2.

AWOL:
Try two readings each time you do an analogRead, and chuck the first one away.

ok thx im gnna try this now

this looks like its working the values are more consistent now. thank you guys.

results.png