This is the code for getting percentage which works properly with percentage as output.
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int sensorPin = A0;
int sensorValue = 0;
int percentValue = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.print("\n\nAnalog Value: ");
Serial.print(sensorValue);
percentValue = map(sensorValue, 1023, 200, 0, 100);
Serial.print("\nPercentValue: ");
Serial.print(percentValue);
Serial.print("%");
lcd.setCursor(0, 0);
lcd.print("Soil Moisture");
lcd.setCursor(0, 1);
lcd.print("Percent: ");
lcd.print(percentValue);
lcd.print("%");
delay(1000);
lcd.clear();
}
I wanted to display respective conductivity values for various ranges of the moisture percentage for which I altered the code using multiple if conditions by assigning values for moisture percentage and displaying that in lcd. But the lcd displays the output of the last if statement even though the moisture percentage is in the range of 0-5. Why is the value not getting updated as I change the sensor's moisture level. In the serial monitor, the percentage is still changing every second as I dip the sensor inside water but this code with the multiple if is not working even though it has no errors there is some logical error.
Here is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int sensorPin = A0;
int sensorValue = 0;
int percentValue = 0;
float conductivity=0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.print("\n\nAnalog Value: ");
Serial.print(sensorValue);
percentValue = map(sensorValue, 1023, 200, 0, 100);
Serial.print("\nPercentValue: ");
Serial.print(percentValue);
Serial.print("%");
lcd.setCursor(0, 0);
lcd.print("Electrical Conductivity: ");
if (0<=percentValue<5)
conductivity=0.00;
if (5<=percentValue<=10)
conductivity=0.25;
if (11<=percentValue<=20)
conductivity=0.25;
if (21<=percentValue<=30)
conductivity=0.75;
if (31<=percentValue<=40)
conductivity=1.13;
if (41<=percentValue<=50)
conductivity=1.53;
if (51<=percentValue<=60)
conductivity=1.93;
if (61<=percentValue<=70)
conductivity=2.33;
if (71<=percentValue<=80)
conductivity=2.72;
if (81<=percentValue<=90)
conductivity=3.12;
if (91<=percentValue<=100)
conductivity=3.65;
lcd.setCursor(0, 1);
lcd.print(conductivity);
lcd.print(" S/m");
delay(1000);
lcd.clear();
}