G'day & Namate to All.
I appreciate the chance to submit a query here on this forum.
Any advice on why the following code always output a zero for the second Serial.println() in each of the two if, else blocks?
I hope that the previously stored value of either A1 or A2 pins is retained at every loop() call as the respective variables are declared out of scope.
const byte inputs[] = {A1, A2};
float milliVolt_A1 = 0;
float milliVolt_A2 = 0;
void setup()
{
Serial.begin(115200);
while (!Serial);
}
void loop()
{
for (int pin = 0; pin < 2 ; pin++){
int dummy = analogRead(inputs[pin]); //read values twice as advise
int value = analogRead(inputs[pin]);
if (pin == 0){
float milliVolt_A1 = (value/1023.0)*5*1000;
Serial.print(milliVolt_A1);
Serial.print(",");
Serial.println(milliVolt_A2); //second println
}
else {
float milliVolt_A2 = (value/1023.0)*5*1000;
Serial.print(milliVolt_A2);
Serial.print(",");
Serial.println(milliVolt_A1); //second println
}
delay(1);
}
}