// Install from the Library Manager: // "TM1637" (Avishay Orpaz) #include // Rotary encoder (KY-040): two signals // and a button that closes when pressed const int ENCODER_CLK = 2; const int ENCODER_DT = 3; const int ENCODER_SW = 4; // The display module's CLK and DIO pins const int DISPLAY_CLK = 7; const int DISPLAY_DIO = 6; const int BUZZER_PIN = 5; // Set true if turning right lowers it const bool REVERSE = false; // Longest time: 99 minutes const long MAX_SECONDS = 99L * 60; // Holding this long resets (ms) const unsigned long LONG_PRESS = 800; // The alarm rings this long at most (ms) const unsigned long ALARM_TIME = 60000; // The colon in the middle of the display const uint8_t COLON = 0b01000000; TM1637Display display(DISPLAY_CLK, DISPLAY_DIO); // Setting, counting, paused and alarm enum State { SETTING, RUNNING, PAUSED, ALARM }; State state = SETTING; // Time set (s) and time left (ms) long setSeconds = 0; long remainingMs = 0; unsigned long lastTick = 0; unsigned long alarmStart = 0; bool beeping = false; // Clicks the interrupt counted (+ right) volatile int turns = 0; volatile unsigned long lastEdge = 0; unsigned long lastTurn = 0; bool buttonDown = false; unsigned long buttonChange = 0; unsigned long pressStart = 0; bool longDone = false; // What was last written on the display long shown = -1; // Runs on every click: when CLK falls, DT // tells which way the knob turned void onTurn() { // Bounces within 3 ms are not counted unsigned long now = millis(); if (now - lastEdge < 3) return; lastEdge = now; turns += digitalRead(ENCODER_DT) == HIGH ? 1 : -1; } long secondsLeft() { return (remainingMs + 999) / 1000; } // Prints a time to Serial Monitor (MM:SS) void printTime(const char *label, long seconds) { Serial.print(label); if (seconds / 60 < 10) Serial.print("0"); Serial.print(seconds / 60); Serial.print(":"); if (seconds % 60 < 10) Serial.print("0"); Serial.println(seconds % 60); } // One click's step: 10 s up to 2 minutes, // 30 s up to 10 minutes, then 1 minute long stepFor(long seconds, int dir) { if (dir > 0) seconds++; if (seconds > 600) return 60; if (seconds > 120) return 30; return 10; } void stopAlarm() { noTone(BUZZER_PIN); beeping = false; state = SETTING; // One press runs the same time again remainingMs = setSeconds * 1000; Serial.println("Alarm stopped."); } void turn(int dir) { if (state == ALARM) { stopAlarm(); return; } long seconds = state == SETTING ? setSeconds : secondsLeft(); long step = stepFor(seconds, dir); // Lands on a step: right from 47 is 50 long next = dir > 0 ? (seconds / step + 1) * step : ((seconds + step - 1) / step - 1) * step; next = constrain(next, 0, MAX_SECONDS); if (next == seconds) return; // While counting it never turns to zero if (next == 0 && state != SETTING) return; if (state == SETTING) setSeconds = next; remainingMs = next * 1000; printTime("Time: ", next); tone(BUZZER_PIN, 4000, 2); } void shortPress() { if (state == SETTING) { if (setSeconds == 0) return; remainingMs = setSeconds * 1000; state = RUNNING; lastTick = millis(); tone(BUZZER_PIN, 1500, 80); printTime("Started: ", setSeconds); } else if (state == RUNNING) { state = PAUSED; tone(BUZZER_PIN, 800, 80); printTime("Paused: ", secondsLeft()); } else if (state == PAUSED) { state = RUNNING; lastTick = millis(); tone(BUZZER_PIN, 1500, 80); printTime("Resumed: ", secondsLeft()); } } // Held down: when counting, back to the // time set; when setting, back to zero void longPress() { if (state == SETTING) setSeconds = 0; state = SETTING; remainingMs = setSeconds * 1000; tone(BUZZER_PIN, 600, 150); printTime("Reset: ", setSeconds); } // A short press starts or pauses, a long // press resets void readButton() { bool down = digitalRead(ENCODER_SW) == LOW; if (down != buttonDown) { // Contact bounce: wait 30 ms if (millis() - buttonChange < 30) return; buttonChange = millis(); buttonDown = down; if (down) { pressStart = millis(); longDone = false; // A press stops the alarm at once if (state == ALARM) { stopAlarm(); longDone = true; } } else if (!longDone) { shortPress(); } } else if (down && !longDone && millis() - pressStart >= LONG_PRESS) { longDone = true; longPress(); } } void update() { unsigned long now = millis(); if (state == RUNNING) { remainingMs -= (long)(now - lastTick); lastTick = now; if (remainingMs <= 0) { remainingMs = 0; state = ALARM; alarmStart = now; Serial.println("Time's up!"); } } if (state == ALARM) { unsigned long t = now - alarmStart; // Goes quiet by itself after a minute if (t >= ALARM_TIME) { stopAlarm(); return; } // Three short beeps every second bool on = t % 1000 < 600 && t % 200 < 100; if (on != beeping) { beeping = on; if (on) tone(BUZZER_PIN, 2000); else noTone(BUZZER_PIN); } } } // Writes to the display only on a change; // writing takes a few ms void updateDisplay() { unsigned long now = millis(); long seconds = state == SETTING ? setSeconds : secondsLeft(); bool colon = true; bool visible = true; if (state == RUNNING) { // The colon blinks once a second colon = remainingMs % 1000 >= 500; } else if (state == PAUSED) { // Paused, the digits blink; after a // turn the new time stays in view visible = now % 1000 < 600 || now - lastTurn < 1500; } else if (state == ALARM) { visible = (now - alarmStart) % 1000 < 500; } long key = seconds * 4 + colon * 2 + visible; if (key == shown) return; shown = key; if (!visible) { display.clear(); return; } int value = seconds / 60 * 100 + seconds % 60; display.showNumberDecEx(value, colon ? COLON : 0, true); } void setup() { Serial.begin(9600); pinMode(ENCODER_CLK, INPUT_PULLUP); pinMode(ENCODER_DT, INPUT_PULLUP); pinMode(ENCODER_SW, INPUT_PULLUP); // CLK on an interrupt: no lost clicks attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), onTurn, FALLING); display.setBrightness(5); Serial.println("Kitchen timer ready."); } void loop() { // Take the clicks the interrupt counted noInterrupts(); int n = turns; turns = 0; interrupts(); if (REVERSE) n = -n; if (n != 0) lastTurn = millis(); while (n > 0) { turn(1); n--; } while (n < 0) { turn(-1); n++; } readButton(); update(); updateDisplay(); }