#include <LiquidCrystal_I2C.h>
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int potassiumSensorPin = A0; // Potassium sensor analog pin
const int nitrogenSensorPin = A1; // Nitrogen sensor analog pin
const int phosphorusSensorPin = A2; // Phosphorus sensor analog pin
const int ledPin = 4; // LED connected to digital pin 4
// Thresholds in ppm
const int potassiumThreshold = 200; // ppm
const int nitrogenThreshold = 500; // ppm
const int phosphorusThreshold = 300; // ppm
// Mapping range
const int sensorMin = 0; // sensor minimum, representing 0 ppm
const int sensorMax = 1023; // sensor maximum, representing 1000 ppm
unsigned long previousMillis = 0; // will store last time the nutrients were updated
const long interval = 10000; // interval at which to update the nutrient readings (10 seconds)
void setup() {
lcd.init(); // Start the LCD
lcd.backlight();
pinMode(potassiumSensorPin, INPUT);
pinMode(nitrogenSensorPin, INPUT);
pinMode(phosphorusSensorPin, INPUT);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
digitalWrite(ledPin, LOW); // Start with the LED off
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Read the value from the potentiometers
int potassiumLevel = analogRead(potassiumSensorPin);
int nitrogenLevel = analogRead(nitrogenSensorPin);
int phosphorusLevel = analogRead(phosphorusSensorPin);
// Map the potentiometer value to a ppm range (0 - 1000 ppm for example)
int potassiumPPM = map(potassiumLevel, sensorMin, sensorMax, 0, 1000);
int nitrogenPPM = map(nitrogenLevel, sensorMin, sensorMax, 0, 1000);
int phosphorusPPM = map(phosphorusLevel, sensorMin, sensorMax, 0, 1000);
// Clear the LCD and set the cursor to the beginning
lcd.clear();
lcd.setCursor(0, 0);
// Display the current simulated nutrient levels
lcd.print("K: ");
lcd.print(potassiumPPM);
lcd.print(" N: ");
lcd.print(nitrogenPPM);
lcd.print(" P: ");
lcd.print(phosphorusPPM);
// Check if any nutrient level is below the threshold and turn on the LED if so
if (potassiumPPM < potassiumThreshold || nitrogenPPM < nitrogenThreshold || phosphorusPPM < phosphorusThreshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
}