#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <DHT.h>
// rtc
#include <Wire.h>
#include <RTClib.h>
//tipke
#define LB 6
#define RB 5
#define U 4
#define L 3
#define R 2
#define D 1
RTC_DS3231 rtc;
int hour, minute, second;
// 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();
// timer
unsigned long previousTempMillis = 0;
unsigned long previousClockMillis = 0;
// update intervals
const long tempInterval = 200;
const long clockInterval = 1000;
bool defbar = false;
int hms = 0;
void setup() {
pinMode(LB, INPUT_PULLUP);
pinMode(RB, INPUT_PULLUP);
pinMode(U, INPUT_PULLUP);
pinMode(L, INPUT_PULLUP);
pinMode(R, INPUT_PULLUP);
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
}
resetBar();
}
void loop() {
int setup = digitalRead(LB);
if (setup == LOW)
{
//Serial.print("setup");
delay(80);
defbar = false;
hms = 0; //nastavitev časa h/m/s
DateTime now = rtc.now();
hour = now.hour();
minute = now.minute();
second = now.second();
tft.fillRect(0, 280, 240, 40, ILI9341_RED);
tft.setCursor(0, 286);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("SAVE");
tft.setCursor(125, 286);
tft.print("H/M/S");
while (digitalRead(LB) == HIGH) {
//Serial.print("inside");
delay(80);
if (digitalRead(RB) == LOW) {
if (hms == 2) {
hms = 0;
}
else {
hms = hms + 1;
}
Serial.print(hms);
}
if (digitalRead(R) == LOW) {
if (hms == 0) {
hour = hour + 1;
}
if (hms == 1) {
minute = minute + 1;
}
if (hms == 2) {
second = second + 1;
}
rtc.adjust(DateTime(now.year(), now.month(), now.day(), hour, minute, second));
}
}
}
resetBar();
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();
// izpis casa
tft.fillRect(0, 50, 300, 98, 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");
}
}
void resetBar() {
if (!defbar){
tft.fillRect(0, 280, 240, 40, ILI9341_RED);
tft.setCursor(0, 286);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("SET");
defbar = true;
}
}
L
R
↑
↓
-
+