#include <LedControl.h>
#include <RTClib.h>
#include <DHT.h>
#define DHTPIN 2 // Pin for the DHT sensor
#define DHTTYPE DHT22 // DHT 11 or DHT22
// MAX7219 Connections
LedControl lcDate = LedControl(3, 5, 4, 4); // Date Module (DIN, CLK, CS)
LedControl lcTime = LedControl(6, 8, 7, 4); // Time Module (DIN, CLK, CS)
LedControl lcYear = LedControl(9, 11, 10, 4); // Year Module (DIN, CLK, CS)
LedControl lcTemp = LedControl(12, A0, 13, 4); // Temperature Module (DIN, CLK, CS)
LedControl lcHum = LedControl(A1, A3, A2, 4); // Humidity Module (DIN, CLK, CS)
DHT dht(DHTPIN, DHTTYPE);
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
dht.begin();
rtc.begin();
// Initialize all MAX7219 modules
initializeModule(lcDate);
initializeModule(lcTime);
initializeModule(lcYear);
initializeModule(lcTemp);
initializeModule(lcHum);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void initializeModule(LedControl lc) {
lc.shutdown(0, false); // Wake up display
lc.setIntensity(0, 8); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register
}
void loop() {
DateTime now = rtc.now();
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
displayDateTime(now);
displayYear(now.year());
displayTemp(temp);
displayHumidity(humidity);
delay(1000);
}
void displayDateTime(DateTime now) {
// Display Date (DDMM)
int dateValue = now.day() * 100 + now.month();
lcDate.setDigit(0, 3, dateValue % 10, false);
lcDate.setDigit(0, 2, (dateValue / 10) % 10, false);
lcDate.setDigit(0, 1, (dateValue / 100) % 10, false);
lcDate.setDigit(0, 0, (dateValue / 1000) % 10, false);
// Display Time (HHMM)
int timeValue = now.hour() * 100 + now.minute();
lcTime.setDigit(0, 3, timeValue % 10, false);
lcTime.setDigit(0, 2, (timeValue / 10) % 10, false);
lcTime.setDigit(0, 1, (timeValue / 100) % 10, false);
lcTime.setDigit(0, 0, (timeValue / 1000) % 10, false);
}
void displayYear(int year) {
// Display Year (YYYY)
lcYear.setDigit(0, 3, year % 10, false);
lcYear.setDigit(0, 2, (year / 10) % 10, false);
lcYear.setDigit(0, 1, (year / 100) % 10, false);
lcYear.setDigit(0, 0, (year / 1000) % 10, false);
}
void displayTemp(float temp) {
// Display Temperature with two decimal places
int tempInt = (int)(temp * 100);
lcTemp.setDigit(0, 3, tempInt % 10, false);
lcTemp.setDigit(0, 2, (tempInt / 10) % 10, true); // Decimal point after the second digit
lcTemp.setDigit(0, 1, (tempInt / 100) % 10, false);
lcTemp.setDigit(0, 0, (tempInt / 1000) % 10, false);
}
void displayHumidity(float humidity) {
// Display Humidity with two decimal places
int humidityInt = (int)(humidity * 100);
lcHum.setDigit(0, 3, humidityInt % 10, false);
lcHum.setDigit(0, 2, (humidityInt / 10) % 10, true); // Decimal point after the second digit
lcHum.setDigit(0, 1, (humidityInt / 100) % 10, false);
lcHum.setDigit(0, 0, (humidityInt / 1000) % 10, false);
}