Go Down

Topic: Soil Moisture sensor (Read 6184 times) previous topic - next topic

robtillaart

OOPS!  :smiley-red: :smiley-red: :smiley-red:

did not check the code well enough ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Papa G


OOPS!  :smiley-red: :smiley-red: :smiley-red:

did not check the code well enough ...


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

Lentrave

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

Papa G


... 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.

Lentrave

Only the define of sensorPin has changed ....

Code: [Select]
#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;

  }

}

Lentrave

.... Another question ... Why when i wrote that:
Code: [Select]
Serial.print(100-(avg2*100/1010));

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

robtillaart

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

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Lentrave


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 :P ....
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??

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?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Lentrave


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 :P

Lentrave

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

robtillaart

Quote
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 ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Lentrave


Quote
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 ;)


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

robtillaart

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


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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Lentrave

...... 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 .....

Go Up