Soil Moisture sensor

depends if the
pinMode(sensorPin3, INPUT);
is done before or after the serial.begin()

Note that the OP nowhere uses the Serial in its sketch so his code is OK in that sense.

robtillaart:
depends if the
pinMode(sensorPin3, INPUT);
is done before or after the serial.begin()

Note that the OP nowhere uses the Serial in its sketch so his code is OK in that sense.

So you're saying that if this case

 case 5:
    Serial.print("Valeur sensor #1: ");
    Serial.println(avg1);
    Serial.println();
    Serial.print("Valeur sensor #2: ");
    Serial.println(avg2);
    Serial.println();
    Serial.print("Valeur sensor #3: ");
    Serial.println(avg3);
    Serial.println();
    Serial.print("Valeur sensor #4: ");
    Serial.println(avg4);
    Serial.println("-----------------------");
     Serial.print("Valeur moyenne de tout les sensors: ");
    Serial.println((avg1+avg2+avg3+avg4)/4);
    Serial.println();
    Serial.println();
    currentsensor=1;
    break;

is never executed, it's all good? Seems kind of iffy to me.

OOPS! :blush: :blush: :blush:

did not check the code well enough ...

robtillaart:
OOPS! :blush: :blush: :blush:

did not check the code well enough ...

:slight_smile: Not surprising, it goes on for a bit.

... and the sensorPin suppose to be A0, A1 like robtillaart said ... Hardware serial is digital pin not analog no? So no conflict !

Lentrave:
... and the sensorPin suppose to be A0, A1 like robtillaart said ... Hardware serial is digital pin not analog no? So no conflict !

Correct. It's hard to tell what your current code is.

Only the define of sensorPin has changed ....

#define voltageFlipPin11 2
#define voltageFlipPin12 3
#define voltageFlipPin21 4
#define voltageFlipPin22 6
#define voltageFlipPin31 7
#define voltageFlipPin32 8
#define voltageFlipPin41 9
#define voltageFlipPin42 11
#define sensorPin1 A4
#define sensorPin2 A2
#define sensorPin3 A1
#define sensorPin4 A0

int flipTimer = 1000;
int currentsensor=1;
int val11;
int val12;
int avg1;
int val21;
int val22;
int avg2;
int val31;
int val32;
int avg3;
int val41;
int val42;
int avg4;

void setup(){
  Serial.begin(9600);
  pinMode(voltageFlipPin11, OUTPUT);
  pinMode(voltageFlipPin12, OUTPUT);
  pinMode(voltageFlipPin21, OUTPUT);
  pinMode(voltageFlipPin22, OUTPUT);
  pinMode(voltageFlipPin31, OUTPUT);
  pinMode(voltageFlipPin32, OUTPUT);
  pinMode(voltageFlipPin41, OUTPUT);
  pinMode(voltageFlipPin42, OUTPUT);
  digitalWrite(voltageFlipPin11, LOW);
  digitalWrite(voltageFlipPin12, LOW);
  digitalWrite(voltageFlipPin21, LOW);
  digitalWrite(voltageFlipPin22, LOW);
  digitalWrite(voltageFlipPin31, LOW);
  digitalWrite(voltageFlipPin32, LOW);
  digitalWrite(voltageFlipPin41, LOW);
  digitalWrite(voltageFlipPin42, LOW);
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPin3, INPUT);
  pinMode(sensorPin4, INPUT);
}

