#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the address if needed
const int selectButtonPin = 6; // Pin for the select button
const int startButtonPin = 7; // Pin for the start/stop button
int selectedPreset = 0; // 0: No preset, 1: Rice, 2: Egg, 3: Lentil
int presets[] = {0, 1500, 720, 1200}; // Preset durations in seconds (0 for custom)
unsigned long startTime = 0;
bool timerRunning = false;
void setup() {
lcd.begin(16, 2);
lcd.backlight();
pinMode(selectButtonPin, INPUT_PULLUP);
pinMode(startButtonPin, INPUT_PULLUP);
}
void loop() {
handleButtons();
if (timerRunning) {
displayTimer();
checkTimer();
} else {
displayMenu();
}
delay(100);
}
void handleButtons() {
if (digitalRead(selectButtonPin) == LOW) {
selectPreset();
}
if (digitalRead(startButtonPin) == LOW) {
startStopTimer();
}
}
void selectPreset() {
selectedPreset = (selectedPreset % 3) + 1; // Cycle through presets (1-3)
displayPreset();
delay(500); // Debounce delay
}
void startStopTimer() {
if (timerRunning) {
stopTimer();
} else {
startTimer();
}
delay(500); // Debounce delay
}
void startTimer() {
if (selectedPreset > 0) {
startTime = millis();
timerRunning = true;
}
}
void stopTimer() {
timerRunning = false;
}
void displayPreset() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selected Preset:");
switch (selectedPreset) {
case 1:
lcd.setCursor(0, 1);
lcd.print("Rice");
break;
case 2:
lcd.setCursor(0, 1);
lcd.print("Egg");
break;
case 3:
lcd.setCursor(0, 1);
lcd.print("Lentil");
break;
default:
lcd.setCursor(0, 1);
lcd.print("Custom");
}
}
void displayMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Rice");
lcd.setCursor(0, 1);
lcd.print("2. Egg");
}
void displayTimer() {
unsigned long currentTime = millis();
unsigned long elapsedTime = (currentTime - startTime) / 1000;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(elapsedTime / 60);
lcd.print("m ");
lcd.print(elapsedTime % 60);
lcd.print("s");
}
void checkTimer() {
unsigned long currentTime = millis();
unsigned long elapsedTime = (currentTime - startTime) / 1000;
if (elapsedTime >= presets[selectedPreset]) {
stopTimer();
buzzer(); // Add your buzzer function here
}
}
void buzzer() {
// Implement your buzzer function to indicate the end of the timer
// For simplicity, you can toggle a pin or add more complex melody logic
// For example:
// digitalWrite(buzzerPin, HIGH);
// delay(1000);
// digitalWrite(buzzerPin, LOW);
}