So im working on a soil moisture detector warning system. i programmed it but the problem is that only one LED turns on and i dont know what to do to make the others light up when a value is reached.
i would appreciate if someone has an idea of what i can do to fix the code.
const int VAL_PROBE = 0; // Analog pin 0
const int MOISTURE_LEVEL = 0; // the value after the LED goes ON
void setup() {
Serial.begin(9600);
}
void Dry(int state) {
digitalWrite(11, state);
}
void Humid(int state) {
digitalWrite(13, state);
}
void Water(int state) {
digitalWrite(12, state);
}
void loop() {
int moisture = analogRead(VAL_PROBE);
Serial.println(moisture);
if(moisture < 180) {
Dry(HIGH);
} else {
Dry(LOW);
}
Serial.println(moisture);
if(moisture > 60) {
Humid(HIGH);
} else {
Humid(LOW);
}
Serial.println(moisture);
if(moisture < 59) {
Water(HIGH);
} else {
Water(LOW);
}
delay(1000);
}
One thing that might help you easily determine the issue would be adding a few more serial.println inside each outcome in your if blocks, for instance "serial.println("Dry High)", "serial.println("Dry High)", etc. That way you will quickly determine if your logic is working as expected.
The way I read the code is that it should function like this..
moisture < 180 dry LED on
moisture > 180 dry LED off
moisture > 60 Humid LED on
moisture < 60 Humid LED off
moisture < 59 water LED on
moisture > 59 water LED off
Thanks for the help but could you please give a example code. i'm quite new to the arduino programming language so im not sure off how to make it work.
Thanks for the code
but there is a problem non of the LED turn on unless i stick it on water so only one "water" activates. also i wondered if there was a way to activate it when between to points like 100 -180 or 0 - 55?
here is a screenshot of what its doing.
Also sorry, little mistake in code above, please try this (figured it out from the screenshot), it was going to say WATER ON all the time.
const int VAL_PROBE = 0; // Analog pin 0
const int MOISTURE_LEVEL = 0; // the value after the LED goes ON
void setup() {
Serial.begin(9600);
}
void Dry(int state) {
digitalWrite(11, state);
}
Also, at which point is it in the water, the high numbers (like 1022) or the lower numbers?
void Humid(int state) {
digitalWrite(13, state);
}
void Water(int state) {
digitalWrite(12, state);
}
void loop() {
int moisture = analogRead(VAL_PROBE);
Serial.println(moisture);
if(moisture <= 180) {
Dry(HIGH);
Serial.println("DRY ON");
} else {
Dry(LOW);
Serial.println("DRY OFF");
}
Serial.println(moisture);
if(moisture >= 60) {
Humid(HIGH);
Serial.println("HUMID ON");
} else {
Humid(LOW);
Serial.println("HUMID OFF");
}
Serial.println(moisture);
if(moisture <= 59) {
Water(HIGH);
Serial.println("WATER ON");
} else {
Water(LOW);
Serial.println("WATER OFF");
}
delay(1000);
}
Also is it in the water when the numbers are high (like 1022) or the lower numbers?
I am guessing that it is the higher numbers, and in that case I am going to also guess your LEDs are connected something like this
Arduino -> Resistor -> LED -> +5V
If that is the case then the LED will only turn on when the Arduino provides a ground (LOW). To correct this you can either switch the HIGH and LOW in the if statements or turn you LEDs around and hook up like this
Thanks again for the code
so i tried the new code and it worked alot better then the old did.
im sorry not for clarifying what values there were in the screenshot that i added a new one. The lower the value the more moisture there is i think it measures the resistance with a electric field around the two rods of the earth moisture meter. The Picture
The only problem i'm seeing now is that both the Humid and the Dry indicator goes of at the same time so if there was a way to only make the Water/Humid and Dry marks be active between for example:
water = 0 - 70
Humid = 70 - 170
Dry = 170 - 1023
Thank you so much the detector is working flawlessly
Now i just have to figure out why my LED´s are not as bright as earlier.
Thanks a lot for the help.