#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
// Pin Definitions
#define SEGMENT_A_PIN 2
#define SEGMENT_B_PIN 3
#define SEGMENT_C_PIN 4
#define SEGMENT_D_PIN 5
#define SEGMENT_E_PIN 6
#define SEGMENT_F_PIN 7
#define SEGMENT_G_PIN 8
#define SEGMENT_DP_PIN 13
// Define pins for hour and minute adjustment buttons
#define BUTTON_HOUR_PIN 9
#define BUTTON_MINUTE_PIN 10
// Digital Pins for digits
#define DIGIT_1_PIN A0
#define DIGIT_2_PIN A1
#define DIGIT_3_PIN A2
#define DIGIT_4_PIN A3
// Segment Pins
const int segmentPins[] = {SEGMENT_A_PIN, SEGMENT_B_PIN, SEGMENT_C_PIN, SEGMENT_D_PIN, SEGMENT_E_PIN, SEGMENT_F_PIN, SEGMENT_G_PIN};
const int digitPins[] = {DIGIT_1_PIN, DIGIT_2_PIN, DIGIT_3_PIN, DIGIT_4_PIN};
// Variables to store current time
int currentHour = 0;
int currentMinute = 0;
int currentSecond = 0;
// Function to update the display with the current time
void updateDisplay() {
// Turn off all digits
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], LOW);
}
// Get individual digits of the hour and minute
int hourDigit1 = currentHour / 10;
int hourDigit2 = currentHour % 10;
int minuteDigit1 = currentMinute / 10;
int minuteDigit2 = currentMinute % 10;
// Display the hour and minute on the digits
digitalWrite(digitPins[0], HIGH);
displayDigit(hourDigit1);
delay(5);
digitalWrite(digitPins[1], HIGH);
displayDigit(hourDigit2);
delay(5);
digitalWrite(digitPins[2], HIGH);
displayDigit(minuteDigit1);
delay(5);
digitalWrite(digitPins[3], HIGH);
displayDigit(minuteDigit2);
// Display the time on the serial monitor
Serial.print("Time: ");
if (currentHour < 10) {
Serial.print("0");
}
Serial.print(currentHour);
Serial.print(":");
if (currentMinute < 10) {
Serial.print("0");
}
Serial.println(currentMinute);
}
// Function to display a digit on the 7-segment display
void displayDigit(int digit) {
// Define the segment patterns for each digit
const byte digitPatterns[] = {
B11111100, // Digit 0
B01100000, // Digit 1
B11011010, // Digit 2
B11110010, // Digit 3
B01100110, // Digit 4
B10110110, // Digit 5
B10111110, // Digit 6
B11100000, // Digit 7
B11111110, // Digit 8
B11110110 // Digit 9
};
// Display the digit by setting the appropriate segments
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], bitRead(digitPatterns[digit], i));
}
}
void setup() {
// Initialize the RTC module
Wire.begin();
rtc.begin();
// Set the initial time to 12:00:00
rtc.adjust(DateTime(2024, 4, 6, 12, 0, 0));
// Set the button pins as inputs with internal pull-ups
pinMode(BUTTON_HOUR_PIN, INPUT_PULLUP);
pinMode(BUTTON_MINUTE_PIN, INPUT_PULLUP);
// Set the digit pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}
// Set the segment pins as outputs
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Start the serial monitor
Serial.begin(9600);
}
void loop() {
// Read the current time from the RTC module
DateTime now = rtc.now();
currentHour = now.hour();
currentMinute = now.minute();
currentSecond = now.second();
// Update the display
updateDisplay();
}