#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD with I2C address 0x27, 16 columns and 2 rows
// Keypad configuration (4x3)
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad // Define column pins for the keypad (3 columns)
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int soilSensorPin = A0; // Analog pin for soil moisture sensor
int pumpPin = A3; // Digital pin for controlling the pump
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight(); // Turn on LCD backlight
lcd.clear(); // Clear LCD screen
// Display menu options on two lines
lcd.setCursor(0, 0);
lcd.print("1Sensor Reading");
lcd.setCursor(0, 1);
lcd.print("2Threshold");
lcd.setCursor(11, 1); // Move cursor to position 11 of the second line
lcd.print("3Timer");
// Set pin modes
pinMode(soilSensorPin, INPUT); // Set soil moisture sensor pin as input
pinMode(pumpPin, OUTPUT); // Set pump pin as output
}
void loop() {
char key = keypad.getKey(); // Get pressed key from the keypad
if (key) {
switch (key) {
case '1':
sensorReading(); // Perform soil moisture reading function
break;
case '2':
sensorThreshold(); // Perform soil moisture threshold function
break;
case '3':
timerFunction(); // Perform timer function
break;
}
}
}
// Function to read soil moisture sensor value
void sensorReading() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Soil Moisture:");
// Read soil moisture value from the sensor
int soilMoisture = analogRead(soilSensorPin);
lcd.setCursor(0, 1);
lcd.print("Value: ");
lcd.print(soilMoisture);
delay(2000); // Delay before returning to the main screen
}
// Function to control pump based on soil moisture threshold
void sensorThreshold() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Threshold:");
int threshold = 0; // Initialize threshold value
// Capture the entered threshold value
String thresholdStr = "";
while (1) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print(key);
thresholdStr += key;
} else if (key == '#') {
break; // Break loop when '#' key is pressed
}
}
// Convert the entered string to an integer
threshold = thresholdStr.toInt();
// Display the entered threshold value on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Threshold:");
lcd.setCursor(0, 1);
lcd.print(threshold);
delay(2000); // Delay before returning to the main screen
// Read soil moisture value from the sensor
int soilMoisture = analogRead(soilSensorPin);
// Compare soil moisture value with the entered threshold
if (soilMoisture < threshold) {
lcd.setCursor(0, 1);
lcd.print("Pump ON");
digitalWrite(pumpPin, HIGH); // Turn on the pump
} else {
lcd.setCursor(0, 1);
lcd.print("Pump OFF");
digitalWrite(pumpPin, LOW); // Turn off the pump
}
delay(2000); // Delay before returning to the main screen
}/*
void timerFunction() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Time (MM:SS)");
int minutes = 0;
int seconds = 0;
bool enteringMinutes = true; // Flag to track whether entering minutes or seconds
while (true) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
if (enteringMinutes) {
// Input for minutes
if (minutes < 10) {
// Capture tens place of minutes
minutes = minutes * 10 + (key - '0'); // Convert char to int
lcd.setCursor(10, 0); // Set cursor to display minutes
lcd.print(minutes);
} else {
// Capture ones place of minutes
minutes = key - '0'; // Convert char to int
lcd.setCursor(11, 0); // Set cursor to display minutes
lcd.print(minutes);
enteringMinutes = false; // Switch to entering seconds
}
} else {
// Input for seconds
if (seconds < 10) {
// Capture tens place of seconds
seconds = seconds * 10 + (key - '0'); // Convert char to int
lcd.setCursor(13, 0); // Set cursor to display seconds
lcd.print(seconds);
} else {
// Capture ones place of seconds
seconds = key - '0'; // Convert char to int
lcd.setCursor(14, 0); // Set cursor to display seconds
lcd.print(seconds);
break; // Exit input loop after entering all digits
}
}
}
}
// Calculate total duration in seconds
unsigned long duration = (minutes * 60 + seconds) * 1000;
unsigned long startTime = millis();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("pump on");
lcd.setCursor(0, 1);
lcd.print("Timer Running");
// Run pump for the specified duration
while (millis() - startTime < duration) {
digitalWrite(pumpPin, HIGH); // Turn on the pump
// Additional actions can be added here
}
digitalWrite(pumpPin, LOW); // Turn off the pump after timer completes
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timer Complete");
delay(2000); // Delay before returning to the main screen
}*/
void timerFunction() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Time (MM:SS)");
int minutes = 0;
int seconds = 0;
bool enteringMinutes = true; // Flag to track whether entering minutes or seconds
// Capture minutes and seconds input
while (true) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
if (enteringMinutes) {
// Input for minutes
if (minutes < 10) {
minutes = minutes * 10 + (key - '0'); // Capture tens and ones place of minutes
lcd.setCursor(10, 0);
lcd.print(minutes);
} else {
enteringMinutes = false; // Switch to entering seconds
}
} else {
// Input for seconds
if (seconds < 10) {
seconds = seconds * 10 + (key - '0'); // Capture tens and ones place of seconds
lcd.setCursor(13, 0);
lcd.print(seconds);
} else {
break; // Exit loop after capturing all digits
}
}
}
}
// Calculate total duration in seconds
unsigned long baseDuration = (minutes * 60 + seconds) * 1000;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Repetitions:");
int repetitions = 0;
String repetitionsStr = "";
// Capture repetitions input
while (true) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print(key); // Display input number on LCD
repetitionsStr += key; // Append key press to string
} else if (key == '#') {
// Convert string to integer
repetitions = repetitionsStr.toInt();
break; // Exit loop on '#' key press
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Running Pump...");
// Run pump for the specified number of repetitions
for (int i = 0; i < repetitions; i++) {
void runPump(unsigned long duration);
//runPump(baseDuration);
if (i < repetitions - 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Next in 5s...");
delay(5000); // Delay between repetitions
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pump Complete");
delay(2000); // Delay before returning to the main screen
}