Pages: 1 [2] 3   Go Down
Author Topic: Soil Moisture sensor  (Read 5673 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OOPS!  smiley-red smiley-red smiley-red

did not check the code well enough ...
Logged

Rob Tillaart

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

Dallas, TX
Offline Offline
Sr. Member
****
Karma: 10
Posts: 318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OOPS!  smiley-red smiley-red smiley-red

did not check the code well enough ...

 smiley Not surprising, it goes on for a bit.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Dallas, TX
Offline Offline
Sr. Member
****
Karma: 10
Posts: 318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Only the define of sensorPin has changed ....

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

  }

}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz ....
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??
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink

ok i read it but i not sure to understand what i'm reading smiley-razz
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2] 3   Go Up
Jump to: