Serial print of second value is always 0

Hi everybody

I'm new to this board and testing some things out with arduino.

I tried a code that prints me out the value of a moisture sensor, that works fine.

Now I wanted to add a second sensor to this installation on a different pin and used the code below, but now every time I serial print the values I get a zero on the second value.

- Plant1 = 518 Plant2 = 0

1st question: What is my mistake in this consideration? 2nd question: Is there an easier way to accomplish such multiple sensor codes if I d'like to add some more sensors to it?

int val = 0;
int val2 = 0;
int soilPin = A0;
int soilPin2 = A2;
int soilPower = 7;
int soilPower2 = 6;

void setup() 
{
  Serial.begin(9600);  

  pinMode(soilPower, OUTPUT);
  digitalWrite(soilPower,LOW);
  pinMode(soilPower2, OUTPUT);
  digitalWrite(soilPower2,LOW);
}

void loop() 
{
Serial.print("Plant1 = ");    
Serial.println(readSoil());
Serial.print("Plant2 = ");    
Serial.println(readSoil2());

delay(1000);
}
int readSoil()
{

    digitalWrite(soilPower, HIGH);
    delay(100);
    val = analogRead(soilPin);
    digitalWrite(soilPower, LOW);
    return val;
}
int readSoil2()
{
    digitalWrite(soilPower2, HIGH);
    delay(100);
    val2 = analogRead(soilPin2);
    digitalWrite(soilPower2, LOW);
    return val2;
}
int val = 0;
int val2 = 0;
int soilPin = A0;
int soilPin2 = A2;
int soilPower = 7;
int soilPower2 = 6;

Do you count this way? Uh-huh, 2, 3...? I doubt it. When you number one variable in a set, number them ALL. Or, better, use arrays.

Why do you need two functions that do exactly the same thing to different pins? You don't seem to need to use analogRead0() and analogRead2() to read from two different analog pins, so take a clue, and write ONE function that takes arguments.

Why does those functions return the value in global variables?

They should return values, but the variables should be local to the function(s). val(1) and val2 should NOT be global.

If you have two identical sensors connected to analog pins 0 and 2, and one always reads 0, then it is defective or it is not connected to the pin you are reading from.

PaulS has some good points. You could use arrays and function parameters. However, the code looks like it should work.

Check your wiring. See if you have really connected the second sensor to pins A2 and 6.

Maybe a sensor is bad. Swap sensors and see if the results change.