#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
#define heat 8
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 = 999;
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 menu = 0;
int hms = 0;
int lasthms = 0;
int lasth = 0;
int lastm = 0;
int lasts = 0;
int schHour = 0;
int schMinute = 0;
int schHourOld = 1;
int schMinuteOld = 1;
int lastSetSchHour = 99;
int lastSetSchMinute = 99;
int setCookHour = 0;
int setCookMinute = 0;
int lastCookHour = -1;
int lastCookMinute = -1;
int cookTime = 0;
int cookTemp = 40;
int cookTimeOld = 1;
int cookTempOld = 1;
int lastMenu = 9;
void resetBar() {
if (!defbar){
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0, 280, 240, 40, ILI9341_RED);
tft.setCursor(0, 286);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("SET");
tft.setCursor(145, 286);
tft.print("START");
tft.fillRect(0, 96, 300, 40, ILI9341_RED);
tft.setCursor(0, 98);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("SCHEDULED:");
cookTempOld = 999;
defbar = true;
lastTemp = 999;
}
}
void setDisplayTime(int hour, int minute, int second, int hms) {
tft.fillRect(0, 40, 300, 49, ILI9341_BLACK);
if (hms == 0) {
tft.fillRect(0, 40, 60, 49, ILI9341_GREEN);
}
else if (hms == 1) {
tft.fillRect(88, 40, 60, 49, ILI9341_GREEN);
}
else if (hms == 2) {
tft.fillRect(177, 40, 60, 49, 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 setDisplaySch(int hour, int minute, int hms) {
tft.fillRect(0, 140, 300, 49, ILI9341_BLACK);
if (hms == 3) {
tft.fillRect(0, 140, 60, 49, ILI9341_GREEN);
}
else if (hms == 4) {
tft.fillRect(88, 140, 60, 49, ILI9341_GREEN);
}
// else if (hms == 5) {
// tft.fillRect(177, 140, 60, 49, ILI9341_GREEN);
// }
tft.setCursor(0, 150);
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("X");}
tft.print("X");
}
void setDisplayCookTime(int hour, int minute, int hms) {
tft.fillRect(0, 185, 300, 49, ILI9341_BLACK);
if (hms == 5) {
tft.fillRect(0, 185, 60, 49, ILI9341_GREEN);
}
else if (hms == 6) {
tft.fillRect(88, 185, 60, 49, ILI9341_GREEN);
}
// else if (hms == 5) {
// tft.fillRect(177, 140, 60, 49, ILI9341_GREEN);
// }
tft.setCursor(0, 195);
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);}
}
bool infinicook() {
tft.setCursor(70, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.print("-");
while (digitalRead(RB) == HIGH){
defbar = false;
tft.fillRect(40, 80, 90, 40, ILI9341_BLACK);
tft.setCursor(70, 80);
tft.print(cookTemp);
if (digitalRead(R) == LOW) {
if (cookTemp == 80){cookTemp = 40;} else {cookTemp = cookTemp + 1;}
}
if (digitalRead(L) == LOW) {
if (cookTemp == 40){cookTemp = 80;} else {cookTemp = cookTemp - 1;}
}
if (digitalRead(U) == LOW || digitalRead(D) == LOW){
//cookTime = cookTime + 60000;
cookTime = 1;
return {true};
}
if (dht.readTemperature() <= cookTemp) {
digitalWrite(heat, HIGH);
}
else if (dht.readTemperature() > cookTemp) {
digitalWrite(heat, LOW);
}
}
return{false};
}
void timedCook() {
delay(80);
unsigned long start = millis();
unsigned long millisCookTime = cookTime*60000;
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
while (digitalRead(RB) == HIGH){
unsigned long now = millis() - start;
tft.setCursor(40, 160);
int leftMinutes = (millisCookTime-now)/60000;
int leftSeconds = ((millisCookTime-now) % 60000)/1000;
tft.fillRect(40, 160, 140, 40, ILI9341_BLACK);
tft.print(leftMinutes);
tft.print(":");
tft.print(leftSeconds);
tft.fillRect(40, 80, 90, 40, ILI9341_BLACK);
tft.setCursor(70, 80);
tft.print(cookTemp);
if (digitalRead(R) == LOW) {
if (cookTemp == 80){cookTemp = 40;} else {cookTemp = cookTemp + 1;}
}
if (digitalRead(L) == LOW) {
if (cookTemp == 40){cookTemp = 80;} else {cookTemp = cookTemp - 1;}
}
if (digitalRead(U) == LOW) {
millisCookTime = millisCookTime + 60000;
// Serial.println(millisCookTime/60000);
}
if (digitalRead(D) == LOW) {
millisCookTime = millisCookTime - 60000;
// Serial.println(millisCookTime/60000);
}
if (dht.readTemperature() <= cookTemp) {
digitalWrite(heat, HIGH);
}
else if (dht.readTemperature() > cookTemp) {
digitalWrite(heat, LOW);
}
if (millis() > start + millisCookTime) {
digitalWrite(heat, LOW);
return;
}
}
}
void cook(bool autostart) {
if (autostart == true && cookTime == 0) {Serial.println(autostart); return;}
else {
delay(80);
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0, 20, 300, 40, ILI9341_RED);
tft.setCursor(0, 25);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("COOKING");
tft.setCursor(0, 80);
tft.setTextSize(5);
tft.print("<");
tft.setCursor(210, 80);
tft.print(">");
tft.setCursor(0, 160);
tft.print("\x1E");
tft.setCursor(210, 160);
tft.print("\x1F");
}
if (cookTime == 0) {
if (infinicook()) {
timedCook();
}
}
else {timedCook();}
defbar = false;
}
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);
pinMode (heat, OUTPUT);
dht.begin();
tft.begin();
if (!rtc.begin()) {
while (1);
}
if (rtc.lostPower()) {
// 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() {
DateTime now = rtc.now();
int setup = digitalRead(LB);
if (digitalRead(RB) == LOW) {
cook(false);
}
if (setup == LOW) {
//Serial.print("setup");
hour = now.hour();
minute = now.minute();
second = now.second();
int setSchHour = schHour;
int setSchMinute = schMinute;
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);
setDisplaySch(schHour, schMinute, 9);
setDisplayCookTime(0, cookTime, 9); /**/
int lastMenu = 9;
while (digitalRead(LB) == HIGH) {
//Serial.print("inside");
if (menu == 0) {
if (lastMenu != 0){
delay(80);
}
lasthms = hms;
lasth = hour;
lastm = minute;
lasts = second;
// setCookHour = cookTime / 60000;
// setCookMinute = cookTime - setCookHour * 60000;
lastSetSchMinute = setSchMinute;
lastSetSchHour = setSchHour;
lastCookHour = setCookHour;
lastCookMinute = setCookMinute;
// setDisplayCookTime(schHour, schMinute, hms);
if (digitalRead(RB) == LOW) {
if (hms == 2) {
hms = 0;
}
else if (hms == 4){
hms = 3;
}
else if (hms == 6){
hms = 5;
}
else if (hms < 7){
hms = hms + 1;
}
}
if (digitalRead(R) == LOW) {
if (hms == 0) {
if (hour == 23) {hour = 0;} else {hour = hour + 1;}
}
if (hms == 1) {
if (minute == 59) {minute = 0;} else {minute = minute + 1;}
}
if (hms == 2) {
if (second == 59) {second = 0;} else {second = second + 1;}
}
else if (hms == 3) {
if (setSchHour == 23) {setSchHour = 0;} else {setSchHour = setSchHour + 1;}
}
else if (hms == 4) {
if (setSchMinute == 59) {setSchMinute = 0;} else {setSchMinute = setSchMinute + 1;}
}
else if (hms == 5) {
if (setCookHour == 23) {setCookHour = 0;} else {setCookHour = setCookHour + 1;}
}
else if (hms == 6) {
if (setCookMinute == 59) {setCookMinute = 0;} else {setCookMinute = setCookMinute + 1;}
}
}
if (digitalRead(L) == LOW) {
if (hms == 0) {
if (hour == 0) {hour = 23;} else {hour = hour - 1;}
}
else if (hms == 1) {
if (minute == 0) {minute = 59;} else {minute = minute - 1;}
}
else if (hms == 2) {
if (second == 0) {second = 59;} else {second = second - 1;}
}
else if (hms == 3) {
if (setSchHour == 0) {setSchHour = 23;} else {setSchHour = setSchHour - 1;}
}
else if (hms == 4) {
if (setSchMinute == 0) {setSchMinute = 59;} else {setSchMinute = setSchMinute - 1;}
}
else if (hms == 5) {
if (setCookHour == 0) {setCookHour = 23;} else {setCookHour = setCookHour - 1;}
}
else if (hms == 6) {
if (setCookMinute == 0) {setCookMinute = 59;} else {setCookMinute = setCookMinute - 1;}
}
}
if (digitalRead(D) == LOW) {
if (hms <= 2){
hms = 3;
}
else if (hms > 2 && hms <= 4) {
hms = 5;
}
else if (hms >= 6) {
hms = 7;
}
}
if (digitalRead(U) == LOW) {
hms = 0;
}
if (hms != lasthms || hour != lasth || minute != lastm || second != lasts || setSchHour != lastSetSchHour || setSchMinute != lastSetSchMinute || lastCookHour != setCookHour || lastCookMinute != setCookMinute) {
setDisplayTime(hour, minute, second, hms);
setDisplaySch(setSchHour, setSchMinute, hms);
setDisplayCookTime(setCookHour, setCookMinute, hms);
}
}
rtc.adjust(DateTime(now.year(), now.month(), now.day(), hour, minute, second));
schHour = setSchHour;
schMinute = setSchMinute;
// cookTime = setCookHour * 60000 + setCookMinute * 1000;
cookTime = setCookHour * 60 + setCookMinute;
}}
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;
}
// posodobitev časa
if (currentMillis - previousClockMillis >= clockInterval) {
previousClockMillis = currentMillis;
DateTime now = rtc.now();
// izpis casa
tft.fillRect(0, 40, 300, 49, 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);
}
if (schHour != schHourOld || schMinute != schMinuteOld || cookTemp != cookTempOld || cookTime != cookTimeOld) {
tft.fillRect(0, 140, 300, 140, ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(5);
tft.setCursor(0, 140);
tft.print(schHour);
tft.print(":");
tft.print(schMinute);
tft.print(":");
tft.print("0");
int cookHour = cookTime/60000;
int cookMinute = cookTime - cookTime/60000;
tft.print("\n");
tft.print(cookHour);
tft.print(":");
tft.print(cookMinute);
tft.print("\n");
tft.print(cookTemp);
tft.print(" ");
tft.setTextColor(ILI9341_GREEN);
tft.print((char)247);
tft.print("C");
schHourOld = schHour;
schMinuteOld = schMinute;
cookTimeOld = cookTime;
cookTempOld = cookTemp;
}
if (now.hour() == schHour && now.minute() == schMinute){cook(true);}
resetBar();
}}
L
R
↑
↓
-
+