/*
Timer circuit: Arduino Uno, 2 LEDs, 2 push buttons, I2C 16x2 LCD
Wiring
Start button : pin 2 -> GND (INPUT_PULLUP, no resistor needed)
Select button : pin 3 -> GND (INPUT_PULLUP, no resistor needed)
Green LED : pin 8 -> 220 ohm -> LED anode, cathode -> GND
Red LED : pin 9 -> 220 ohm -> LED anode, cathode -> GND
I2C LCD : GND, 5V, SDA -> A4, SCL -> A5
Flow
IDLE : everything off, waiting for START
SELECT : green on, SELECT cycles 30 -> 60 -> 90 -> 30 min.
3 s after the last press the timer starts
RUNNING : red on, green off, LCD shows time left
DONE : green on again, LCD says time is up. START begins again
Wokwi: add the "LiquidCrystal I2C" library in the Library Manager.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ---------- Pins ----------
const uint8_t PIN_START = 2;
const uint8_t PIN_SELECT = 3;
const uint8_t PIN_GREEN = 9;
const uint8_t PIN_RED = 8;
// ---------- Settings ----------
// TEST_MODE true : "30 min" really runs for 30 seconds, so you can test quickly.
// TEST_MODE false: real minutes. Set this to false for the final version.
#define TEST_MODE true
const uint8_t LCD_ADDR = 0x27; // Wokwi uses 0x27
const unsigned long GRACE_MS = 3000UL; // grace period after the last SELECT press
const uint8_t DURATIONS[] = {30, 60, 90};
const uint8_t NUM_DURATIONS = 3;
const unsigned long UNIT_MS = TEST_MODE ? 1000UL : 60000UL; // length of one "minute"
LiquidCrystal_I2C lcd(LCD_ADDR, 16, 2);
// ---------- State ----------
enum State { IDLE, SELECT, RUNNING, DONE };
State state = IDLE;
uint8_t durIndex = 0; // which duration is selected
unsigned long graceStart = 0; // time of last SELECT press (or entering SELECT)
unsigned long runStart = 0; // when the countdown began
unsigned long runDuration = 0; // countdown length in ms
unsigned long lastShownS = 0xFFFFFFFFUL; // last second value drawn on the LCD
// ---------- Debounced button ----------
struct Button {
uint8_t pin;
bool lastReading;
bool stable;
unsigned long lastChange;
};
Button btnStart = {PIN_START, HIGH, HIGH, 0};
Button btnSelect = {PIN_SELECT, HIGH, HIGH, 0};
// Returns true once for each press (HIGH -> LOW after debouncing)
bool pressed(Button &b) {
bool reading = digitalRead(b.pin);
if (reading != b.lastReading) {
b.lastReading = reading;
b.lastChange = millis();
}
if ((millis() - b.lastChange) > 30 && reading != b.stable) {
b.stable = reading;
if (b.stable == LOW) return true;
}
return false;
}
// ---------- LCD helpers ----------
void showSelection() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select time:");
lcd.setCursor(0, 1);
lcd.print(DURATIONS[durIndex]);
lcd.print(" min");
}
void showCountdown(unsigned long remainingS) {
unsigned long h = remainingS / 3600UL;
unsigned long m = (remainingS % 3600UL) / 60UL;
unsigned long s = remainingS % 60UL;
char buf[9];
snprintf(buf, sizeof(buf), "%02lu:%02lu:%02lu", h, m, s);
lcd.setCursor(0, 1);
lcd.print(buf);
}
// ---------- State changes ----------
void enterIdle() {
state = IDLE;
digitalWrite(PIN_GREEN, HIGH);
digitalWrite(PIN_RED, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reserve Your Seat");
}
void enterSelect() {
state = SELECT;
durIndex = 0; // default is 30 min
graceStart = millis();
digitalWrite(PIN_GREEN, HIGH);
digitalWrite(PIN_RED, LOW);
showSelection();
}
void enterRunning() {
state = RUNNING;
runDuration = (unsigned long)DURATIONS[durIndex] * UNIT_MS;
runStart = millis();
lastShownS = 0xFFFFFFFFUL;
digitalWrite(PIN_GREEN, LOW);
digitalWrite(PIN_RED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time left:");
}
void enterDone() {
state = DONE;
digitalWrite(PIN_RED, LOW);
digitalWrite(PIN_GREEN, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time is up!");
lcd.setCursor(0, 1);
lcd.print("START = again");
}
// ---------- Arduino ----------
void setup() {
pinMode(PIN_START, INPUT_PULLUP);
pinMode(PIN_SELECT, INPUT_PULLUP);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_RED, OUTPUT);
lcd.init();
lcd.backlight();
enterIdle();
}
void loop() {
bool startPressed = pressed(btnStart);
bool selectPressed = pressed(btnSelect);
switch (state) {
case IDLE:
if (startPressed) enterSelect();
break;
case SELECT:
if (selectPressed) {
durIndex = (durIndex + 1) % NUM_DURATIONS;
graceStart = millis(); // restart the 3 s grace period
showSelection();
} else if (millis() - graceStart >= GRACE_MS) {
enterRunning();
}
break;
case RUNNING: {
unsigned long elapsed = millis() - runStart;
if (elapsed >= runDuration) {
enterDone();
} else {
unsigned long remainingS = (runDuration - elapsed + 999UL) / 1000UL; // round up
if (remainingS != lastShownS) { // redraw only when the second changes
lastShownS = remainingS;
showCountdown(remainingS);
}
}
break;
}
case DONE:
if (startPressed) enterSelect();
break;
}
}