So this is the code:
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <Encoder.h>
hd44780_I2Cexp lcd;
const int tempPin = A0; // Temperature sensor pin
Encoder thresholdEncoder(2, 3); // Rotary encoder pins A and B for threshold value
Encoder durationEncoder(4, 5); // Rotary encoder pins A and B for watering duration
Encoder intervalEncoder(6, 7); // Rotary encoder pins A and B for watering interval
int thresholdValue = 50; // Default threshold value
int wateringDuration = 5; // Default watering duration in seconds
int wateringInterval = 60; // Default watering interval in minutes
void setup() {
Serial.begin(9600); // Start serial communication
lcd.begin(20, 4); // Initialize the LCD with 20 columns and 4 rows
lcd.setBacklight(HIGH); // Turn on the backlight
}
void loop() {
// Read temperature value
int tempValue = readTemperature();
// Display temperature value
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tempValue);
lcd.print("C");
// Read encoder values
int thresholdEncoderValue = thresholdEncoder.read();
int durationEncoderValue = durationEncoder.read();
int intervalEncoderValue = intervalEncoder.read();
// Update threshold value
thresholdValue += thresholdEncoderValue;
// Display threshold value
lcd.setCursor(0, 1);
lcd.print("Threshold: ");
lcd.print(thresholdValue);
// Update watering duration
if (durationEncoderValue > 0) {
wateringDuration++;
} else if (durationEncoderValue < 0 && wateringDuration > 0) {
wateringDuration--;
}
// Display watering duration
lcd.setCursor(0, 2);
lcd.print("Duration: ");
lcd.print(wateringDuration);
lcd.print("s");
// Update watering interval
if (intervalEncoderValue > 0) {
wateringInterval++; // Clockwise rotation
} else if (intervalEncoderValue < 0 && wateringInterval > 0) {
wateringInterval--; // Counterclockwise rotation
}
// Display watering interval
lcd.setCursor(0, 3);
lcd.print("Interval: ");
lcd.print(wateringInterval);
lcd.print("min");
// Reset encoder values
thresholdEncoder.write(0);
durationEncoder.write(0);
intervalEncoder.write(0);
}
int readTemperature() {
// Simulated temperature reading
return 25;
}
The variables should increase and decrease depending on the rotation of the encoder. The problem is duration and interval changes inaccurately and adds random value and increases/decreases randomly whether rotated left or right. The wiring is ok, so I think it's the code