//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Arduino TFT Touchscreen DHT22 & Control LED
//----------------------------------------Include Library
#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> // Hardware-specific library
#include <TouchScreen.h>
#include <DHT.h>
//----------------------------------------
Adafruit_FT6206 ctp = Adafruit_FT6206();
//----------------------------------------Pin configuration and initialization for LCD TFT
#define LCD_CS 10
#define LCD_CD 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(LCD_CS, LCD_CD);
//----------------------------------------
//----------------------------------------Defines colors
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define LIGHT_BLUE 0x0575
#define RED 0xF800
#define GREY 0x8410
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define DARKORANGE 0xFB60
#define MAROON 0x7800
#define BLACKM 0x18E3
#define PINK 0xFDF9
#define VIOLET 0x781F
#define CREAM 0xEF7D
// Shades of Green
#define GREEN 0x07E0
#define CITRON 0x84A3
#define VERDUN 0x42E2
#define SPROUT 0xBE93
#define SALTPAN 0xE77B
#define ASPARAGUS 0x7CCA
//----------------------------------------
//----------------------------------------Defines an LED pin
#define LED 7 // Red
#define LED1 6 // Green
#define LED2 5 // Blue
//----------------------------------------
//----------------------------------------Button location at point x and y
int BtnGreenX = 40;
int BtnGreenY = 30;
int BtnRedX = 40;
int BtnRedY = 115;
int BtnGreenX1 = 125;
int BtnGreenY1 = 30;
int BtnRedX1 = 125;
int BtnRedY1 = 115;
int BtnGreenX2 = 210;
int BtnGreenY2 = 30;
int BtnRedX2 = 210;
int BtnRedY2 = 115;
//----------------------------------------
//---------------------------------------DHT22 configuration and data variables
#define DHTPIN A3
#define DHTTYPE DHT22
DHT dht22 = DHT(DHTPIN, DHTTYPE);
//----------------------------------------
//---------------------------------------DEFINING THE TRIG AND ECHO ULTRASONIC PIN
#define trigPin 3
#define echoPin 4
int ldrPin = A2;
int buzzerPin = 8;
int h;
float t;
float f;
float hif;
float hic;
int safeState, riskyState, dangerState;
//----------------------------------------
//----------------------------------------LDR Characteristics
int ldrVal = 0; // Value of LDR
const float GAMMA = 0.7;
const float RL10 = 50;
//----------------------------------------
//----------------------------------------ultrasonic output variable:
long duration, distance;//int value
float distance2;//decimals value
//----------------------------------------
//----------------------------------------The x and y points for the Temperature bar
int x_bar_t = 20;
int y_bar_t = 60;
//----------------------------------------
//----------------------------------------The variable to hold the conversion value from the temperature value to the value for the temperature bar
int T_to_Bar;
//----------------------------------------
//----------------------------------------Menu = 0 to display the Main Menu Display, Menu = 1 to control the LED and Menu = 2 to display DHT22 sensor data
int Menu = 0;
//----------------------------------------
//----------------------------------------Variable for the x, y and z points on the touch screen
int x_set_rotatoon_135;
int y_set_rotatoon_135;
//----------------------------------------
//----------------------------------------Millis variable to update the temperature and humidity values
unsigned long previousMillis = 0; //--> will store last time updated
// constants won't change:
const long interval = 2000; //--> interval
//----------------------------------------
//========================================================================VOID SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
tft.begin(); //--> SPFD5408/ILI9341
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
//----------------------------------------Pin Mode Setup
pinMode(buzzerPin, OUTPUT);
pinMode(LED2, OUTPUT), pinMode(LED1, OUTPUT), pinMode(LED, OUTPUT);
pinMode(trigPin, OUTPUT), pinMode(echoPin, INPUT);
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
//----------------------------------------
tft.setRotation(3); //--> Need for the Mega, please changed for your choice or rotation initial
tft.fillScreen(SPROUT); //--> TFT LCD background color
dht22.begin();
ShowPhotoData();
}
//========================================================================
//========================================================================GetTSPoint()
void GetTSPoint() {
// Wait for a touch
if (! ctp.touched()) {
return;
}
// Retrieve a point
TS_Point p = ctp.getPoint();
y_set_rotatoon_135 = map(p.x, 0, 240, 0, tft.height());
x_set_rotatoon_135 = map(tft.width() - p.y, 0, 320, 0, tft.width());
}
//========================================================================
//========================================================================RGB Function Setup
void setRGB(int r, int g, int b) // RGB Function Setup
{
analogWrite(LED, r);
analogWrite(LED1, g);
analogWrite(LED2, b);
}
//========================================================================
//========================================================================SET MSG
void tft_msg(int x, int y, int sz, const GFXfont * f, String msg)
{
int16_t x1, y1;
uint16_t wid, ht;
tft.setFont(f);
tft.setCursor(x, y);
tft.setTextColor(BLACK);
tft.setTextSize(sz);
tft.print(msg);
}
//========================================================================
//========================================================================SET SIGN
void tft_signs(int x, int y, int sz, const GFXfont * f, String msg, uint16_t color)
{
int16_t x1, y1;
uint16_t wid, ht;
tft.setFont(f);
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(sz);
tft.print(msg);
}
//========================================================================
//========================================================================VOID LOOP()
void loop() {
//----------------------------------------Start Display
if (Menu == 0) {
GetTSPoint();
//----------------------------------------Conditions for detecting when the Button for Start is touched and its command (Enter the LED controlling menu)
if (x_set_rotatoon_135 > 97 && x_set_rotatoon_135 < (97+130) && y_set_rotatoon_135 > 85 && y_set_rotatoon_135 < (85+60)) {
Menu = 1;
DrawButtonStartPress();
delay(100);
DrawButtonStart();
delay(100);
tft.fillScreen(SALTPAN);
delay(100);
Menu_display();
}
//----------------------------------------
}
//----------------------------------------
//----------------------------------------Main Menu Display
if (Menu == 1) {
DisplayMainMenu();
}
//----------------------------------------
//----------------------------------------Menu or Mode to control the LED
if (Menu == 2) {
ControlTheLED();
}
//----------------------------------------
//----------------------------------------Menu or Mode to display DHT22 sensor data
if (Menu == 3) {
ShowDHT22Data();
}
//----------------------------------------
//----------------------------------------Menu or Mode to display Ultrasonic data
if (Menu == 4) {
ShowUltraData();
}
//----------------------------------------
//----------------------------------------Menu or Mode to display Photoresistor data
if (Menu == 5) {
ShowPhotoData();
}
//----------------------------------------
//----------------------------------------Menu or Mode to display Noise data
if (Menu == 6) {
ShowNoiseData();
}
//----------------------------------------
//----------------------------------------Menu or Mode to display Game data
if (Menu == 7) {
ShowGameData();
}
//----------------------------------------
}
//========================================================================
//========================================================================DrawButtonStart()
void DrawButtonStart() {
tft.fillRoundRect(97, 85, 130, 60, 10, BLACKM);
tft.fillRoundRect(99, 87, 126, 56, 10, CITRON);
tft.setTextSize(4);
tft.setTextColor(SALTPAN);
tft.setCursor(104, 103);
tft.print("START");
}
//========================================================================
//========================================================================DrawButtonStartPress()
void DrawButtonStartPress() {
tft.fillRoundRect(97, 85, 130, 60, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonControlLED()
void DrawButtonControlLED() {
tft.fillRoundRect(17, 90, 130, 40, 10, BLACKM);
tft.fillRoundRect(19, 92, 126, 36, 10, SPROUT);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.setCursor(22, 103);
tft.print("LED Switch");
}
//========================================================================
//========================================================================DrawButtonControlLEDPress()
void DrawButtonControlLEDPress() {
tft.fillRoundRect(17, 90, 130, 40, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonDistance()
void DrawButtonDistance() {
tft.fillRoundRect(167, 90, 130, 40, 10, BLACKM);
tft.fillRoundRect(169, 92, 126, 36, 10, SPROUT);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.setCursor(185, 103);
tft.print("Distance");
}
//========================================================================
//========================================================================DrawButtonDistancePress()
void DrawButtonDistancePress() {
tft.fillRoundRect(167, 90, 130, 40, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonTempHum()
void DrawButtonTempHum() {
tft.fillRoundRect(17, 140, 130, 40, 10, BLACKM);
tft.fillRoundRect(19, 142, 126, 36, 10, SPROUT);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.setCursor(22, 153);
tft.print("Temp & Hum");
}
//========================================================================
//========================================================================DrawButtonTempHumPress()
void DrawButtonTempHumPress() {
tft.fillRoundRect(17, 140, 130, 40, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonPhoto()
void DrawButtonPhoto() {
tft.fillRoundRect(167, 140, 130, 40, 10, BLACKM);
tft.fillRoundRect(169, 142, 126, 36, 10, SPROUT);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.setCursor(173, 153);
tft.print("Lux Sensor");
}
//========================================================================
//========================================================================DrawButtonPhotoPress()
void DrawButtonPhotoPress() {
tft.fillRoundRect(167, 140, 130, 40, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonNoise()
void DrawButtonNoise() {
tft.fillRoundRect(17, 190, 130, 40, 10, BLACKM);
tft.fillRoundRect(19, 192, 126, 36, 10, SPROUT);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.setCursor(42, 203);
tft.print("Decibel");
}
//========================================================================
//========================================================================DrawButtonNoisePress()
void DrawButtonNoisePress() {
tft.fillRoundRect(17, 190, 130, 40, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonGame()
void DrawButtonGame() {
tft.fillRoundRect(167, 190, 130, 40, 10, BLACKM);
tft.fillRoundRect(169, 192, 126, 36, 10, SPROUT);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.setCursor(210, 203);
tft.print("Game");
}
//========================================================================
//========================================================================DrawButtonGamePress()
void DrawButtonGamePress() {
tft.fillRoundRect(167, 190, 130, 40, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonGreen(x, y) Red
void DrawButtonGreen(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
tft.fillRoundRect(xp+2, yp+2, 50, 50, 10, GREEN);
tft.setCursor(xp+18, yp+22);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("ON");
}
//========================================================================
//========================================================================DrawButtonGreenPress(x, y)
void DrawButtonGreenPress(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonRed(x, y) Red
void DrawButtonRed(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
tft.fillRoundRect(xp+2, yp+2, 50, 50, 10, RED);
tft.setCursor(xp+12, yp+22);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("OFF");
}
//========================================================================
//========================================================================DrawButtonRedPress(x, y)
void DrawButtonRedPress(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonGreen(x, y) Green
void DrawButtonGreen1(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
tft.fillRoundRect(xp+2, yp+2, 50, 50, 10, GREEN);
tft.setCursor(xp+18, yp+22);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("ON");
}
//========================================================================
//========================================================================DrawButtonGreenPress(x, y)
void DrawButtonGreenPress1(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonRed(x, y) Green
void DrawButtonRed1(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
tft.fillRoundRect(xp+2, yp+2, 50, 50, 10, RED);
tft.setCursor(xp+12, yp+22);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("OFF");
}
//========================================================================
//========================================================================DrawButtonRedPress(x, y)
void DrawButtonRedPress1(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonGreen(x, y) Blue
void DrawButtonGreen2(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
tft.fillRoundRect(xp+2, yp+2, 50, 50, 10, GREEN);
tft.setCursor(xp+18, yp+22);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("ON");
}
//========================================================================
//========================================================================DrawButtonGreenPress(x, y)
void DrawButtonGreenPress2(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonRed(x, y) Blue
void DrawButtonRed2(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
tft.fillRoundRect(xp+2, yp+2, 50, 50, 10, RED);
tft.setCursor(xp+12, yp+22);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("OFF");
}
//========================================================================
//========================================================================DrawButtonRedPress(x, y)
void DrawButtonRedPress2(int xp, int yp) {
tft.fillRoundRect(xp, yp, 54, 54, 10, BLACKM);
}
//========================================================================
//========================================================================DrawButtonBack(x, y)
void DrawButtonBack(int x_btn_back, int y_btn_back) {
tft.fillRoundRect(x_btn_back, y_btn_back, 30, 30, 5, BLACKM);
tft.fillRoundRect(x_btn_back+2, y_btn_back+2, 26, 26, 5, YELLOW);
tft.setTextSize(2);
tft.setTextColor(BLACKM);
tft.setCursor(x_btn_back+7, y_btn_back+7);
tft.print("<");
}
//========================================================================
//========================================================================DrawButtonBackPress(x, y)
void DrawButtonBackPress(int x_btn_back, int y_btn_back) {
tft.fillRoundRect(x_btn_back, y_btn_back, 30, 30, 5, BLACKM);
}
//========================================================================
//========================================================================GetDHT22Data()
void GetDHT22Data() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht22.readHumidity();
// Read temperature as Celsius (the default)
t = dht22.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
f = dht22.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
return;
}
// Compute heat index in Fahrenheit (the default)
hif = dht22.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
hic = dht22.computeHeatIndex(t, h, false);
}
//========================================================================
//========================================================================GetUltraData()
void GetUltraData(int safe, int risky, int danger, float ultradis) {
//variable for screen animation setup
int v = 170, v1 = 190, line1 = 230, line2 = 168, line3 = 285, line4 = 128, lineX1 = 250, lineY1 = 223, lineX2 = 250, lineY2 = 223;
float scale = ultradis / 75;
float val_limit = (scale * 150) + 45;
tft.fillRect(10, 150, 200, 85, VERDUN);
tft.fillRect(14, 182, 18, 18, SPROUT);//logo animation
tft.fillRect(32, 170, 10, 42, SPROUT);//logo animation
tft.fillRect(180, 70, 127, 24, SPROUT);// clear number screen
tft.fillRect(96, 126, 110, 16, ASPARAGUS);
tft.fillRect(220, 112, 86, 124, SPROUT);//clear symbol
ultradis = ultradis - 1;
String dis = String(ultradis);
if (safe == 1 && risky == 0 && danger == 0 ) {
for (int x = 0; x <= 5; x++) {
tft.drawLine(45, 180, 200 , v, WHITE);
v += 5;
}
for (int x = 0; x <= 5; x++) {
tft.drawLine(45, 200, 200, v1, WHITE);
v1 += 5;
}
tft_msg(180, 70, 3, NULL, dis);
tft_signs(96, 126, 2, NULL, "SAFE", GREEN);
for (int n = 0; n <= 12; n++)
{
tft.drawLine(line1, line2, lineX1 , lineY1, GREEN);
tft.drawLine(line3, line4, lineX2, lineY2, GREEN);
line1 += 1;
line3 += 1;
lineX1 += 1;
lineX2 += 1;
}
}
else if (safe == 0 && risky == 1 && danger == 0) {
for (int x = 0; x <= 5; x++) {
tft.drawLine(45, 180, val_limit, v, WHITE);
v += 5;
}
for (int x = 0; x <= 5; x++) {
tft.drawLine(45, 200, val_limit, v1, WHITE);
v1 += 5;
}
tft.fillRoundRect(val_limit, 170, 8, 45, 3, SPROUT);
tft_msg(180, 70, 3, NULL, dis);
tft_signs(96, 126, 2, NULL, "CAUTION", YELLOW);
tft.fillRoundRect(245, 118, 35, 75, 30, YELLOW);
tft.fillCircle(260, 215, 14, YELLOW);
}
else if (safe == 0 && risky == 0 && danger == 1)
{
for (int x = 0; x <= 5; x++) {
tft.drawLine(45, 180, val_limit, v, WHITE);
v += 5;
}
for (int x = 0; x <= 5; x++) {
tft.drawLine(45, 200, val_limit, v1, WHITE);
v1 += 5;
}
tft.fillRoundRect(val_limit, 170, 8, 45, 1, SPROUT);
tft_msg(180, 70, 3, NULL, dis);
tft_signs(96, 126, 2, NULL, "DANGEROUS", RED);
tft.fillRoundRect(225, 118, 30, 75, 30, RED);
tft.fillRoundRect(270, 118, 30, 75, 30, RED);
tft.fillCircle(240, 215, 14, RED);
tft.fillCircle(285, 215, 14, RED);
tone(buzzerPin, 950);
}
}
//========================================================================
//========================================================================GetPhotoData()
void GetPhotoData() {
// Read the analog value of the LDR
ldrVal = analogRead(ldrPin);
float voltage = ldrVal / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
int lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// If the LDR value is lower than 200
if (lux < 200) {
digitalWrite(LED, HIGH); // Turn buildin LED on
} else {
digitalWrite(LED, LOW); // Turn buildin LED off
}
}
//========================================================================
//========================================================================draw_bar (Temperature Bar)
void draw_bar(int x_bar, int y_bar) {
tft.fillRoundRect(x_bar, y_bar, 35, 120, 5, BLACK);
tft.fillCircle(x_bar+17, y_bar+140, 30, BLACK);
tft.fillRoundRect(x_bar+4, y_bar+4, 27, 120, 2, WHITE);
tft.fillCircle(x_bar+17, y_bar+140, 25, WHITE);
tft.fillRect(x_bar+8, y_bar+8, 19, 120, DARKORANGE);
tft.fillCircle(x_bar+17, y_bar+140, 21, DARKORANGE);
//tft.fillRect(41, 58, 19, 108, RED);
tft.drawLine(x_bar+37, y_bar+8, x_bar+42, y_bar+8, RED);
tft.setTextSize(1);
tft.setTextColor(RED);
tft.setCursor(x_bar+47, y_bar+4);
tft.println("50");
tft.drawLine(x_bar+37, y_bar+115, x_bar+42, y_bar+115, RED);
tft.setCursor(x_bar+47, y_bar+111);
tft.println("0");
}
//========================================================================
//========================================================================Start_display()
void Start_display() {
tft.setTextSize(2);
tft.setTextColor(VERDUN);
tft.setCursor(16, 170);
tft.print("CREATED BY:");
tft.setTextSize(3);
tft.setTextColor(VERDUN);
tft.setCursor(24, 200);
tft.print("BALAN & SENILLO");
DrawButtonStart();
}
//========================================================================
//========================================================================Menu_display()
void Menu_display() {
tft.setTextSize(5);
tft.setTextColor(CITRON);
tft.setCursor(96, 43);
tft.print("MENU");
DrawButtonControlLED();
DrawButtonTempHum();
DrawButtonDistance();
DrawButtonPhoto();
DrawButtonNoise();
DrawButtonGame();
}
//========================================================================
//========================================================================DisplayMainMenu()
void DisplayMainMenu() {
GetTSPoint();
//----------------------------------------Conditions for detecting when the Button for controlling the LED is touched and its command (Enter the LED controlling menu)
if (x_set_rotatoon_135 > 17 && x_set_rotatoon_135 < (17+130) && y_set_rotatoon_135 > 90 && y_set_rotatoon_135 < (90+40)) {
Menu = 2;
DrawButtonControlLEDPress();
delay(150);
DrawButtonControlLED();
delay(200);
tft.fillScreen(SALTPAN);
delay(200);
DrawButtonGreen(BtnGreenX,BtnGreenY);
DrawButtonRed(BtnRedX, BtnRedY);
tft.setTextSize(2);
tft.setTextColor(BLACK, SALTPAN);
tft.setCursor(50, 180);
tft.print("RED");
DrawButtonGreen1(BtnGreenX1,BtnGreenY1);
DrawButtonRed1(BtnRedX1, BtnRedY1);
tft.setTextSize(2);
tft.setTextColor(BLACK, SALTPAN);
tft.setCursor(125, 180);
tft.print("GREEN");
DrawButtonGreen2(BtnGreenX2,BtnGreenY2);
DrawButtonRed2(BtnRedX2, BtnRedY2);
tft.setTextSize(2);
tft.setTextColor(BLACK, SALTPAN);
tft.setCursor(215, 180);
tft.print("BLUE");
DrawButtonBack(10, 200);
}
//----------------------------------------Condition to detect when the button to display DHT22 sensor data is touched and the command (Enter the menu displays DHT22 sensor data)
if (x_set_rotatoon_135 > 17 && x_set_rotatoon_135 < (17+130) && y_set_rotatoon_135 > 140 && y_set_rotatoon_135 < (140+40)) {
Menu = 3;
DrawButtonTempHumPress();
delay(150);
DrawButtonTempHum();
delay(200);
tft.fillScreen(SALTPAN);
delay(200);
tft.drawLine(15, 40, 300, 40, MAROON);
tft.drawLine(15, 39, 300, 39, MAROON);
tft.setTextSize(2);
tft.setTextColor(BLUE);
tft.setCursor(40, 13);
tft.print("Temperature & Humidity");
draw_bar(x_bar_t, y_bar_t);
tft.drawLine(190, 60, 190, 227, MAROON);
tft.drawLine(190, 127, 300, 127, MAROON);
tft.fillRect(202, 60, 100, 27, CYAN);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(205, 65);
tft.print("Humidity");
tft.fillRect(202, 140, 100, 43, GREEN);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(227, 145);
tft.print("Heat");
tft.setCursor(220, 165);
tft.print("Index");
DrawButtonBack(8, 6);
GetDHT22Data();
delay(750);
}
//----------------------------------------
//----------------------------------------Conditions for detecting when the Button for Ultrasensor is touched and its command (Enter the Distance Measuring menu)
if (x_set_rotatoon_135 > 167 && x_set_rotatoon_135 < (167+130) && y_set_rotatoon_135 > 90 && y_set_rotatoon_135 < (90+40)) {
Menu = 4;
DrawButtonDistancePress();
delay(150);
DrawButtonDistance();
delay(200);
setRGB(0, 0, 0), delay(200);
setRGB(255, 0, 0), delay(200);
setRGB(0, 0, 255), delay(200);
setRGB(125, 125, 125), delay(200);
setRGB(255, 255, 255), delay(200);
tft.fillScreen(SALTPAN);
tft.fillRoundRect(10, 5, 230, 40, 5, CREAM);
tft_msg(48, 18, 2, NULL, "Distance Measure");
tft.fillRoundRect(10, 50, 300, 60, 5, SPROUT);
tft.drawRoundRect(10, 50, 300, 60, 5, CREAM);
tft.drawRoundRect(11, 51, 298, 58, 5, CREAM);
tft.drawRoundRect(12, 52, 296, 56, 5, CREAM);
tft_msg(16, 70, 3, NULL, "Distance:");
tft.fillRoundRect(10, 118, 200, 30, 5, ASPARAGUS);
tft.drawRoundRect(10, 118, 200, 30, 5, CREAM);
tft.drawRoundRect(11, 119, 198, 28, 5, CREAM);
tft_msg(14, 126, 2, NULL, "Status:");
DrawButtonBack(14, 10);
ShowUltraData();
delay(500);
}
//----------------------------------------
//----------------------------------------Conditions for detecting when the Button for Photoresistor is touched and its command (Enter the Photoresistor menu)
if (x_set_rotatoon_135 > 167 && x_set_rotatoon_135 < (167+130) && y_set_rotatoon_135 > 140 && y_set_rotatoon_135 < (140+40)) {
Menu = 5;
DrawButtonPhotoPress();
delay(150);
DrawButtonPhoto();
delay(200);
tft.fillScreen(WHITE);
delay(200);
tft.fillRect(20, 20, 280, 200, BLACK);
tft.fillRect(30, 30, 260, 180, SPROUT);
tft.fillRect(30, 80, 260, 10, BLACK);
ShowPhotoData();
delay(500);
}
//----------------------------------------
//----------------------------------------Conditions for detecting when the Button for Noise is touched and its command (Enter the Photoresistor menu)
if (x_set_rotatoon_135 > 17 && x_set_rotatoon_135 < (17+130) && y_set_rotatoon_135 > 190 && y_set_rotatoon_135 < (190+40)) {
Menu = 6;
DrawButtonNoisePress();
delay(150);
DrawButtonNoise();
delay(200);
tft.fillScreen(WHITE);
delay(200);
}
//----------------------------------------
//----------------------------------------Conditions for detecting when the Button for Game is touched and its command (Enter the Photoresistor menu)
if (x_set_rotatoon_135 > 167 && x_set_rotatoon_135 < (167+130) && y_set_rotatoon_135 > 190 && y_set_rotatoon_135 < (190+40)) {
Menu = 7;
DrawButtonGamePress();
delay(150);
DrawButtonGame();
delay(200);
tft.fillScreen(WHITE);
delay(200);
}
//----------------------------------------
}
//========================================================================
//========================================================================draw7Number()
void draw7Number(int n, unsigned int xLoc, unsigned int yLoc, char cS, unsigned int fC, unsigned int bC, char nD) {
unsigned int num=abs(n),i,s,t,w,col,h,a,b,si=0,j=1,d=0,S1=cS,S2=5*cS,S3=2*cS,S4=7*cS,x1=(S3/2)+1,x2=(2*S1)+S2+1,y1=yLoc+x1,y3=yLoc+(2*S1)+S4+1;
unsigned int seg[7][3]={{(S3/2)+1,yLoc,1},{x2,y1,0},{x2,y3+x1,0},{x1,(2*y3)-yLoc,1},{0,y3+x1,0},{0,y1,0},{x1,y3,1}};
unsigned char nums[12]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67,0x00,0x40},c=(c=abs(cS))>10?10:(c<1)?1:c,cnt=(cnt=abs(nD))>10?10:(cnt<1)?1:cnt;
for (xLoc+=cnt*(d=(2*S1)+S2+(2*S3)+2);cnt>0;cnt--){
for (i=(num>9)?num%10:((!cnt)&&(n<0))?11:((nD<0)&&(!num))?10:num,xLoc-=d,num/=10,j=0;j<7;++j){
col=(nums[i]&(1<<j))?fC:bC;s=(2*S1)/S3;
if (seg[j][2])for(w=S2,t=seg[j][1]+S3,h=seg[j][1]+(S3/2),a=xLoc+seg[j][0]+S1,b=seg[j][1];b<h;b++,a-=s,w+=(2*s))tft.drawFastHLine(a,b,w,col);
else for(w=S4,t=xLoc+seg[j][0]+S3,h=xLoc+seg[j][0]+S3/2,b=xLoc+seg[j][0],a=seg[j][1]+S1;b<h;b++,a-=s,w+=(2*s))tft.drawFastVLine(b,a,w,col);
for (;b<t;b++,a+=s,w-=(2*s))seg[j][2]?tft.drawFastHLine(a,b,w,col):tft.drawFastVLine(b,a,w,col);
}
}
}
//========================================================================
//========================================================================ControlTheLED()
void ControlTheLED() {
GetTSPoint();
if (x_set_rotatoon_135 > BtnGreenX && x_set_rotatoon_135 < (BtnGreenX+54) && y_set_rotatoon_135 > BtnGreenY && y_set_rotatoon_135 < (BtnGreenY+54)) {
tft.setTextSize(2);
tft.setTextColor(GREEN, SALTPAN);
tft.setCursor(44, 180);
tft.print("R ON ");
digitalWrite(LED, HIGH);
DrawButtonGreenPress(BtnGreenX, BtnGreenY);
delay(100);
DrawButtonGreen(BtnGreenX,BtnGreenY);
}
if (x_set_rotatoon_135 > BtnRedX && x_set_rotatoon_135 < (BtnRedX+54) && y_set_rotatoon_135 > BtnRedY && y_set_rotatoon_135 < (BtnRedY+54)) {
tft.setTextSize(2);
tft.setTextColor(RED, SALTPAN);
tft.setCursor(44, 180);
tft.print("R OFF");
digitalWrite(LED, LOW);
DrawButtonRedPress(BtnRedX, BtnRedY);
delay(100);
DrawButtonRed(BtnRedX, BtnRedY);
}
if (x_set_rotatoon_135 > BtnGreenX1 && x_set_rotatoon_135 < (BtnGreenX1+54) && y_set_rotatoon_135 > BtnGreenY1 && y_set_rotatoon_135 < (BtnGreenY1+54)) {
tft.setTextSize(2);
tft.setTextColor(GREEN, SALTPAN);
tft.setCursor(125, 180);
tft.print("G ON ");
digitalWrite(LED1, HIGH);
DrawButtonGreenPress(BtnGreenX1, BtnGreenY1);
delay(100);
DrawButtonGreen(BtnGreenX1,BtnGreenY1);
}
if (x_set_rotatoon_135 > BtnRedX1 && x_set_rotatoon_135 < (BtnRedX1+54) && y_set_rotatoon_135 > BtnRedY1 && y_set_rotatoon_135 < (BtnRedY1+54)) {
tft.setTextSize(2);
tft.setTextColor(RED, SALTPAN);
tft.setCursor(125, 180);
tft.print("G OFF");
digitalWrite(LED1, LOW);
DrawButtonRedPress(BtnRedX1, BtnRedY1);
delay(100);
DrawButtonRed(BtnRedX1, BtnRedY1);
}
if (x_set_rotatoon_135 > BtnGreenX2 && x_set_rotatoon_135 < (BtnGreenX2+54) && y_set_rotatoon_135 > BtnGreenY2 && y_set_rotatoon_135 < (BtnGreenY2+54)) {
tft.setTextSize(2);
tft.setTextColor(GREEN, SALTPAN);
tft.setCursor(210, 180);
tft.print("B ON ");
digitalWrite(LED2, HIGH);
DrawButtonGreenPress(BtnGreenX2, BtnGreenY2);
delay(100);
DrawButtonGreen(BtnGreenX2,BtnGreenY2);
}
if (x_set_rotatoon_135 > BtnRedX2 && x_set_rotatoon_135 < (BtnRedX2+54) && y_set_rotatoon_135 > BtnRedY2 && y_set_rotatoon_135 < (BtnRedY2+54)) {
tft.setTextSize(2);
tft.setTextColor(RED, SALTPAN);
tft.setCursor(210, 180);
tft.print("B OFF");
digitalWrite(LED2, LOW);
DrawButtonRedPress(BtnRedX2, BtnRedY2);
delay(100);
DrawButtonRed(BtnRedX2, BtnRedY2);
}
if (x_set_rotatoon_135 > 10 && x_set_rotatoon_135 < (10+30) && y_set_rotatoon_135 > 200 && y_set_rotatoon_135 < (200+30)) {
Menu = 1;
DrawButtonBackPress(10, 200);
delay(100);
DrawButtonBack(10, 200);
delay(100);
tft.fillScreen(SALTPAN);
delay(100);
Menu_display();
}
}
//========================================================================
//========================================================================ShowDHT22Data()
void ShowDHT22Data() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
GetDHT22Data();
}
T_to_Bar = map(t, 0.0, 50.0, 108, 0);
tft.fillRect(x_bar_t+8, (y_bar_t+8)+T_to_Bar, 19, 108-T_to_Bar, ORANGE);
tft.fillRect(x_bar_t+8, y_bar_t+8, 19, T_to_Bar, WHITE);
tft.setTextSize(2);
tft.setTextColor(ORANGE, SALTPAN);
tft.setCursor(75, 100);
tft.print(t);
if (t < 10) tft.print(" ");
tft.setCursor(160, 100);
tft.print((char)247);
tft.println("C");
tft.setCursor(75, 135);
tft.print(f);
if (f < 100) tft.print(" ");
tft.setCursor(160, 135);
tft.print((char)247);
tft.println("F");
tft.setTextSize(3);
tft.setTextColor(CYAN, SALTPAN);
tft.setCursor(205, 95);
tft.print(h);
tft.print(" %");
tft.setTextSize(1);
tft.setTextColor(GREEN, SALTPAN);
tft.setCursor(205, 200);
tft.print(hic);
tft.print(" ");
tft.print((char)247);
tft.print("C");
if (hic < 10) tft.print(" ");
tft.setTextSize(2);
tft.setTextColor(GREEN, SALTPAN);
tft.setCursor(205, 220);
tft.print(hif);
tft.print(" ");
tft.print((char)247);
tft.print("F");
if (hif < 100) tft.print(" ");
GetTSPoint();
if (x_set_rotatoon_135 > 8 && x_set_rotatoon_135 < (8+30) && y_set_rotatoon_135 > 6 && y_set_rotatoon_135 < (6+30)) {
Menu = 1;
DrawButtonBackPress(8, 6);
delay(100);
DrawButtonBack(8, 6);
delay(100);
tft.fillScreen(SALTPAN);
delay(100);
Menu_display();
}
}
//========================================================================
//========================================================================ShowUltraData()
void ShowUltraData() {
noTone(buzzerPin);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;//integers value
distance2 = (duration / 2) / 29.1;//decimal value
if (distance >= 75 || distance <= 0)
{
safeState = 1;
riskyState = 0;
dangerState = 0;
setRGB(0, 255, 0);
GetUltraData(safeState, riskyState, dangerState, distance2);//Displayer Funtion
}
else if (distance > 25) {
safeState = 0;
riskyState = 1;
dangerState = 0;
setRGB(0, 0, 255);
GetUltraData(safeState, riskyState, dangerState, distance2);
}
else
{
safeState = 0;
riskyState = 0;
dangerState = 1;
setRGB(255, 0, 0);
GetUltraData(safeState, riskyState, dangerState, distance2);
}
GetTSPoint();
if (x_set_rotatoon_135 > 14 && x_set_rotatoon_135 < (14+30) && y_set_rotatoon_135 > 10 && y_set_rotatoon_135 < (10+30)) {
Menu = 1;
DrawButtonBackPress(14, 10);
delay(100);
DrawButtonBack(14, 10);
delay(100);
tft.fillScreen(SALTPAN);
delay(100);
Menu_display();
}
}
//========================================================================
//========================================================================ShowPhotoData()
void ShowPhotoData() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
GetPhotoData();
}
draw7Number(lux, 30 , 40 , 8 , BLACK, SPROUT, -2);
}
//========================================================================
//========================================================================ShowNoiseData()
void ShowNoiseData() {
}
//========================================================================
//========================================================================ShowGameData()
void ShowGameData() {
}
//========================================================================
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<