Hey guys im trying to make like a countdown where you can switch between hours minutes and seconds. Theres one button to switch between the three and another to change the value of the time they picked. Last to start the countdown and when the countdown ends the servos move. My problem is like the switching button only switches to Minutes and doesnt go to seconds or hours. The other problem is the increasing button only goes up to 1. Please help guys
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
Servo mainServo;
Servo servo1;
Servo servo2;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int selectButton = 2;
const int increaseButton = 3;
const int startButton = 4;
int selectedTime = 0; // 0 = hours, 1 = minutes, 2 = seconds
int countdownHours = 0;
int countdownMinutes = 0;
int countdownSeconds = 0;
bool lastSelectState = HIGH;
bool lastIncreaseState = HIGH;
bool lastStartState = HIGH;
void setup() {
Serial.begin(9600);
mainServo.attach(9);
servo1.attach(10);
servo2.attach(11);
lcd.init();
lcd.backlight();
pinMode(selectButton, INPUT_PULLUP);
pinMode(increaseButton, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
displayTimeSelection();
}
void loop() {
if (buttonPressed(selectButton, lastSelectState)) {
selectedTime = (selectedTime + 1) % 3; // Cycles between 0, 1, 2
displayTimeSelection();
}
if (buttonPressed(increaseButton, lastIncreaseState)) {
increaseTime();
displayTimeSelection();
}
if (buttonPressed(startButton, lastStartState)) {
startCountdown();
}
}
void increaseTime() {
if (selectedTime == 0 && countdownHours < 23) {
countdownHours++;
} else if (selectedTime == 1 && countdownMinutes < 59) {
countdownMinutes++;
} else if (selectedTime == 2 && countdownSeconds < 59) {
countdownSeconds++;
}
}
void displayTimeSelection() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Time:");
lcd.setCursor(0, 1);
lcd.print(selectedTime == 0 ? "[H] " : " H ");
lcd.print(countdownHours);
lcd.print(" : ");
lcd.print(selectedTime == 1 ? "[M] " : " M ");
lcd.print(countdownMinutes);
lcd.print(" : ");
lcd.print(selectedTime == 2 ? "[S] " : " S ");
lcd.print(countdownSeconds);
}
void startCountdown() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Counting down...");
while (countdownHours > 0 || countdownMinutes > 0 || countdownSeconds > 0) {
lcd.setCursor(0, 1);
lcd.print(countdownHours);
lcd.print(" : ");
lcd.print(countdownMinutes);
lcd.print(" : ");
lcd.print(countdownSeconds);
delay(1000);
if (countdownSeconds > 0) {
countdownSeconds--;
} else if (countdownMinutes > 0) {
countdownMinutes--;
countdownSeconds = 59;
} else if (countdownHours > 0) {
countdownHours--;
countdownMinutes = 59;
countdownSeconds = 59;
}
}
triggerServo();
}
void triggerServo() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing...");
mainServo.write(45);
delay(1000);
mainServo.write(180);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Done!");
}
bool buttonPressed(int button, bool &lastState) {
bool currentState = digitalRead(button);
if (currentState == LOW && lastState == HIGH) {
delay(50);
while (digitalRead(button) == LOW);
lastState = LOW;
return true;
}
lastState = currentState;
return false;
}
Here my tinkercad for the timer it a little messy right but ill tidy it up once i get to fix my problem


