#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 = 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 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 cookTime = 0;
int cooktemp = 40;
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");
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);}
}
bool infinicook() {
tft.setCursor(70, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.print("-");
while (digitalRead(RB) == HIGH){
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);
}
}
resetBar();
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_PINK);
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);
resetBar();
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();}
}
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
}
resetBar();
tft.fillRect(0, 109, 300, 40, ILI9341_RED);
tft.setCursor(0, 115);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("SCHEDULED:");
}
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();
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;
}
}
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;}
}
}
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) {
if (hms != lasthms || hour != lasth || minute != lastm || second != lasts) {
setDisplayTime(hour, minute, second, 9);}
//Serial.print("m1");
}}
}
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;
}
// 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);
}
if (schHour != schHourOld || schMinute != schMinuteOld) {
tft.fillRect(0, 170, 300, 69, ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(5);
tft.setCursor(0, 170);
tft.print(schHour);
tft.print(":");
tft.print(schMinute);
schHourOld = schHour;
schMinuteOld = schMinute;
}
if (now.hour() == schHour && now.minute() == schMinute){cook(true);}
}}
L
R
↑
↓
-
+