// V.1.1
#include <SPI.h>
#include <TFT_eSPI.h>
#include <Adafruit_SHT4x.h>
// encoder pinout
#define CLK 16
#define DT 17
#define SW 13
// led pinout
#define ledR 27
#define ledG 14
#define ledB 12
// tft pinout
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
// library specific
TFT_eSPI tft = TFT_eSPI();
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
// user specific default values
String mode = "Cool"; // Modes - Cool, Heat, Off
String unit = "C"; // Units - C, F
// variables
bool lastClk = 1;
byte setTemp = 0;
int count = 0;
byte menu = 3;
unsigned long timeNow = -1000;
int tempC, tempF;
void setup() {
Serial.begin(115200);
// init TFT display
tft.begin();
tft.setRotation(0);
//tft.invertDisplay(1);
tft.fillScreen(TFT_BLACK);
// int SHT sensor
shtSetup();
// encoder pin definitions
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
// led pin definitions
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
// show default settemp
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(80, 220, 7);
if (unit == "C") {
setTemp = 25;
tft.print(setTemp);
tft.setTextFont(4);
tft.print("`C");
}
if (unit == "F") {
setTemp = 77;
tft.print(setTemp);
tft.setTextFont(4);
tft.print("`F");
}
tft.fillScreen(TFT_BLACK);
}
void loop() {
// when button pressed
if (digitalRead(SW) == 0) {
menu += 1;
if (menu > 3) menu = 0;
tft.fillScreen(TFT_BLACK);
delay(250);
count = 0;
if (menu == 0) {
tft.setCursor(80, 220, 7);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.print(setTemp);
tft.setTextFont(4);
if (unit == "C") {
tft.print("`C");
}
if (unit == "F") {
tft.print("`F");
}
}
}
// main screen
if (menu == 0) {
// update every seconds
if (millis() - timeNow > 1000) {
timeNow = millis();
// read current temperature
sensors_event_t humidity, temp;
sht4.getEvent(&humidity, &temp);
tempC = random(25, 30);
//tempC = temp.temperature;
tempF = (tempC * 9.0 / 5.0) + 32.0;
//Serial.println(tempC);
// display mode
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(20, 20, 4);
tft.print(mode);
// display current temperature
tft.setCursor(80, 100, 7);
if (unit == "C") {
tft.print(tempC);
tft.setTextFont(4);
tft.print("`C");
}
if (unit == "F") {
tft.print(tempF);
tft.setTextFont(4);
tft.print("`F");
}
// set led colors
if (tempC > setTemp) led(1);
if (tempC == setTemp) led(2);
if (tempC < setTemp) led(3);
}
bool newClk = digitalRead(CLK);
if (newClk != lastClk) {
lastClk = newClk;
bool dtValue = digitalRead(DT);
if (newClk == 0 && dtValue == 1) {
setTemp++;
}
if (newClk == 0 && dtValue == 0) {
setTemp--;
}
//Serial.println(setTemp);
// display set temperature
tft.setCursor(80, 220, 7);
tft.print(setTemp);
tft.setTextFont(4);
if (unit == "C") {
tft.print("`C");
}
if (unit == "F") {
tft.print("`F");
}
}
}
// Menu 1 - Select Mode
if (menu == 1) {
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(40, 20, 4);
tft.print("Mode");
tft.drawFastHLine(30, 50, 80, TFT_WHITE);
tft.setCursor(40, 80, 4);
tft.print("HEAT");
tft.setCursor(40, 120, 4);
tft.print("COOL");
tft.setCursor(40, 160, 4);
tft.print("OFF");
// rotate the encoder to toggle modes
bool newClk = digitalRead(CLK);
if (newClk != lastClk) {
lastClk = newClk;
bool dtValue = digitalRead(DT);
if (newClk == 0 && dtValue == 1) {
count++;
if (count > 3) count = 1;
}
if (newClk == 0 && dtValue == 0) {
count--;
if (count < 1) count = 3;
}
//Serial.println(abs(count));
tft.drawRect(30, 70, 85, 40, TFT_BLACK);
tft.drawRect(30, 110, 85, 40, TFT_BLACK);
tft.drawRect(30, 150, 85, 40, TFT_BLACK);
switch (abs(count)) {
case 1:
tft.drawRect(30, 70, 85, 40, TFT_WHITE);
break;
case 2:
tft.drawRect(30, 110, 85, 40, TFT_WHITE);
break;
case 3:
tft.drawRect(30, 150, 85, 40, TFT_WHITE);
break;
}
}
// save the mode when the button is pressed
if (digitalRead(SW) == 0) {
switch (abs(count)) {
case 1:
mode = "Heat";
break;
case 2:
mode = "Cool";
break;
case 3:
mode = "Off";
break;
}
if(count){
menu = 0;
tft.fillScreen(TFT_BLACK);
delay(250);
count = 0;
if (menu == 0) {
tft.setCursor(80, 220, 7);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.print(setTemp);
tft.setTextFont(4);
if (unit == "C") {
tft.print("`C");
}
if (unit == "F") {
tft.print("`F");
}
}
}
}
}
// menu 2 - Select Unit
if (menu == 2) {
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(40, 20, 4);
tft.print("Unit");
tft.drawFastHLine(30, 50, 60, TFT_WHITE);
tft.setCursor(50, 80, 4);
tft.print("`C");
tft.setCursor(50, 120, 4);
tft.print("`F");
// rotate the encoder to toggle the units
bool newClk = digitalRead(CLK);
if (newClk != lastClk) {
lastClk = newClk;
bool dtValue = digitalRead(DT);
if (newClk == 0 && dtValue == 1) {
count++;
if (count > 2) count = 1;
}
if (newClk == 0 && dtValue == 0) {
count--;
if (count < 1) count = 2;
}
//Serial.println(abs(count));
tft.drawRect(30, 70, 85, 40, TFT_BLACK);
tft.drawRect(30, 110, 85, 40, TFT_BLACK);
switch (abs(count)) {
case 0:
tft.drawRect(30, 70, 85, 40, TFT_WHITE);
break;
case 1:
tft.drawRect(30, 110, 85, 40, TFT_WHITE);
break;
}
}
// press the switch to save the unit
if (digitalRead(SW) == 0) {
switch (abs(count)) {
case 0:
unit = "C";
setTemp = 25;
break;
case 1:
unit = "F";
setTemp = 77;
break;
}
if(count){
menu = 0;
tft.fillScreen(TFT_BLACK);
delay(250);
count = 0;
if (menu == 0) {
tft.setCursor(80, 220, 7);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.print(setTemp);
tft.setTextFont(4);
if (unit == "C") {
tft.print("`C");
}
if (unit == "F") {
tft.print("`F");
}
}
}
}
}
// Menu 3 - Devices
if (menu == 3) {
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(40, 20, 4);
tft.print("Devices");
tft.drawFastHLine(30, 50, 100, TFT_WHITE);
for (int i = 0; i < 4; i++) {
tft.setCursor(40, 80 + i * 40);
tft.print("[");
tft.print(i + 1);
tft.print("] ");
tft.print("Device");
tft.print(i + 1);
tft.setCursor(15, 80 + i * 40);
tft.print("+");
}
bool newClk = digitalRead(CLK);
if (newClk != lastClk) {
lastClk = newClk;
bool dtValue = digitalRead(DT);
if (newClk == 0 && dtValue == 1) {
count++;
if (count > 4) count = 1;
}
if (newClk == 0 && dtValue == 0) {
count--;
if (count < 1) count = 4;
}
Serial.println(count);
tft.drawRect(30, 70, 160, 40, TFT_BLACK);
tft.drawRect(30, 110, 160, 40, TFT_BLACK);
tft.drawRect(30, 150, 160, 40, TFT_BLACK);
tft.drawRect(30, 190, 160, 40, TFT_BLACK);
switch (abs(count)) {
case 1:
tft.drawRect(30, 70, 160, 40, TFT_WHITE);
break;
case 2:
tft.drawRect(30, 110, 160, 40, TFT_WHITE);
break;
case 3:
tft.drawRect(30, 150, 160, 40, TFT_WHITE);
break;
case 4:
tft.drawRect(30, 190, 160, 40, TFT_WHITE);
break;
}
}
}
}
void led(int num) {
digitalWrite(ledR, 0);
digitalWrite(ledG, 0);
digitalWrite(ledB, 0);
if (num == 1) digitalWrite(ledR, 1);
if (num == 2) digitalWrite(ledG, 1);
if (num == 3) digitalWrite(ledB, 1);
}
void shtSetup() {
if (!sht4.begin()) {
Serial.println("Couldn't find SHT4x");
//while (1) delay(1);
}
Serial.println("Found SHT4x sensor");
Serial.print("Serial number 0x");
Serial.println(sht4.readSerial(), HEX);
// You can have 3 different precisions, higher precision takes longer
sht4.setPrecision(SHT4X_HIGH_PRECISION);
switch (sht4.getPrecision()) {
case SHT4X_HIGH_PRECISION:
Serial.println("High precision");
break;
case SHT4X_MED_PRECISION:
Serial.println("Med precision");
break;
case SHT4X_LOW_PRECISION:
Serial.println("Low precision");
break;
}
// You can have 6 different heater settings
// higher heat and longer times uses more power
// and reads will take longer too!
sht4.setHeater(SHT4X_NO_HEATER);
switch (sht4.getHeater()) {
case SHT4X_NO_HEATER:
Serial.println("No heater");
break;
case SHT4X_HIGH_HEATER_1S:
Serial.println("High heat for 1 second");
break;
case SHT4X_HIGH_HEATER_100MS:
Serial.println("High heat for 0.1 second");
break;
case SHT4X_MED_HEATER_1S:
Serial.println("Medium heat for 1 second");
break;
case SHT4X_MED_HEATER_100MS:
Serial.println("Medium heat for 0.1 second");
break;
case SHT4X_LOW_HEATER_1S:
Serial.println("Low heat for 1 second");
break;
case SHT4X_LOW_HEATER_100MS:
Serial.println("Low heat for 0.1 second");
break;
}
}