#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int ledPin = 2; // LED pin
void setup()
{
Wire.begin(23, 22); // Initialize the I2C communication with SDA pin 23 and SCL pin 22.
Serial.begin(9600); /// Initialize the serial communication at a baud rate of 9600 bits per second.
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on the backlight
pinMode(ledPin, OUTPUT);
}
void loop()
{
int16_t i = analogRead(34);
// Use the ternary operator to determine the moisture status based on the value of 'i'.
// If 'i' is less than 300, set 'msg' to "DRY"; if 'i' is greater than 700, set 'msg' to "WET"; otherwise, set 'msg' to "OK".
String msg = i < 300 ? "DRY" : i > 700 ? "WET" : "OK";
lcd.clear();
lcd.print("Soil: ");
lcd.print(msg);
if(i<=300){
digitalWrite(ledPin, HIGH); // will turn on the LED(motor) if the soil is dry (i<300)
}
else {
digitalWrite(ledPin, LOW);
}
lcd.setCursor(0, 1); // Set cursor to the first column and second row
lcd.print("SensorValue:");
lcd.print(i);
}