#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Initialize the LCD library with the address of the I2C module
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the keypad
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
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Relay control pin
const int relayPin = 10;
// Variables to store selected time (two digits)
int tensDigit = 0;
int onesDigit = 0;
// Flag to indicate if washing is in progress
bool washingInProgress = false;
unsigned long washingStartTime = 0;
unsigned long washingDuration = 0;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select time (min)");
// Initialize relay pin
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Make sure relay is initially off
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == 'A') {
if (tensDigit > 0 || onesDigit > 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("OK");
delay(1000); // Delay to show "OK" message
lcd.clear(); // Clear LCD after showing "OK"
lcd.setCursor(0, 0);
lcd.print("Washing...");
int selectedTime = tensDigit * 10 + onesDigit;
startWashing(selectedTime * 60); // Start washing for selected time
tensDigit = 0;
onesDigit = 0;
}
} else if (key == 'B') {
if (washingInProgress) {
stopWashing();
}
} else if (key == 'C') {
tensDigit = 0; // Clear selected time if 'C' is pressed
onesDigit = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select time (min)");
} else if (isdigit(key)) {
int digit = key - '0'; // Convert char to int for selected time
if (tensDigit == 0) {
tensDigit = digit;
lcd.setCursor(0, 1);
lcd.print("Selected: ");
lcd.print(tensDigit);
lcd.print(onesDigit);
lcd.print(" min ");
} else if (onesDigit == 0 && digit >= 0) {
onesDigit = digit;
lcd.setCursor(0, 1);
lcd.print("Selected: ");
lcd.print(tensDigit);
lcd.print(onesDigit);
lcd.print(" min ");
}
}
}
if (washingInProgress) {
if (millis() - washingStartTime >= washingDuration) {
stopWashing();
}
}
}
void startWashing(int seconds) {
digitalWrite(relayPin, HIGH); // Activate relay
washingInProgress = true;
washingStartTime = millis();
washingDuration = seconds * 1000; // Convert seconds to milliseconds
}
void stopWashing() {
digitalWrite(relayPin, LOW); // Deactivate relay
washingInProgress = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stopped");
delay(2000); // Display "Stopped" message for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select time (min)");
}