// ILI9341 example with embedded color bitmaps in sketch.
// WILL NOT FIT ON ARDUINO UNO OR OTHER AVR BOARDS;
// uses large bitmap image stored in array!

// Options for converting images to the format used here include:
//   http://www.rinkydinkelectronics.com/t_imageconverter565.php    เว็บแปลงรูป
// or
//  GIMP (https://www.gimp.org/) as follows:
//    1. File -> Export As
//    2. In Export Image dialog, use 'C source code (*.c)' as filetype.
//    3. Press export to get the export options dialog.
//    4. Type the desired variable name into the 'prefixed name' box.
//    5. Uncheck 'GLIB types (guint8*)'
//    6. Check 'Save as RGB565 (16-bit)'
//    7. Press export to save your image.
//  Assuming 'image_name' was typed in the 'prefixed name' box of step 4,
//  you can have to include the c file, then using the image can be done with:
//    tft.drawRGBBitmap(0, 0, image_name.pixel_data, image_name.width, image_name.height);
//  See also https://forum.pjrc.com/threads/35575-Export-for-ILI9341_t3-with-GIMP

#include "SPI.h"
#include <Adafruit_ILI9341.h>
#include "Humidity.h"  //รูป

#define TFT_DC 4
#define TFT_CS 2
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
//------------------------------------------------------------//
#define Ry1 27
#define Ry2 25
//-------------Set valiable Pin sw -----
#define sw1 32
#define sw2 26
#define sw3 27
byte cnt = 0;
boolean st_setTemp, st_tempOk, st_humOk, st_setHumi = 0;
byte temp_set = 25;
byte humid_set = 50;

/************************************************************/
void setup() {

  pinMode(Ry1, OUTPUT);
  pinMode(Ry2, OUTPUT);
  pinMode(sw1, INPUT_PULLUP);
  pinMode(sw2, INPUT_PULLUP);
  pinMode(sw3, INPUT_PULLUP);

  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(ILI9341_BLACK);
  //tft.drawRGBBitmap(0, 0, (uint16_t *)dragonBitmap, DRAGON_WIDTH, DRAGON_HEIGHT);

  Serial.begin(9600);
};

/************************************************************/
void loop(void) {


  if (digitalRead(sw1) == 0) {
    delay(2000);  //รอเกด sw1 2วิ
    if (digitalRead(sw1) == 0) {
      Serial.println("MODE SETUP");
      st_setTemp = 1;
      //while Set PEMP
      while (st_setTemp == 1) {
        // temp_setTemp;
        Serial.println("Temp Set = " + String(temp_set));
        /*---------------เงือนไขการตั้งต่า Temp----------------------//
          1. กด sw2 เพิ่มค่าครั้งละ 1
          2. กด sw3 ลดค่าครั้งละ 1
          3. กด sw1 เพื่อยืนยัน */
        if (digitalRead(sw2) == 0) {  //เพิ่มค่า
          while (digitalRead(sw2) == 0) delay(10);
          temp_set++;
          Serial.println("Temp Set = " + String(temp_set));
          st_tempOk = 1;
        }

        if (digitalRead(sw3) == 0) {  //ลดค่า
          while (digitalRead(sw3) == 0) delay(10);
          temp_set--;
          Serial.println("Temp Set = " + String(temp_set));
          st_tempOk = 1;
        }
        tft.setCursor(20, 20);
        tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
        tft.setTextSize(3);
        tft.println("TEMP Set = " + String(temp_set) + " C ");

        if (digitalRead(sw1) == 0 && st_tempOk == 1) {
          st_setTemp = 0;
          st_tempOk = 0;
          st_setHumi = 1;
          tft.setCursor(60, 120);
          tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
          tft.setTextSize(3);
          tft.println("SET TEMP OK");
          Serial.println();
          Serial.println("SET TEMP OK");
          delay(1000);
        }
        delay(100);
      }  //while

      //while Ser humidity

      while (st_setHumi == 1) {
        Serial.println("Humid Set = " + String(humid_set));

        /*---------------เงือนไขการตั้งต่า Temp----------------------//
          1. กด sw2 เพิ่มค่าครั้งละ 1
          2. กด sw3 ลดค่าครั้งละ 1
          3. กด sw1 เพื่อยืนยัน */
        if (digitalRead(sw2) == 0) {  //เพิ่มค่า
          while (digitalRead(sw2) == 0) delay(10);
          humid_set = humid_set + 5;
          Serial.println("Humid Set = " + String(humid_set));
          st_humOk = 1;
        }

        if (digitalRead(sw3) == 0) {  //ลดค่า
          while (digitalRead(sw3) == 0) delay(10);
          humid_set = humid_set - 5;
          Serial.println("Humid Set = " + String(humid_set));
          st_humOk = 1;
        }
        tft.setCursor(20, 60);
        tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
        tft.setTextSize(3);
        tft.println("HUMI Set = " + String(humid_set) + " % ");
        if (digitalRead(sw1) == 0 && st_humOk == 1) {
          st_setTemp = 0;
          st_humOk = 0;
          st_setHumi = 0;

          tft.setCursor(55, 120);
          tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
          tft.setTextSize(3);
          tft.println("SET HUMID OK");
          Serial.println();
          Serial.println("SET HUMID OK");
          delay(1000);
          tft.setCursor(55, 120);
          tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
          tft.setTextSize(3);
          tft.println("             ");
        }

        delay(100);

      }  //while SerHumi
    }    //if
  }      //if 2 วิ
  tft.setCursor(20, 20);
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  tft.setTextSize(3);
  tft.println("TEMP Set = " + String(temp_set) + " C ");

  tft.setCursor(20, 60);
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  tft.setTextSize(3);
  tft.println("HUMI Set = " + String(humid_set) + " % ");

  Serial.println("Temperature Set = " + String(temp_set) + " °C ");
  Serial.println("Humidity Set = " + String(humid_set) + " % ");
  delay(500);
};
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module