#include <Wire.h> #include "DHT.h" #include <LiquidCrystal_I2C.h> //Define DHT pin and type #define DHTPIN 9 #define DHTTYPE DHT11 //Initialize the sensor and LCD LiquidCrystal_I2C lcd(0x27, 16, 2); DHT dht(DHTPIN, DHTTYPE); const int pumpPin = 8; const int soilMoisturePin = A0; const int potentiometerPin = A2; unsigned long pumpStartTime = 0; // unsigned: only >=0 bool pumpRunning = false; void setup() { lcd.init(); lcd.backlight(); dht.begin(); pinMode(pumpPin, OUTPUT); pinMode(soilMoisturePin, INPUT); pinMode(potentiometerPin, INPUT); lcd.setCursor(2, 0); lcd.print("Initializing"); lcd.setCursor(0, 1); delay(2000); lcd.clear(); } void loop() { int moistureValue = analogRead(soilMoisturePin); // Read current soil moisture level int potentiometerValue = analogRead(potentiometerPin); // Read potentiometer value int targetMoisturePercent; // Map values to percentage int moisturePercent = map(moistureValue, 1023, 0, 0, 100); int potentiometerPercent = map(potentiometerValue, 0, 1023, 0, 100); if (potentiometerPercent >= 40) { targetMoisturePercent = potentiometerPercent; } else { targetMoisturePercent = 40; } // Check if pump should be running if (pumpRunning) { unsigned long elapsedTime = millis() - pumpStartTime; // If pump has been running for 20 seconds and soil moisture hasn't increased if (elapsedTime >= 20000 && moisturePercent <= 30) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Low water! Pump"); lcd.setCursor(0, 1); lcd.print("stopped."); // Stop the pump digitalWrite(pumpPin, LOW); pumpRunning = false; delay(20000); lcd.clear(); } // Stop the pump if desired soil moisture level is reached else if (moisturePercent >= targetMoisturePercent) { digitalWrite(pumpPin, LOW); // Stop the pump pumpRunning = false; lcd.clear(); lcd.setCursor(0, 0); lcd.print("Plants watered!"); lcd.setCursor(7, 1); lcd.print(":)"); delay(2000); // Display message for 2 seconds lcd.clear(); } else { lcd.setCursor(0, 0); lcd.print("Pump running..."); lcd.setCursor(0, 1); lcd.print("Soil: "); lcd.print(moisturePercent); lcd.print("% Man: "); lcd.print(potentiometerPercent); lcd.print("%"); } } else { float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println(F("Failed to read from DHT sensor!")); } // If pump has stopped, continuously display moisture and potentiometer levels lcd.setCursor(0, 0); lcd.print("Mst:"); lcd.print(moisturePercent); lcd.print("% "); lcd.setCursor(8, 0); lcd.print("Tmp:"); if (!isnan(t)) { lcd.print(t); lcd.print("oC "); } lcd.setCursor(8, 1); lcd.print("Hum:"); if (!isnan(h)) { lcd.print(h); lcd.print("% "); } lcd.setCursor(0, 1); lcd.print("Man:"); lcd.print(potentiometerPercent); lcd.print("% "); // Start the pump if potentiometerPercent is above 50% and soil is below target if (moisturePercent < targetMoisturePercent) { digitalWrite(pumpPin, HIGH); pumpStartTime = millis(); pumpRunning = true; lcd.clear(); lcd.setCursor(0, 0); lcd.print("Pump running..."); } } delay(2000); // Adjust delay as needed to update readings }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter