Hello. I have a project that works with 4 moisture sensors turning on 4 motors. The problem I have is that using the code below, I do not know how to get the lcd to show the results from each sensor. It can be one then another after a 4 second delay or any other applicable design. Any help would be appreciated.
The first Sensor works perfectly displaying as wanted. Additional sensors is my problem.
const int sensor0 = 0; // Analog pin 0
const int sensor1 = 1; // Analog pin 1
const int sensor2 = 2; // Analog pin 2
const int sensor3 = 3; // Analog pin 3
const int MOISTURE_LEVEL = 600; // Analog pin 0 value after LED goes on
const int MOISTURE_LEVEL1 = 600; //Analog pin 1 value
const int MOISTURE_LEVEL2 = 600; //Analog pin 2 value
const int MOISTURE_LEVEL3 = 600; //Analog pin 3 value
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup(){
lcd.begin(16,2); //Set LCD Display
Serial.begin(9600); // Set baud rate
}
void Relay(int normallyopen){ //
pinMode(13, OUTPUT);
digitalWrite(13, normallyopen); // Assign pin 13 to Relay 1
}
void Relay1(int normallyopen1){
pinMode(10, OUTPUT);
digitalWrite(10,normallyopen1);
}
void Relay2(int normallyopen2){
pinMode(9, OUTPUT);
digitalWrite(9,normallyopen2);
}
void Relay3(int normallyopen3){
pinMode(8, OUTPUT);
digitalWrite(8,normallyopen3);
}
void loop(){
int moisture = analogRead(sensor0); // Sets moisture to equal the value of the analog pin 0
int moisture1 = analogRead(sensor1);
int moisture2 = analogRead(sensor2);
int moisture3 = analogRead(sensor3);
Serial.println(moisture);
Serial.println(moisture1);
Serial.println(moisture2);
Serial.println(moisture3);
if(moisture > MOISTURE_LEVEL){
Relay(LOW);
} else {
Relay(HIGH);
}
delay(100);
if(moisture1 > MOISTURE_LEVEL1){
Relay1(LOW);
} else {
Relay1(HIGH);
}
delay(100);
if(moisture2 > MOISTURE_LEVEL2){
Relay2(LOW);
} else {
Relay2(HIGH);
}
delay(100);
if(moisture3 > MOISTURE_LEVEL3){
Relay3(LOW);
} else {
Relay3(HIGH);
}
delay(100);
// read the input on analog pin 0:
int sensorValue0 = analogRead(A0);
int sensorValue1 = analogRead(A1);
int sensorValue2 = analogRead(A2);
int sensorValue3 = analogRead(A3);
Serial.println(sensorValue0);
if(sensorValue0<400) {
lcd.print("Oversaturated ");
lcd.setCursor(0,1);
lcd.print("Zone 1");
delay(1800);
lcd.clear();
}
if((sensorValue0>400)&&(sensorValue0<600)) {
lcd.print("Ideal Moisture");
lcd.setCursor(0,1);
lcd.print("Zone 1");
delay(1800);
lcd.clear();
}
if((sensorValue0>600)&&(sensorValue0<1023)){
lcd.print("Dehydrated Soil");
lcd.setCursor(0,1);
lcd.print("Zone 1");
delay(1800);
lcd.clear();
}
}
Please edit your post to put the code inside code tags. It will make it easier to analyze.
Im sorry Im new and do not know what that means.
I think i figured it out
Tried a few more thing with no good outcomes. Any help would be appreciated.
eugenenelson:
Tried a few more thing with no good outcomes. Any help would be appreciated.
Post your serial output from the computer as well as what is happening on the LCD when you run the code.
So I got the LCD working. I had to figure it all out by trial and error cause this thread got buried fast.
So it works except for two things now. As you can see in my code i have 4 moisture sensors turning on 4 relays. Those work fine. The problem is that the sensors all read around 750 instead of 1023 when unplugged or plugged in and not in any soil or moisture. The analog inputs with infinite resistance are supposed to show 1023. They do not. The other problem is one sensor reads 100 lower then the other sensors at all times. I have swapped out the sensors with each other and it made no difference, sensor1 in the code is always 100 less. No matter which sensor is connected to sensor1. I even used another Arduino and it does the same exact thing. The kicker is when I remove the entire section near the bottom that prints sensor1 to the lcd, I have no discrepancy between the sensors anyomore. They all read within 5 of each other. As soon as I put sensor1 code back in for printing to the lcd it goes back to being 100 lower.
I will put the code here. Please help as I am getting very frustrated with coding at this point and would like to finally get this project done. Any help would be greatly appreciated.
const int sensor0 = 0; // Analog pin 0
const int sensor1 = 1; // Analog pin 1
const int sensor2 = 2; // Analog pin 2
const int sensor3 = 3; // Analog pin 3
const int MOISTURE_LEVEL = 600; // Analog pin 0 value to turn relay off
const int MOISTURE_LEVEL1 = 600; //Analog pin 1 value to turn relay off
const int MOISTURE_LEVEL2 = 600; //Analog pin 2 value to turn relay off
const int MOISTURE_LEVEL3 = 600; //Analog pin 3 value to turn relay off
#include <LiquidCrystal.h> //identify lcd
LiquidCrystal lcd(12,11,5,4,3,2); //set the lcd pins
void setup(){
lcd.begin(16,2); //Set LCD Display
Serial.begin(9600); // Set baud rate
}
void Relay(int normallyopen){ //make relay set to normallyopen 1
pinMode(13, OUTPUT); //set pin 13 as an output
digitalWrite(13, normallyopen); //
}
void Relay1(int normallyopen1){ //make relay1 set to normallyopen 1
pinMode(10, OUTPUT); //set pin 10 as an output
digitalWrite(10,normallyopen1);
}
void Relay2(int normallyopen2){ //make relay2 set to normallyopen2
pinMode(9, OUTPUT); //set pin 9 as an output
digitalWrite(9,normallyopen2);
}
void Relay3(int normallyopen3){ //make relay3 set to normallyopen3
pinMode(8, OUTPUT); //set pin 8 as an output
digitalWrite(8,normallyopen3);
}
void loop(){
int moisture = analogRead(sensor0); // Sets moisture to equal the value of the analog pin 0
int moisture1 = analogRead(sensor1); // Sets moisture to equal the value of the analog pin 1
int moisture2 = analogRead(sensor2); // Sets moisture to equal the value of the analog pin 2
int moisture3 = analogRead(sensor3); // Sets moisture to equal the value of the analog pin 3
Serial.println(moisture); //print to serial monitor
Serial.println(moisture1); //print to serial monitor
Serial.println(moisture2); //print to serial monitor
Serial.println(moisture3); //print to serial monitor
if(moisture > MOISTURE_LEVEL){ //if moisture level is greater then constant stated above relay is high, is not relay is low
Relay(LOW);
} else {
Relay(HIGH);
}
delay(100);
if(moisture1 > MOISTURE_LEVEL1){ //if moisture level is greater then constant stated above relay is high, is not relay is low
Relay1(LOW);
} else {
Relay1(HIGH);
}
delay(100);
if(moisture2 > MOISTURE_LEVEL2){ //if moisture level is greater then constant stated above relay is high, is not relay is low
Relay2(LOW);
} else {
Relay2(HIGH);
}
delay(100);
if(moisture3 > MOISTURE_LEVEL3){ //if moisture level is greater then constant stated above relay is high, is not relay is low
Relay3(LOW);
} else {
Relay3(HIGH);
}
delay(100);
int sensorValue0 = analogRead(A0); // read the input on analog pin to sensorvalue0
int sensorValue1 = analogRead(A1); // read the input on analog pin to sensorvalue1
int sensorValue2 = analogRead(A2); // read the input on analog pin to sensorvalue2
int sensorValue3 = analogRead(A3); // read the input on analog pin to sensorvalue3
Serial.println(sensorValue0); //send sensorvalue0 value command for lcd for printout
if(sensorValue0<400) {
lcd.print("Oversaturated ");
lcd.setCursor(0,1);
lcd.print("Zone 1");
delay(4000);
lcd.clear();
}
if((sensorValue0>400)&&(sensorValue0<600)) {
lcd.print("Ideal Moisture");
lcd.setCursor(0,1);
lcd.print("Zone 1");
delay(4000);
lcd.clear();
}
if((sensorValue0>600)&&(sensorValue0<1023)){
lcd.print("Dehydrated Soil");
lcd.setCursor(0,1);
lcd.print("Zone 1");
delay(4000);
lcd.clear();
}
}
This is the serial monitor
As you can see, every second set of 4 it drops significantly.
1023
1023
1023
1023
664
462
704
718
682
683
682
682
683
362
682
682
683
682
683
682
682
366
682
682
682
682
682
682
682
368
682
682
683
682
683
683
683
367
682
683
683
683
683
682
684
368
683
682
681
683
682
683
682
368
682
682
682
682
682
682
682
368
683
682
682
682
683
682
683
368
682
682
683
682
682
682
682
368
682
682
684
682
683
682
683
368
681
682
682
682
682
682
683
368
683
682
683
682
682
682
682
368
681
681
682
682
682
682
682
368
681
681
683
682
683
682
683
368
684
682
683
682
683
682
683
368
683
682
682
683
683
684