/*
MSJ Researchers World
Date - 2nd DEC 2024
Mentor - Mr. Siranjeevi M
Contact - 7373771991
*/
#include <TM1637Display.h>
#include <Wire.h>
#include <RTClib.h>
// Initialize TM1637 display (CLK, DIO pins)
TM1637Display display(5, 3);
// Initialize RTC
RTC_DS1307 rtc;
// Button pin
const int buttonPin = 4; // Button to toggle between time and date
bool showTime = true; // Flag to show time or date
// Variable to store last button press time for debouncing
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // debounce delay in milliseconds
void setup() {
Serial.begin(9600);
// Initialize display
display.setBrightness(7); // Adjust brightness (0-7)
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is not running! Setting current time.");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set RTC to compile time
}
// Initialize button pin
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
}
void loop() {
// Read the button state
int buttonState = digitalRead(buttonPin);
// Check if the button was pressed (LOW means pressed due to pull-up)
if (buttonState == LOW) {
// Debounce button press to avoid multiple toggles
if (millis() - lastDebounceTime > debounceDelay) {
showTime = !showTime; // Toggle between time and date
lastDebounceTime = millis();
}
}
// Get current date and time
DateTime now = rtc.now();
if (showTime) {
// Display time in HH:MM format
int displayTime = now.hour() * 100 + now.minute(); // HHMM format
display.showNumberDecEx(displayTime, 0b01000000, true); // ":" separator
} else {
// Display date in DD:MM format
int displayDate = now.day() * 100 + now.month(); // DDMM format
display.showNumberDecEx(displayDate, 0b00000000, true); // No colon
}
delay(1000); // Update every second
}