Sprinkler system using soil moisture sensor

Hi guys, I'm trying to create a sprinkler system with 2 soil moisture sensors instead of one. I want to control the pump, on and off, according to if average moisture level that the sensors detected is less than 25%. I tried to do this in my code but the pump always turns on even if the moisture level is at 100%. Can someone please tell me the problem in my code. Thank You!

Here is my code:

<
#include <LCD5110_Graph.h>
#include <avr/pgmspace.h>

LCD5110 lcd(8,9,10,12,11);

extern unsigned char BigNumbers[];

int sensorPin = A0;
int sensorValue = 0;
int percent = 0;
String percentString ="0";
int stringLength = 0;
int sensor;

int OtherSensorPin = A1;
int OtherSensorValue = 0;
int OtherPercent = 0;
String OtherPercentString = "0";
int OtherStringLength = 0;

int sprinkler = 13;
int waterTime = 100;

int Value;
int Value2;
int data;
int data2;

void setup() {
Serial.begin(9600);
while(!Serial);
lcd.InitLCD();
lcd.setFont(BigNumbers);
pinMode(sprinkler, OUTPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);

delay(1000);
}

void loop() {
lcd.clrScr();
lcd.drawBitmap(0, 0, ui, 84, 48);
sensorValue = analogRead(sensorPin);
percent = convertToPercent(sensorValue);
percentString = String(percent);
stringLength = percentString.length();
displayPercent(stringLength);

lcd.update();
delay(1000);
}

int convertToPercent(int value)
{
int percentValue = 0;
percentValue = map(value, 1023, 350, 0, 100);
if(percentValue>100)
percentValue = 100;
return percentValue;

int OtherPercentValue = 0;
OtherPercentValue = map(OtherSensorValue, 350, 1023, 0, 100);
if(OtherPercentValue>100)
OtherPercentValue = 100;
return OtherPercentValue;

Value = analogRead(A0);
Serial.println(Value);

data = map(Value, 0, 1023, 0, 100);

Value2 = analogRead(A1);

data2 = map(Value2, 0, 1023, 0, 100);

int c;
int d;
(c = data + data2);
(d = c/2);
Serial.println(d);

if (d <= 25)
{
digitalWrite(sprinkler, HIGH);
}
else
{
digitalWrite(sprinkler, LOW);
}

Serial.println(data);

delay(500);
}

void displayPercent(int length)
{
switch(length)
{
case 1: lcd.print(percentString,38,19); break;
case 2: lcd.print(percentString,24,19); break;
case 3: lcd.print(percentString,10,19); break;
default: lcd.print(percentString,0,19); break;
}
}

IRP18-19code.ino (5.12 KB)

  1. Use code tags

  2. You seem to have 2 analog pins but only one analogRead()

  3. EVERYTHING after a return statement in a function is NEVER executed.

Thanks, I'm pretty new to this. Can you explain to me what number 3 means? Does this have an effect on the pump running no matter what.

AyyBruhChill:
Thanks, I'm pretty new to this. Can you explain to me what number 3 means? Does this have an effect on the pump running no matter what.

He means this:

.
.
.
int convertToPercent(int value)
{
    int percentValue = 0;
    percentValue = map(value, 1023, 350, 0, 100);
    if(percentValue>100)
        percentValue = 100;

    return percentValue;  <-- always leaves; everything below this point in this function never executes

    ^^^ and if you fix this...

    int OtherPercentValue = 0;
    OtherPercentValue = map(OtherSensorValue, 350, 1023, 0, 100);
    if(OtherPercentValue>100)
        OtherPercentValue = 100;
        
    return OtherPercentValue;  <-- it will happen again here

    Value = analogRead(A0);
.
.
.

Oh thanks, so I should just rid of those right?

AyyBruhChill:
Oh thanks, so I should just rid of those right?

Well, no, not necessarily. Think about what you're trying to do. It may just be that you need to correct some logical error; maybe you need to put the return xxx within braces associated with an if.

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a link to data/specs of your sensors please?

Thanks.. Tom.. :slight_smile:

Blackfin:
maybe you need to put the return xxx within braces associated with an if.

But if you need both percentValue and otherPercentValue to be "returned" in the same pass through the function, you can't, since a function can only return one value when it's called. You would need those to be variables of global scope to allow them to be visible outside the called function.

As a matter of interest, why are you checking those values to make sure they're not over 100? The map is making sure they're not, anyway.

Blackfin:
Well, no, not necessarily. Think about what you're trying to do. It may just be that you need to correct some logical error; maybe you need to put the return xxx within braces associated with an if.

I always try to avoid return statements halfway a function.
Much rather use an else if statement. Much less confusion as you can be sure that you always reach the very end of the function. Often enough that you want to add some more code, and then find out that in certain situations it's not executed as expected. Can be quite some hair pulling to find that the return several dozens of lines up is the culprit!

If I understand

control_point = (sensor1 + sensor2) /2
if(control_point =< 25) turn on sprinklers else turn them off

the way I see this, adding values of ( sensor 1 = 100 and sensor 2 = 0) / 2
then your reading will be 50

since the math would allow one to be completely dry if the other were fully wet, there is a fundamental problem.
actually ( 52 + 0)/2 = 26
so one could be bone dry, the other just over wet......

should it not be :
if sensor 1 or sensor 2 were less than 25% turn on the valve ?