/*
* 4-Digit 7-Segment Clock Display
* Displays time in HH:MM format using TM1637 display module
* Features: Clock display, adjustable time, blinking colon
*/
#include <TM1637Display.h>
// Pin definitions for TM1637
#define CLK_PIN 2 // Clock pin
#define DIO_PIN 3 // Data pin
// Button pins for setting time
#define HOUR_BTN 4 // Button to increment hours
#define MIN_BTN 5 // Button to increment minutes
#define SET_BTN 6 // Button to enter/exit set mode
// Create display object
TM1637Display display(CLK_PIN, DIO_PIN);
// Time variables
int hours = 12;
int minutes = 0;
int seconds = 0;
// State variables
bool setMode = false; // Time setting mode flag
bool colonBlink = true; // Colon blinking state
unsigned long lastUpdate = 0; // Last time update timestamp
unsigned long lastBlink = 0; // Last colon blink timestamp
// Button debouncing
unsigned long lastHourPress = 0;
unsigned long lastMinPress = 0;
unsigned long lastSetPress = 0;
const int debounceDelay = 200;
// Segment data for digits with colon
const uint8_t SEG_COLON = 0b01000000; // Colon segment
void setup() {
Serial.begin(9600);
// Initialize button pins
pinMode(HOUR_BTN, INPUT_PULLUP);
pinMode(MIN_BTN, INPUT_PULLUP);
pinMode(SET_BTN, INPUT_PULLUP);
// Initialize display
display.setBrightness(0x0f); // Maximum brightness
// Display startup message
uint8_t data[] = {0x00, 0x00, 0x00, 0x00};
display.setSegments(data);
delay(500);
Serial.println("4-Digit 7-Segment Clock Initialized");
Serial.println("Use buttons to set time:");
Serial.println("- SET button: Enter/Exit set mode");
Serial.println("- HOUR button: Increment hours");
Serial.println("- MIN button: Increment minutes");
}
void loop() {
unsigned long currentMillis = millis();
// Check button presses
checkButtons();
// Update seconds and time (only when not in set mode)
if (!setMode && currentMillis - lastUpdate >= 1000) {
lastUpdate = currentMillis;
updateTime();
}
// Blink colon every 500ms
if (currentMillis - lastBlink >= 500) {
lastBlink = currentMillis;
colonBlink = !colonBlink;
}
// Display time
displayTime();
delay(10); // Small delay for stability
}
void updateTime() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
// Print to Serial Monitor
Serial.print("Time: ");
if (hours < 10) Serial.print("0");
Serial.print(hours);
Serial.print(":");
if (minutes < 10) Serial.print("0");
Serial.print(minutes);
Serial.print(":");
if (seconds < 10) Serial.print("0");
Serial.println(seconds);
}
void displayTime() {
// Create time display value
int displayValue = hours * 100 + minutes;
// Show time with or without colon based on blink state
if (setMode) {
// In set mode, always show colon
display.showNumberDecEx(displayValue, 0b01000000, true);
} else {
// Normal mode, blink colon
if (colonBlink) {
display.showNumberDecEx(displayValue, 0b01000000, true);
} else {
display.showNumberDecEx(displayValue, 0b00000000, true);
}
}
}
void checkButtons() {
unsigned long currentMillis = millis();
// SET button - toggle set mode
if (digitalRead(SET_BTN) == LOW && currentMillis - lastSetPress > debounceDelay) {
lastSetPress = currentMillis;
setMode = !setMode;
if (setMode) {
Serial.println("*** SET MODE ENABLED ***");
Serial.println("Use HOUR and MIN buttons to adjust time");
} else {
Serial.println("*** SET MODE DISABLED ***");
Serial.println("Time saved. Clock running.");
seconds = 0; // Reset seconds when exiting set mode
lastUpdate = currentMillis; // Reset update timer
}
delay(200); // Additional debounce
}
// HOUR button - increment hours (only in set mode)
if (setMode && digitalRead(HOUR_BTN) == LOW && currentMillis - lastHourPress > debounceDelay) {
lastHourPress = currentMillis;
hours++;
if (hours >= 24) {
hours = 0;
}
Serial.print("Hours set to: ");
Serial.println(hours);
}
// MIN button - increment minutes (only in set mode)
if (setMode && digitalRead(MIN_BTN) == LOW && currentMillis - lastMinPress > debounceDelay) {
lastMinPress = currentMillis;
minutes++;
if (minutes >= 60) {
minutes = 0;
}
Serial.print("Minutes set to: ");
Serial.println(minutes);
}
}
Comments
Post a Comment