#include <LedControl.h>
#include <Wire.h>
#include <RTClib.h>
#include "DHT.h"
// Pin Definitions
#define DIN_PIN 12
#define CS_PIN 11
#define CLK_PIN 10
#define DHT_PIN 8
#define DHT_TYPE DHT22
// Initialize the LED matrix (32x8 -> 4 matrices)
LedControl lc = LedControl(DIN_PIN, CLK_PIN, CS_PIN, 1);
// Initialize the DHT sensor
DHT dht(DHT_PIN, DHT_TYPE);
// Initialize the RTC
RTC_DS1307 rtc;
// Circle pattern
byte circle[8] = {
B00000000,
B00000000,
B00000000,
B01111110,
B10000001,
B10000001,
B01111110,
B00000000
};
// Line pattern
byte line[8] = {
B00000000,
B00000000,
B00000000,
B00000000,
B11111111,
B11111111,
B00000000,
B00000000
};
void setup() {
Serial.begin(9600);
// Initialize LED matrices
for (int i = 0; i < 4; i++) {
lc.shutdown(i, false); // Wake up displays
lc.setIntensity(i, 32); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(i); // Clear display register
}
// Initialize DHT22
dht.begin();
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// Use DATE and TIME directly without F() macro
rtc.adjust(DateTime(__DATE__, __TIME__)); // Set RTC to the date & time this sketch was compiled
}
}
void loop() {
// Get the temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Get the current time
DateTime now = rtc.now();
// Clear all matrices
for (int i = 0; i < 4; i++) {
lc.clearDisplay(i);
}
// Display the time on the first two matrices
displayTime(now.hour(), now.minute());
// Display temperature on the third matrix
displayNumber(temperature, 2);
// Display humidity on the fourth matrix
displayNumber(humidity, 3);
delay(1000); // Update every second
// Display the circle pattern
displayPattern(circle);
delay(1000);
// Display the line pattern
displayPattern(line);
delay(1000);
}
void displayTime(int hour, int minute) {
// Display hours
lc.setDigit(0, 1, hour / 10, false);
lc.setDigit(0, 0, hour % 10, false);
// Display minutes
lc.setDigit(1, 1, minute / 10, false);
lc.setDigit(1, 0, minute % 10, false);
}
void displayNumber(float num, int matrix) {
int intPart = (int)num; // Integer part of the number
lc.setDigit(matrix, 1, intPart / 10, false); // Tens place of integer part
lc.setDigit(matrix, 0, intPart % 10, false); // Units place without decimal point
}
void displayPattern(byte pattern[8]) {
for (int i = 0; i < 8; i++) {
lc.setRow(0, i, pattern[i]); // Display pattern on the first matrix
lc.setRow(1, i, pattern[i]); // Display pattern on the second matrix
lc.setRow(2, i, pattern[i]); // Display pattern on the third matrix
lc.setRow(3, i, pattern[i]); // Display pattern on the fourth matrix
}
}