#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define pins for LED bar graph
#define LED_BAR_A1 23
#define LED_BAR_A2 15
#define LED_BAR_A3 34
#define LED_BAR_A4 18
#define LED_BAR_A5 16
#define LED_BAR_A6 17
#define LED_BAR_A7 5
#define LED_BAR_A8 19
#define LED_BAR_A9 2
#define LED_BAR_A10 4
#define RELAY_PIN 0
// Constants for humidity and temperature (for simulation purposes)
const float humidityValue = 60.0; // Example humidity value (in percentage)
const float temperatureValue = 25.0; // Example temperature value (in Celsius)
// Initialize the LCD display with I2C address 0x27 (this can vary, check your LCD module)
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD address, columns, rows
// Function to calculate irrigation level based on humidity and temperature
int calculateIrrigationLevel(float humidity, float temperature) {
if (humidity > 70.0 && temperature > 28.0) {
return 10; // High irrigation level
} else if (humidity > 60.0 && temperature > 25.0) {
return 7; // Medium-high irrigation level
} else if (humidity > 50.0 && temperature > 22.0) {
return 5; // Medium irrigation level
} else if (humidity > 40.0 && temperature > 18.0) {
return 3; // Low-medium irrigation level
} else {
return 1; // Low irrigation level
}
}
void setup() {
Serial.begin(115200);
// Initialize LED bar graph pins as outputs
pinMode(LED_BAR_A1, OUTPUT);
pinMode(LED_BAR_A2, OUTPUT);
pinMode(LED_BAR_A3, OUTPUT);
pinMode(LED_BAR_A4, OUTPUT);
pinMode(LED_BAR_A5, OUTPUT);
pinMode(LED_BAR_A6, OUTPUT);
pinMode(LED_BAR_A7, OUTPUT);
pinMode(LED_BAR_A8, OUTPUT);
pinMode(LED_BAR_A9, OUTPUT);
pinMode(LED_BAR_A10, OUTPUT);
// Initialize relay pin as output
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Initialize relay state as off
// Initialize the LCD
lcd.init(); // Initializes the LCD
lcd.backlight(); // Turn on the backlight
lcd.clear(); // Clear the display
// Debugging: Ensure all LEDs are off initially
updateLEDBarGraph(0);
}
void loop() {
// Calculate irrigation level based on constant humidity and temperature values
int irrigationLevel = calculateIrrigationLevel(humidityValue, temperatureValue);
// Debugging: Print irrigation level to Serial Monitor
Serial.print("Irrigation Level: ");
Serial.println(irrigationLevel);
// Update LED bar graph based on irrigation level
updateLEDBarGraph(irrigationLevel);
// Display the values on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureValue);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidityValue);
lcd.print("%");
lcd.setCursor(0, 2);
lcd.print("Irrigation Level:");
lcd.setCursor(0, 3);
lcd.print(irrigationLevel);
lcd.print("/10");
delay(1000); // Delay for stability
}
// Function to update LED bar graph based on irrigation level
void updateLEDBarGraph(int level) {
// Array of LED pins
int ledPins[] = {LED_BAR_A1, LED_BAR_A2, LED_BAR_A3, LED_BAR_A4, LED_BAR_A5, LED_BAR_A6, LED_BAR_A7, LED_BAR_A8, LED_BAR_A9, LED_BAR_A10};
// Turn off all LED bar graph segments first
for (int i = 0; i < 10; i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on the appropriate number of LED bar graph segments based on irrigation level
for (int i = 0; i < level; i++) {
digitalWrite(ledPins[i], HIGH);
}
}