#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
RTC_DS3231 rtc;
const int tempPin = 6;
DHT dht(tempPin, DHT22);
void setup() {
Wire.begin();
rtc.begin();
dht.begin();
// Set the pins for 74HC595 ICs
int latchPins[] = {4, 5, 6};
int clockPins[] = {5, 6, 6};
int dataPins[] = {6, 6, 4};
for (int i = 0; i < 3; i++) {
pinMode(latchPins[i], OUTPUT);
pinMode(clockPins[i], OUTPUT);
pinMode(dataPins[i], OUTPUT);
}
// Uncomment the following line if you want to set the time on the RTC
// rtc.adjust(DateTime(__DATE__, __TIME__));
}
void shiftOutData(int latchPin, int dataPin, int clockPin, byte data) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, data);
digitalWrite(latchPin, HIGH);
}
void loop() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
int dayOfWeek = now.dayOfTheWeek();
// Display time in 6 digits on the 2nd 74HC595
shiftOutData(5, 6, 1, hour / 10);
shiftOutData(5, 6, 2, hour % 10);
shiftOutData(5, 6, 3, minute / 10);
shiftOutData(5, 6, 4, minute % 10);
shiftOutData(5, 6, 5, second / 10);
shiftOutData(5, 6, 6, second % 10);
// Display date in 8 digits on the 3rd 74HC595
shiftOutData(6, 4, 7, now.day() / 10);
shiftOutData(6, 4, 8, now.day() % 10);
shiftOutData(6, 4, 9, now.month() / 10);
shiftOutData(6, 4, 10, now.month() % 10);
// Display temperature in 2 digits on the 1st 74HC595
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
int tempInt = int(temperature);
shiftOutData(4, 5, 0, tempInt / 10);
shiftOutData(4, 5, 11, tempInt % 10);
}
// Display day of the week using 7 LEDs
byte days[] = {B11000000, B11111001, B10100100, B10110000, B10011001, B10010010, B10000010};
shiftOutData(6, 4, 12, days[dayOfWeek]);
delay(1000); // Update every second
}