#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <RTClib.h>
#include <DHT.h>
#include "Font7seg.h"
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 6
#define MAX_DEVICE_S 4
#define CS_PIN_UTC 10
#define CS_PIN_LOCAL 9
#define CS_PIN_WX 8
#define BUTTON 2
#define LED_ONE 5
#define LED_TWO 4
#define DHT_PIN 7
#define DHT_TYPE DHT22
#define TIME_DHT 1000
DHT dht(DHT_PIN, DHT_TYPE);
MD_Parola led_utc = MD_Parola(HARDWARE_TYPE, CS_PIN_UTC, MAX_DEVICES);
MD_Parola led_local = MD_Parola(HARDWARE_TYPE, CS_PIN_LOCAL, MAX_DEVICES);
MD_Parola led_wx = MD_Parola(HARDWARE_TYPE, CS_PIN_WX, MAX_DEVICE_S);
RTC_DS1307 rtc;
uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 };
volatile int mode = 1;
volatile int debounce = 0;
int temp = 188;
int rh = 188;
int timerDHT = TIME_DHT;
uint8_t h = 0;
uint8_t m = 0;
uint8_t s = 0;
uint16_t day = 0;
uint16_t month = 0;
uint16_t year = 2000;
String colon = "";
String utc_str = "";
String min_str = "";
String sec_str = "";
String hour_str = "";
void setup() {
led_utc.begin();
led_utc.setIntensity(0);
led_utc.displayClear();
led_utc.setTextAlignment(PA_CENTER);
led_local.begin();
led_local.setIntensity(0);
led_local.displayClear();
led_local.setTextAlignment(PA_CENTER);
led_wx.begin();
led_wx.addChar('$', degC);
led_wx.setIntensity(0);
led_wx.displayClear();
led_wx.setTextAlignment(PA_CENTER);
if(!rtc.begin()) {
led_utc.print("RTC");
led_local.print("ERROR!");
for(;;);
}
pinMode(BUTTON, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON), setMode, FALLING);
pinMode(LED_ONE, OUTPUT);
pinMode(LED_TWO, OUTPUT);
digitalWrite(LED_ONE, HIGH);
digitalWrite(LED_TWO, HIGH);
dht.begin();
}
void setMode() {
if((millis() - debounce) > 500) {
debounce = millis();
mode++;
}
}
void dhtRead() {
if ((millis() - timerDHT) > TIME_DHT) {
timerDHT = millis();
temp = dht.readTemperature();
rh = dht.readHumidity();
}
}
void getTime() {
DateTime now = rtc.now();
h = now.hour();
m = now.minute();
s = now.second();
day = now.day();
month = now.month();
year = now.year();
colon = (now.second() % 2 == 0) ? ":" : " ";
int utc_hour = (h - 7) % 24;
utc_str = (utc_hour < 10 ? "0" : "") + String(utc_hour, DEC);
min_str = (m < 10 ? "0" : "") + String(m, DEC);
sec_str = (s < 10 ? "0" : "") + String(s, DEC);
hour_str = (h < 10 ? "0" : "") + String(h, DEC);
}
void utcTime() {
// Display UTC time (Local time - 7; for UTC+7 country e.g., Indonesia)
led_utc.setFont(0, numeric7Seg);
led_utc.print(utc_str + colon + min_str + colon + sec_str + "Z");
}
void localTime() {
// Display local time
led_local.setFont(0, numeric7Seg);
led_local.print(hour_str + colon + min_str + colon + sec_str);
}
void getDate() {
// Display Temperature and Relative humidity from DHT22/DHT11
led_wx.setFont(nullptr);
if (s / 4 % 2 == 0) {
if (s / 2 % 2 == 0) {
led_wx.print("T:" + String(temp) + "$");
} else {
led_wx.print("H:" + String(rh) + "%");
}
} else { // Display Date
if (s / 2 % 2 == 0) {
led_wx.print(String(day) + "/" + String(month));
} else {
led_wx.print(year);
}
}
}
void displayText() {
led_local.setFont(0, nullptr);
led_local.print("By: Arkan");
}
void altTime() {
led_wx.setFont(0, numeric7Seg);
led_wx.print(hour_str + colon + min_str);
}
void ledOne() {
digitalWrite(LED_ONE, HIGH);
digitalWrite(LED_TWO, LOW);
}
void ledTwo() {
digitalWrite(LED_ONE, LOW);
digitalWrite(LED_TWO, HIGH);
}
void ledThree() {
digitalWrite(LED_ONE, HIGH);
digitalWrite(LED_TWO, HIGH);
}
void loop() {
switch (mode) {
case 1:
ledOne();
getTime();
utcTime();
localTime();
dhtRead();
getDate();
break;
case 2:
ledTwo();
getTime();
utcTime();
displayText();
dhtRead();
getDate();
break;
case 3:
ledThree();
getTime();
utcTime();
displayText();
altTime();
break;
default:
mode = 1;
break;
}
delay(15);
}