#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <DHT.h>
// rtc
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
// dht
#define DHTPIN 7
#define DHTTYPE DHT22
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
DHT dht(DHTPIN, DHTTYPE);
float lastTemp = 1.0;
float temp = dht.readTemperature();
// timers for non-blocking tasks
unsigned long previousTempMillis = 0;
unsigned long previousClockMillis = 0;
// update intervals
const long tempInterval = 200;
const long clockInterval = 1000;
void setup() {
dht.begin();
tft.begin();
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
// Set the time to a default value (e.g., 00:00:00)
rtc.adjust(DateTime(0, 1, 1, 0, 0, 0)); // Year, Month, Day, Hour, Minute, Second
}
}
void loop() {
unsigned long currentMillis = millis();
// Temp sprememba
if (currentMillis - previousTempMillis >= tempInterval) {
previousTempMillis = currentMillis;
temp = dht.readTemperature();
if (abs(temp - lastTemp) >= 0.5) {
tft.fillRect(10, 10, 50, 30, ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println(temp, 1);
tft.setCursor(60, 10);
tft.setTextColor(ILI9341_GREEN);
tft.print(" ");
tft.print((char)247);
tft.print("C");
lastTemp = temp;
}
// Serial nastavitev časa
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
adjustTime(input);
}
}
// posodobitev časa
if (currentMillis - previousClockMillis >= clockInterval) {
previousClockMillis = currentMillis;
DateTime now = rtc.now();
// Print the current time to Serial
tft.fillRect(0, 50, 300, 130, ILI9341_BLACK);
tft.setCursor(0, 50);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(5);
tft.print(now.hour(), DEC);
tft.print(':');
tft.print(now.minute(), DEC);
tft.print(':');
tft.print(now.second(), DEC);
}
}
void adjustTime(String input) {
// format: HH:MM:SS
if (input.length() == 8) {
int hour = input.substring(0, 2).toInt();
int minute = input.substring(3, 5).toInt();
int second = input.substring(6, 8).toInt();
DateTime now = rtc.now();
rtc.adjust(DateTime(now.year(), now.month(), now.day(), hour, minute, second));
Serial.println("Time updated!");
} else {
Serial.println("Invalid format. Use HH:MM:SS");
}
}
L
R
↑
↓
←
→