/**
First demo for FT6206 Capactive Touch Screen on Wokwi. Enjoy!
https://wokwi.com/arduino/projects/311598148845830720
*/
/***************************************************
This is our touchscreen painting example for the Adafruit ILI9341
captouch shield
----> http://www.adafruit.com/products/1947
Check out the links above for our tutorials and wiring diagrams
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h> // this is needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
#include <Stepper.h>
#include "DHT.h"
#define TFT_CS 10
#define TFT_DC 9
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#define TS_XDIFF 24
#define TS_YDIFF 32
#define DHTPIN 2 //Оголошуємо пін датчику
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE); //Оголошуємо датчик
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ts = Adafruit_FT6206();
//Adafruit_STMPE610 ts = Adafruit_STMPE610();
float max_t=0; //max 80.0
float min_t=0; //min 0.0
const int stepsPerRevolution = 300;
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
unsigned long previousMillis = 0;
void setup() {
Serial.begin(115200);
dht.begin();
tft.begin();
ts.begin();
tft.setRotation(0);
//ts.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
drawButtons();
myStepper.setSpeed(10);
Serial.println("Capacitive touchscreen started");
}
void drawButtons() {
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
// Draw first button
int16_t button_x = 10;
int16_t button_y = 180;
tft.fillRect(button_x, button_y, 100, 50, ILI9341_BLUE);
tft.drawRect(button_x, button_y, 100, 50, ILI9341_WHITE);
tft.setCursor(button_x + 5, button_y + 15);
tft.print("Temp -");
// Draw second button
button_x = 130;
button_y = 180;
tft.fillRect(button_x, button_y, 100, 50, ILI9341_RED);
tft.drawRect(button_x, button_y, 100, 50, ILI9341_WHITE);
tft.setCursor(button_x+5, button_y+15);
tft.print("Temp +");
button_x = 130;
button_y = 260;
tft.fillRect(button_x, button_y, 100, 50, ILI9341_RED);
tft.drawRect(button_x, button_y, 100, 50, ILI9341_WHITE);
tft.setCursor(button_x+5, button_y+15);
tft.print("Temp +");
button_x = 10;
button_y = 260;
tft.fillRect(button_x, button_y, 100, 50, ILI9341_BLUE);
tft.drawRect(button_x, button_y, 100, 50, ILI9341_WHITE);
tft.setCursor(button_x+5, button_y+15);
tft.print("Temp -");
tft.setCursor(10, 161);
tft.print("Minimum temperature");
tft.setCursor(10, 240);
tft.print("Maximum temperature");
tft.setCursor(50, 20);
tft.print("Temperature");
String max_text = "max="+String(max_t)+"C";
String min_text = "min="+String(min_t)+"C";
//Serial.println(max_text);
// tft.fillRect(10, 50, 100, 100, ILI9341_BLACK);
tft.setCursor(10, 50);
tft.print(max_text);
tft.setCursor(10, 70);
tft.print(min_text);
}
void loop() {
unsigned long currentMillis = millis();
float temperature;
//проверяем не прошел ли нужный интервал, если прошел то
if(currentMillis - previousMillis > 70) {
// сохраняем время последнего переключения
previousMillis = currentMillis;
temperature = dht.readTemperature();
TS_Point p = ts.getPoint();
int key=getPressedKey(p);
//tft.setCursor(10, 30);
if(key==1){
min_t-=5;
if(min_t<-40)
{
min_t=-40;
}
}
if(key==2){
min_t+=5;
if(min_t>80)
{
min_t=80;
}
}
if(key==3){
max_t-=5;
if(max_t<-40)
{
max_t=-40;
}
}
if(key==4){
max_t+=5;
if(max_t>80)
{
max_t=80;
}
}
// text+=key;
if(key!=0)
{
String max_text = "max="+String(max_t)+"C";
String min_text = "min="+String(min_t)+"C";
//Serial.println(max_text);
tft.fillRect(10, 50, 100, 100, ILI9341_BLACK);
tft.setCursor(10, 50);
tft.print(max_text);
tft.setCursor(10, 70);
tft.print(min_text);
}
}
if(temperature>min_t && temperature<max_t)
{
unsigned long currentTime; // Переменная текущего времени
if (millis() - currentTime > 1000) // Если время контроллера millis, больше переменной на 1000, то запускаем условие if
{
currentTime = millis(); // Приравниваем переменную текущего времени к времени контроллера, чтобы через 1000мс опять сработал наш цикл.
myStepper.step(1);
}
}
}
int getPressedKey(TS_Point p) {
int key = 0;
// Serial.println(p.x);
//if (p.z > 0)
int16_t screen_x = p.x;
int16_t screen_y = p.y;
//max +
if(screen_x>=130&&screen_x<=230&&screen_y>=92&&screen_y<=142)
{
key=1;
}
//max -
if(screen_x>=10&&screen_x<=110&&screen_y>=92&&screen_y<=142)
{
key=2;
}
//min +
if(screen_x>=130&&screen_x<=230&&screen_y>=10&&screen_y<=60)
{
key=3;
}
//min -
if(screen_x>=10&&screen_x<=110&&screen_y>=10&&screen_y<=60)
{
key=4;
}
if(key!=0){
}
return key;
}