#include <Wire.h>
#include <LiquidCrystal.h>
#include <ESP32Servo.h>
class Device {
public:
virtual void initialize() = 0;
virtual void clear() = 0;
virtual void print(const String &text) = 0;
};
class LCD : public Device {
private:
LiquidCrystal lcd;
public:
LCD(int rs, int enable, int d4, int d5, int d6, int d7) : lcd(rs, enable, d4, d5, d6, d7) {}
void initialize() override {
lcd.begin(16, 2); // Adjust the dimensions based on your LCD
}
void clear() override {
lcd.clear();
}
void print(const String &text) override {
lcd.print(text);
}
};
class ServoDevice : public Device {
private:
Servo servo;
int initialAngle;
public:
ServoDevice(int pin, int initial) : initialAngle(initial) {
servo.attach(pin);
}
void initialize() override {
servo.write(initialAngle); // Initialize servo to its initial position
}
void clear() override {
// No clearing needed for the servo
}
void print(const String &text) override {}
void moveTo(int angle) {
servo.write(angle);
}
};
LCD lcdDevice( 19, 23, 18, 17, 16, 15); // Modify these pin numbers based on your wiring
ServoDevice servoDevice(5, 0);
ServoDevice servoDevice2(32, 0); // Initialize the servo at 0 degrees
const int soilPin1 = A0; // Soil sensor 1 pin
const int soilPin2 = 35; // Soil sensor 2 pin
void setup() {
Wire.begin(23,22);
Serial.begin(9600);
lcdDevice.initialize();
servoDevice.initialize();
}
void loop() {
int16_t i1 = analogRead(soilPin1);
int16_t i2 = analogRead(soilPin2);
String msg1 = (i1 < 2165) ? "WET" : (i1 > 3135) ? "DRY" : "OK";
String msg2 = (i2 < 2165) ? "WET" : (i2 > 3135) ? "DRY" : "OK";
lcdDevice.clear();
lcdDevice.print("Soil1: "+ msg1);
lcdDevice.print("Soil2:"+ msg2);
delay(500);
if (i1 > 3135) {
servoDevice.moveTo(90); // Move the servo to 90 degrees
} else {
servoDevice.moveTo(0); // Return the servo to its initial position (0 degrees)
}
if (i2 > 3135) {
servoDevice.moveTo(90); // Move the servo to 90 degrees
} else {
servoDevice.moveTo(0); // Return the servo to its initial position (0 degrees)
}
}