#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// I2C LCD setup (address, columns, rows)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to your LCD's I2C address
// Pin definitions
const int buttonUp = 2;
const int buttonDown = 3;
const int buttonEnter = 4;
const int buttonBack = 5;
const int buzzerPin = 6;
const int waterLevelSensor = A0;
// Global variables
int menuState = 0;
int washCycles = 1;
int washTime[4] = {0}; // Array to store wash times for each cycle
int waterLevel[4] = {0}; // Array to store water levels for each cycle
int currentCycle = 0;
bool washing = false;
void setup() {
// Initialize buttons
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonEnter, INPUT_PULLUP);
pinMode(buttonBack, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(waterLevelSensor, INPUT);
// Initialize I2C LCD
lcd.init();
lcd.backlight();
lcd.print("Washing Machine");
lcd.setCursor(0, 1);
lcd.print("V1.0 - Press Enter");
}
void loop() {
// Main state machine
switch (menuState) {
case 0: // Initial screen
if (digitalRead(buttonEnter) == LOW) {
delay(200); // Debounce
menuState = 1;
lcd.clear();
lcd.print("Select Option:");
lcd.setCursor(0, 1);
lcd.print("Wash Timer > Dryer");
}
break;
case 1: // Wash Timer or Dryer selection
if (digitalRead(buttonUp) == LOW || digitalRead(buttonDown) == LOW) {
delay(200); // Debounce
lcd.clear();
lcd.print("Select Option:");
lcd.setCursor(0, 1);
lcd.print("< Wash Timer Dryer");
}
if (digitalRead(buttonEnter) == LOW) {
delay(200); // Debounce
menuState = 2;
lcd.clear();
lcd.print("Wash Cycles:");
lcd.setCursor(0, 1);
lcd.print("Cycles: " + String(washCycles));
}
break;
case 2: // Wash cycles selection
if (digitalRead(buttonUp) == LOW) {
delay(200); // Debounce
washCycles = min(washCycles + 1, 4);
lcd.setCursor(8, 1);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print(String(washCycles));
}
if (digitalRead(buttonDown) == LOW) {
delay(200); // Debounce
washCycles = max(washCycles - 1, 1);
lcd.setCursor(8, 1);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print(String(washCycles));
}
if (digitalRead(buttonEnter) == LOW) {
delay(200); // Debounce
menuState = 3;
currentCycle = 0;
lcd.clear();
lcd.print("Wash Time " + String(currentCycle + 1));
lcd.setCursor(0, 1);
lcd.print("Time: " + String(washTime[currentCycle]) + " min");
}
break;
case 3: // Wash time selection
if (digitalRead(buttonUp) == LOW) {
delay(200); // Debounce
washTime[currentCycle] = min(washTime[currentCycle] + 1, 60);
lcd.setCursor(6, 1);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(String(washTime[currentCycle]));
}
if (digitalRead(buttonDown) == LOW) {
delay(200); // Debounce
washTime[currentCycle] = max(washTime[currentCycle] - 1, 1);
lcd.setCursor(6, 1);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(String(washTime[currentCycle]));
}
if (digitalRead(buttonEnter) == LOW) {
delay(200); // Debounce
currentCycle++;
if (currentCycle >= washCycles) {
menuState = 4;
currentCycle = 0;
lcd.clear();
lcd.print("Water Level " + String(currentCycle + 1));
lcd.setCursor(0, 1);
lcd.print("Level: " + String(waterLevel[currentCycle]));
} else {
lcd.clear();
lcd.print("Wash Time " + String(currentCycle + 1));
lcd.setCursor(0, 1);
lcd.print("Time: " + String(washTime[currentCycle]) + " min");
}
}
break;
case 4: // Water level selection
if (digitalRead(buttonUp) == LOW) {
delay(200); // Debounce
waterLevel[currentCycle] = min(waterLevel[currentCycle] + 1, 100);
lcd.setCursor(7, 1);
lcd.print(" ");
lcd.setCursor(7, 1);
lcd.print(String(waterLevel[currentCycle]));
}
if (digitalRead(buttonDown) == LOW) {
delay(200); // Debounce
waterLevel[currentCycle] = max(waterLevel[currentCycle] - 1, 0);
lcd.setCursor(7, 1);
lcd.print(" ");
lcd.setCursor(7, 1);
lcd.print(String(waterLevel[currentCycle]));
}
if (digitalRead(buttonEnter) == LOW) {
delay(200); // Debounce
currentCycle++;
if (currentCycle >= washCycles) {
menuState = 5;
lcd.clear();
lcd.print("Pouring Water...");
startWashing();
} else {
lcd.clear();
lcd.print("Water Level " + String(currentCycle + 1));
lcd.setCursor(0, 1);
lcd.print("Level: " + String(waterLevel[currentCycle]));
}
}
break;
case 5: // Washing process
if (!washing) {
washing = true;
for (int i = 0; i < washCycles; i++) {
lcd.clear();
lcd.print("Wash Cycle " + String(i + 1));
lcd.setCursor(0, 1);
lcd.print("Time: " + String(washTime[i]) + " min");
delay(washTime[i] * 60000); // Simulate wash time
lcd.clear();
lcd.print("Draining Water...");
delay(60000); // Simulate draining
}
lcd.clear();
lcd.print("Washing Complete!");
digitalWrite(buzzerPin, HIGH);
delay(2000);
digitalWrite(buzzerPin, LOW);
washing = false;
menuState = 0; // Reset to initial state
}
break;
}
}
void startWashing() {
// Simulate water pouring
while (analogRead(waterLevelSensor) < waterLevel[currentCycle] * 10) {
delay(100);
}
lcd.clear();
lcd.print("Water Level OK");
delay(2000);
}