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)