#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();
const int maxtemp = 80;
// timer
unsigned long previousTempMillis = 0;
unsigned long previousClockMillis = 0;
// update intervals
const long tempInterval = 200;
const long clockInterval = 1000;
bool defbar = false;
int menu = 0;
int hms = 0;
int lasthms = 0;
int lasth = 0;
int lastm = 0;
int lasts = 0;
int schHour = 0;
int schMinute = 0;
int schSecond = 0;
int lasths = 0;
int lastms = 0;
int lastss = 0;
int runtime = 0
//temp
int runtime = 60
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;
}
}
void setDisplayTime(int hour, int minute, int second, int hms) {
tft.fillRect(0, 40, 300, 69, ILI9341_BLACK);
if (hms == 0) {
tft.fillRect(0, 40, 60, 60, ILI9341_GREEN);
}
else if (hms == 1) {
tft.fillRect(88, 40, 60, 60, ILI9341_GREEN);
}
else if (hms == 2) {
tft.fillRect(177, 40, 60, 60, ILI9341_GREEN);
}
tft.setCursor(0, 50);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(5);
if (hour < 10){tft.print("0" + String(hour));} else {tft.print(hour, DEC);}
tft.print(':');
if (minute < 10){tft.print("0" + String(minute));} else {tft.print(minute, DEC);}
tft.print(':');
if (second < 10){tft.print("0" + String(second));} else {tft.print(second, DEC);}
}
void setDisplaySchedule(int hour, int minute, int second, int hms) {
tft.fillRect(0, 160, 300, 69, ILI9341_BLACK);
if (hms == 0) {
tft.fillRect(0, 160, 60, 60, ILI9341_GREEN);
}
else if (hms == 1) {
tft.fillRect(88, 160, 60, 60, ILI9341_GREEN);
}
else if (hms == 2) {
tft.fillRect(177, 160, 60, 60, ILI9341_GREEN);
}
tft.setCursor(0, 170);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(5);
if (hour < 10){tft.print("0" + String(hour));} else {tft.print(hour, DEC);}
tft.print(':');
if (minute < 10){tft.print("0" + String(minute));} else {tft.print(minute, DEC);}
tft.print(':');
if (second < 10){tft.print("0" + String(second));} else {tft.print(second, DEC);}
}
void setup() {
pinMode(LB, INPUT_PULLUP);
pinMode(RB, INPUT_PULLUP);
pinMode(U, INPUT_PULLUP);
pinMode(D, 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();
tft.fillRect(0, 109, 300, 40, ILI9341_RED);
tft.setCursor(0, 115);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("SCHEDULED:");
setDisplaySchedule(schHour, schMinute, schSecond, 9);
}
void loop() {
DateTime now = rtc.now();
int setup = digitalRead(LB);
if (setup == LOW) {
//Serial.print("setup");
hour = now.hour();
minute = now.minute();
second = now.second();
delay(80);
defbar = false;
hms = 0; //nastavitev časa h/m/s
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");
setDisplayTime(hour, minute, second, hms);
while (digitalRead(LB) == HIGH) {
//Serial.print("inside");
if (menu == 0) {
delay(80);
lasthms = hms;
lasth = hour;
lastm = minute;
lasts = second;
if (digitalRead(RB) == LOW) {
if (hms == 2) {
hms = 0;
}
else {
hms = hms + 1;
}
Serial.print(hms);
}
if (digitalRead(R) == LOW) {
Serial.print("+");
if (hms == 0) {
if (hour == 23) {hour = 0;} else {hour = hour + 1;}
}
if (hms == 1) {
Serial.print("(1+)");
if (minute == 59) {minute = 0;} else {minute = minute + 1;}
}
if (hms == 2) {
if (second == 59) {second = 0;} else {second = second + 1;}
}
}
if (digitalRead(L) == LOW) {
if (hms == 0) {
if (hour == 0) {hour = 23;} else {hour = hour - 1;}
}
if (hms == 1) {
if (minute == 0) {minute = 59;} else {minute = minute - 1;}
}
if (hms == 2) {
if (second == 0) {second = 59;} else {second = second - 1;}
}}
if (hms != lasthms || hour != lasth || minute != lastm || second != lasts) {
setDisplayTime(hour, minute, second, hms);
}
if (digitalRead(D) == LOW) {menu = 1;}
else if (digitalRead(U) == LOW) {menu = 3;}
}
else if (menu == 1) {
setDisplayTime(schHour, schMinute, schSecond, hms);
delay(80);
lasthms = hms;
lasth = schHour;
lastm = schMinute;
lasts = schSecond;
if (digitalRead(RB) == LOW) {
if (hms == 2) {
hms = 0;
}
else {
hms = hms + 1;
}
Serial.print(hms);
}
if (digitalRead(R) == LOW) {
Serial.print("+");
if (hms == 0) {
if (schHour == 23) {schHour = 0;} else {schHour = schHour + 1;}
}
if (hms == 1) {
Serial.print("(1+)");
if (schMinute == 59) {schMinute = 0;} else {schMinute = schMinute + 1;}
}
if (hms == 2) {
if (schSecond == 59) {schSecond = 0;} else {schSecond = schSecond + 1;}
}
}
if (digitalRead(L) == LOW) {
if (hms == 0) {
if (schHour == 0) {schHour = 23;} else {schHour = schHour - 1;}
}
if (hms == 1) {
if (schMinute == 0) {schMinute = 59;} else {schMinute = schMinute - 1;}
}
if (hms == 2) {
if (schSecond == 0) {schSecond = 59;} else {schSecond = schSecond - 1;}
}}
if (hms != lasthms || schHour != lasth || schMinute != lastm || schSecond != lasts) {
setDisplayTime(schHour, schMinute, schSecond, hms);
}
if (digitalRead(D) == LOW) {menu = 2;}
else if (digitalRead(U) == LOW) {menu = 1;}
}
}
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, 40, 300, 69, 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);
}
}
}L
R
↑
↓
-
+