void loop(){
  switch (currentsensor){

  case 1:
    digitalWrite(voltageFlipPin11, HIGH);
    digitalWrite(voltageFlipPin12, LOW);
    delay(flipTimer);
    val11 = analogRead(sensorPin1);
    delay(flipTimer);
    digitalWrite(voltageFlipPin11, LOW);
    digitalWrite(voltageFlipPin12, HIGH);
    delay(flipTimer);
    val12 = 1023 - analogRead(sensorPin1);
    avg1 = (val11 + val12) / 2;
    digitalWrite(voltageFlipPin11, LOW);
    digitalWrite(voltageFlipPin12, LOW);
    ++currentsensor;
    break;

  case 2:
    digitalWrite(voltageFlipPin21, HIGH);
    digitalWrite(voltageFlipPin22, LOW);
    delay(flipTimer);
    val21 = analogRead(sensorPin2);
    delay(flipTimer);
    digitalWrite(voltageFlipPin21, LOW);
    digitalWrite(voltageFlipPin22, HIGH);
    delay(flipTimer);
    val22 = 1023 - analogRead(sensorPin2);
    avg2 = (val21 + val22) / 2;
    digitalWrite(voltageFlipPin21, LOW);
    digitalWrite(voltageFlipPin22, LOW);
    ++currentsensor;
    break;

  case 3:
    digitalWrite(voltageFlipPin31, HIGH);
    digitalWrite(voltageFlipPin32, LOW);
    delay(flipTimer);
    val31 = analogRead(sensorPin3);
    delay(flipTimer);
    digitalWrite(voltageFlipPin31, LOW);
    digitalWrite(voltageFlipPin32, HIGH);
    delay(flipTimer);
    val32 = 1023 - analogRead(sensorPin3);
    avg3 = (val31 + val32) / 2;
    digitalWrite(voltageFlipPin31, LOW);
    digitalWrite(voltageFlipPin32, LOW);
    ++currentsensor;
    break;

  case 4:
    digitalWrite(voltageFlipPin41, HIGH);
    digitalWrite(voltageFlipPin42, LOW);
    delay(flipTimer);
    val41 = analogRead(sensorPin4);
    delay(flipTimer);
    digitalWrite(voltageFlipPin41, LOW);
    digitalWrite(voltageFlipPin42, HIGH);
    delay(flipTimer);
    val42 = 1023 - analogRead(sensorPin4);
    avg4 = (val41 + val42) / 2;
    digitalWrite(voltageFlipPin41, LOW);
    digitalWrite(voltageFlipPin42, LOW);
    ++currentsensor;
    break;

  case 5:
    Serial.print("Sensor Value #1: ");
    Serial.println(avg1);
    Serial.println();
    Serial.print("Sensor Value #2: ");
    Serial.println(avg2);
    Serial.println();
    Serial.print("Sensor Value #3: ");
    Serial.println(avg3);
    Serial.println();
    Serial.print("Sensor Value #4: ");
    Serial.println(avg4);
    Serial.println("-----------------------");
     Serial.print("Average Sensor Value: ");
    Serial.println((avg1+avg2+avg3+avg4)/4);
    Serial.println();
    Serial.println();
    currentsensor=1;
    break;

  }

}

.... Another question ... Why when i wrote that:

Serial.print(100-(avg2*100/1010));

If avg2=1000, I receive 130 ... ???

there is an int overflow, make all constants long by adding the L modifier to them

Serial.print(100L - (avg2*100L/1010L) );

robtillaart:
there is an int overflow, make all constants long by adding the L modifier to them

Serial.print(100L - (avg2*100L/1010L) );

Tanks, that work but now to want to know why :stuck_out_tongue: ....
The biggest number i have in this formula is 100000 (avg2*100) ... That's why i have this problem due to integer limitation of 32767??

Yes, (even unisgned int would be too small)

looking at the formula you can work around it as follows too:

Serial.print(100 - (avg2*10/101) ); // removed a common factor 10

avg2 *10 is max 10000, divided by 101 is 99 or so all within int range

Q: why divide by 101? what is the range you expect in the print? 1..100 or 0..100?

robtillaart:
Yes, (even unisgned int would be too small)

looking at the formula you can work around it as follows too:

Serial.print(100 - (avg2*10/101) ); // removed a common factor 10

avg2 *10 is max 10000, divided by 101 is 99 or so all within int range

Q: why divide by 101? what is the range you expect in the print? 1..100 or 0..100?

1010 it's the Max of what i can read with the sensor... I want to show in % the soil humidity but Another problem is i dont think the soil resistance increase is linear so maybe i do this for nothing :stuck_out_tongue:

I think i will have to do test by soil weight to know the humidity level and the reading according to that!

I want to show in % the soil humidity but Another problem is i dont think the soil resistance increase is linear so maybe i do this for nothing

Check my multiMap() on the playground, it is invented for non linear thingies. See - http://playground.arduino.cc/Main/MultiMap -

(time for a cup of tea :wink:

robtillaart:

I want to show in % the soil humidity but Another problem is i dont think the soil resistance increase is linear so maybe i do this for nothing

Check my multiMap() on the playground, it is invented for non linear thingies. See - http://playground.arduino.cc/Main/MultiMap -

(time for a cup of tea :wink:

ok i read it but i not sure to understand what i'm reading :stuck_out_tongue:

ok i read it but i not sure to understand what i'm reading

multimap() interpolates between points in a lookup table. Better?

...... maybe... so i make my test with weight/read tu deternine the % of water in the soil and then i enter the value in multimap() (ex: 5%,10%,15% etc...) and the corespondant reading (ex:1000,950,900 etc...) and .....

yes, but only the digits without the % sign :slight_smile:

Feel free to have a look at my blog postings regarding soil moisture measurement. Includes guidelines how to build and calibrate a gypsum block soil moisture sensor.

link: http://vanderleevineyard.com/1/category/vinduino/1.html