Advertisement
wluijkx

Thermostaat

Nov 7th, 2020 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.82 KB | None | 0 0
  1. //**************************************************************************************
  2. // Code voor een wifi thermostaat met een DHT sensor (11 of 22) en touchscreen TFT    **
  3. //**************************************************************************************
  4.  
  5. //ESPWebtime code voor het ophalen van tijd via
  6. #include <NTPClient.h>
  7.  
  8. #include <ESP8266WiFi.h>
  9.  
  10. #include <WiFiUdp.h>
  11.  
  12. const char * ssid = "";
  13. const char * password = "";
  14.  
  15. const long utcOffsetInSeconds = 3600;
  16.  
  17. // Define NTP Client to get time
  18. WiFiUDP ntpUDP;
  19. NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
  20.  
  21. #include <SPI.h>
  22.  
  23. #include <Adafruit_GFX.h>
  24.  
  25. #include <Adafruit_ILI9341.h>
  26.  
  27. #include <XPT2046_Touchscreen.h>
  28.  
  29. #define TFT_CS D0 //for D1 mini or TFT I2C Connector Shield (V1.1.0 or later)
  30. #define TFT_DC D8 //for D1 mini or TFT I2C Connector Shield (V1.1.0 or later)
  31. #define TFT_RST - 1 //for D1 mini or TFT I2C Connector Shield (V1.1.0 or later)
  32. #define TS_CS D3 //for D1 mini or TFT I2C Connector Shield (V1.1.0 or later)
  33.  
  34.  
  35. // #define TFT_CS 14  //for D32 Pro
  36. // #define TFT_DC 27  //for D32 Pro
  37. // #define TFT_RST 33 //for D32 Pro
  38. // #define TS_CS  12 //for D32 Pro
  39.  
  40. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  41. XPT2046_Touchscreen ts(TS_CS);
  42.  
  43. #include <DHT.h>             // DHT sensor (temp en luchtvochtigheid)
  44.  
  45. // Fonts https://rop.nl/truetype2gfx/
  46. #include <Fonts/Segment10pt7b.h>
  47.  
  48. #include <Fonts/Segment18pt7b.h>
  49.  
  50. #include <Fonts/Segment22pt7b.h>
  51.  
  52. #include <Fonts/Segment48pt7b.h>
  53.  
  54. #include <Fonts/icons14pt7b.h>
  55.  
  56. #include <Fonts/icons30pt7b.h>
  57.  
  58.  
  59. //DHT sensor
  60. #define DHTPIN D4
  61. #define DHTTYPE DHT11
  62.  
  63. //Vertalen
  64. #define TS_MINX 230
  65. #define TS_MAXX 3600
  66.  
  67. #define TS_MINY 330
  68. #define TS_MAXY 3700
  69.  
  70. //https://github.com/wemos/D1_mini_Examples/blob/master/examples/04.Shields/TFT_2.4_Touch_Shield/TFT_TouchTest/TFT_TouchTest.ino
  71.  
  72. DHT dht(DHTPIN, DHTTYPE);
  73.  
  74. // 16-bit kleuren definities: https://ee-programming-notepad.blogspot.com/2016/10/16-bit-color-generator-picker.html
  75. #define BLACK 0x0000
  76. #define WHITE 0xFFFF
  77. #define BUTTONBG 0xFFFF
  78. #define BUTTONEDGE 0x9492
  79. #define TEXTCOLOR 0x0000
  80. #define SCREENCOLOR 0xFFFF
  81. #define TOGGLEON 0x53C9
  82. #define TOGGLEOFF 0xDEFB
  83. #define HEATON 0xD1E3
  84. #define HEATOFF 0xDEFB
  85. #define WIFION 0x0000
  86. #define WIFIOFF 0xD1E3
  87.  
  88. //Drukbereik touchscreen
  89. #define MINPRESSURE 400
  90. #define MAXPRESSURE 3000
  91.  
  92. // Variabelen
  93. unsigned long currentTimeMillis = millis();
  94. unsigned long previousTimeMillis;
  95. unsigned long currentScreenTimOuteMillis = millis();
  96. unsigned long previousScreenTimeOutMillis;
  97. int TimeOutPeriod = 20000; //Timeout voor het dimmen van het scherm
  98. int DimMode = 1; //1= helder, 2 = gedimd, 3 = nachtscherm, 4 = uit
  99. float TempSet = 14; //Ingestelde temperatuur
  100. float PrevTempSet;
  101. int D = 1;
  102. String DD = "Ma";
  103. String DayNames[7] = {"Zo","Ma","Di","Wo","Do","Vr","Za"};
  104. int H = 112; // Uren binnen honderd (om de eerste 1 er weer vanaf te kunnen knippen)
  105. int M = 100; // Minuten
  106. String HH;
  107. String MM;
  108. String DisplayTime;
  109. String PrevDisplayTime;
  110. float Temp = dht.readTemperature();
  111. float PrevTemp;
  112. int Humid = dht.readHumidity();
  113. int PrevHumid;
  114. String RecieveFromSerial;
  115. String TimePartH;
  116. String TimePartM;
  117. int HeatState = 0;
  118. int PrevHeatState = 2;
  119. int power1 = 0;
  120. int power2 = 0;
  121. int DisplayScreen = 1;
  122. int PrevDisplayScreen = 0;
  123. int ScreenTouch = 0;
  124. int Toggle1 = 0;
  125. int Toggle2 = 0;
  126. int PrevToggle1 = 2;
  127. int PrevToggle2 = 2;
  128. int WifiStatus = 0;
  129. int PrevWifiStatus = 2;
  130. int HeatingMode = 3; //1=automatisch 2=uitschakeltimer 3=handmatig
  131.  
  132. //schermconfiguratie
  133. //Icoontjes: z Power, O sun high, Q Sun low, k clock, = Wifi, ! error, V checkbox checked, W checkbox empty, U Unlocked, L Locked, d Settings, F WaterDrop, 4 up, ~ down, € €, B Menu btn, h Menu open, , arrow < , . arrow >, Y Pin
  134. int MenuBtnX = 140;
  135. int MenuBtnY = 210;
  136. int CurTempX = 5;
  137. int CurTempY = 50;
  138. String TitleCurTemp = "";
  139. int SetTempX = 0;
  140. int SetTempY = 100;
  141. String TitleSetT = "";
  142. int CurTemp2X = 110;
  143. int CurTemp2Y = 50;
  144. String TitleCurTemp2 = "";
  145. int SetTemp2X = 62;
  146. int SetTemp2Y = 100;
  147. String TitleSetT2 = "";
  148. int UpBtnX = 260;
  149. int UpBtnY = 120;
  150. char UpBtnIcon = '+';
  151. int DwnBtnX = 10;
  152. int DwnBtnY = 120;
  153. char DwnBtnIcon = '~';
  154. int TimeDisplX = 100;
  155. int TimeDisplY = 5;
  156. int HumidX = 5;
  157. int HumidY = 210;
  158. String HumidIcon = "F";
  159. int Toggle1X = 210;
  160. int Toggle1Y = 30;
  161. char Toggle1Icon = 'z';
  162. String Toggle1Title = "";
  163. int Toggle2X = 210;
  164. int Toggle2Y = 120;
  165. char Toggle2Icon = 'z';
  166. int HeatStateX = 285;
  167. int HeatStateY = 232;
  168. char HeatOnIcon = 'O';
  169. char HeatOffIcon = 'Q';
  170. int WifiPosX = 10;
  171. int WifiPosY = 25;
  172. char WifiOnIcon = '=';
  173. char WifiOffIcon = '!';
  174.  
  175. String MenuOption1 = "Automatisch";
  176. int MenuOpt1Number = 1;
  177. String MenuOption2 = "Uitschakeltimer";
  178. int MenuOpt2Number = 2;
  179. String MenuOption3 = "Handmatig";
  180. int MenuOpt3Number = 3;
  181. String MenuOption4 = "Schema";
  182. int MenuOpt4Number = 4;
  183. String MenuOption5 = "Schakelaarklok";
  184. int MenuOpt5Number = 5;
  185. String MenuOption6 = "Andere ruimte";
  186. int MenuOpt6Number = 6;
  187.  
  188. void setup() {
  189.     Serial.begin(9600);
  190.     timeClient.begin();
  191.     timeClient.setUpdateInterval(3600000); //Elk uur updaten i.p.v. elke minuut
  192.     dht.begin();
  193.     initDisplay();
  194.     pinMode(D1,OUTPUT);
  195.     digitalWrite(D1,1);
  196.     tft.fillScreen(SCREENCOLOR);
  197.     tft.setTextColor(TEXTCOLOR);
  198.     tft.setFont( & Segment10pt7b);
  199.     tft.setCursor(50, 150);
  200.     tft.print("Verbinden");
  201.  
  202.     WiFi.begin(ssid, password);
  203.  
  204.     while (WiFi.status() != WL_CONNECTED) {
  205.         delay(500);
  206.         tft.print(".");
  207.     }
  208.     GetWifiStatus();
  209.     tft.fillScreen(SCREENCOLOR);
  210.     GetTime();
  211.     delay(500);
  212. }
  213.  
  214. void initDisplay() {
  215.     ts.begin();
  216.     ts.setRotation(0);
  217.     tft.begin();
  218.     tft.setRotation(3);
  219. }
  220.  
  221. void ScreenTimeOut(){
  222.   currentScreenTimOuteMillis = millis();
  223.   if (previousScreenTimeOutMillis + TimeOutPeriod < currentScreenTimOuteMillis)
  224.   if ((H-100)*100+(M-100) > 1645){
  225.     digitalWrite(D1,0);
  226.   }
  227.   else {analogWrite(D1,150);}
  228. }
  229.  
  230. void GetWifiStatus() {
  231.     if (WiFi.status() == WL_CONNECTED) {
  232.         WifiStatus = 1;
  233.     } else {
  234.         WifiStatus = 0;
  235.     }
  236. }
  237.  
  238. void DrawWifiStatus(int posX, int posY) {
  239.     if ((WifiStatus != PrevWifiStatus)||(DisplayScreen != PrevDisplayScreen)) {
  240.         tft.fillRect(posX - 4, posY - 20, 35, 35, SCREENCOLOR);
  241.         tft.setCursor(posX, posY);
  242.         tft.setFont( & icons14pt7b);
  243.         if (WifiStatus == 1) {
  244.             tft.setTextColor(WIFION);
  245.             tft.print(WifiOnIcon);
  246.         } else {
  247.             tft.setTextColor(WIFIOFF);
  248.             tft.print(WifiOffIcon);
  249.         }
  250.         PrevWifiStatus = WifiStatus;
  251.     }
  252. }
  253. void GetTime() {
  254.     timeClient.update();
  255.  
  256.     H = timeClient.getHours() + 100;
  257.     M = timeClient.getMinutes() + 100;
  258.     D = timeClient.getDay();
  259.     DD = DayNames[D];
  260. }
  261.  
  262. void DrawHeatState(int posX, int posY) {
  263.     if (DisplayScreen != PrevDisplayScreen){
  264.       PrevHeatState = 2;
  265.     }
  266.     tft.setCursor(posX, posY);
  267.     tft.setFont( & icons14pt7b);
  268.     if (Temp < TempSet) {
  269.         HeatState = 1;
  270.         tft.setTextColor(HEATON);
  271.         if (HeatState != PrevHeatState) {
  272.             tft.fillRect(posX - 4, posY - 20, 35, 35, SCREENCOLOR);
  273.             tft.print(HeatOnIcon);
  274.             PrevHeatState = 1;
  275.         }
  276.     } else {
  277.         HeatState = 0;
  278.         tft.setTextColor(HEATOFF);
  279.         if (HeatState != PrevHeatState) {
  280.             tft.fillRect(posX - 4, posY - 20, 35, 35, SCREENCOLOR);
  281.             tft.print(HeatOffIcon);
  282.             PrevHeatState = 0;
  283.         }
  284.     }
  285. }
  286.  
  287. void ToggleHeating() {
  288.     if (DisplayScreen != PrevDisplayScreen) {
  289. //        getTemp();
  290.         DrawHeatState(HeatStateX, HeatStateY);
  291.     }
  292. }
  293.  
  294. void RecTime() {
  295.     if ((Serial.read()) > (0)) {
  296.         RecieveFromSerial = String(Serial.readStringUntil('\n'));
  297.         if (((RecieveFromSerial.substring(1, 1)) == ("T"))) {
  298.             TimePartH = RecieveFromSerial.substring(0, 2);
  299.             TimePartM = RecieveFromSerial.substring(2, 4);
  300.             H = TimePartH.toInt() + 100;
  301.             M = TimePartM.toInt() + 100;
  302.             DrawTime(TimeDisplX, TimeDisplY);
  303.         }
  304.     }
  305. }
  306.  
  307. void DrawCurTemp(int posX, int posY) {
  308.     getTemp();
  309.     if ((Temp != PrevTemp) || (DisplayScreen != PrevDisplayScreen)) {
  310.         tft.setFont( & Segment10pt7b);
  311.         tft.setCursor(posX, posY - 4);
  312.         tft.setTextColor(TEXTCOLOR);
  313.         tft.print(TitleCurTemp);
  314.         getTemp();
  315.         char T[8];
  316.         dtostrf(Temp, 6, 1, T);
  317.         tft.fillRoundRect(posX, posY, 110, 45, 5, BUTTONBG);
  318.         tft.setTextColor(TEXTCOLOR);
  319.         tft.setFont( & Segment22pt7b);
  320.         tft.setCursor(posX - 25, posY + 42);
  321.         tft.print(T);
  322.         int curposx = tft.getCursorX();
  323.         int curposy = tft.getCursorY();
  324.         tft.setCursor(curposx + 2, curposy - 16);
  325.         tft.setFont( & Segment10pt7b);
  326.         tft.print("o");
  327.         PrevTemp = Temp;
  328.     }
  329. }
  330.  
  331. void DrawSetTemp(int posX, int posY) {
  332.     if ((TempSet != PrevTempSet) || (DisplayScreen != PrevDisplayScreen)) {
  333.         char TsetT[8];
  334.         dtostrf(TempSet, 6, 1, TsetT);
  335.         String TempString = String(TsetT);
  336.         String TempString2 = String(TsetT);
  337.         TempString.replace("1", "");
  338.         tft.setFont( & Segment10pt7b);
  339.         tft.setCursor(SetTempX, SetTempY);
  340.         tft.setTextColor(TEXTCOLOR);
  341.         tft.print(TitleSetT);
  342.         tft.fillRoundRect(posX, posY + 4, 190, 82, 5, BUTTONBG);
  343.         //   tft.drawRoundRect(setTposX, setTposY + 4, 190, 82, 5, BUTTONEDGE);
  344.         tft.setTextColor(TEXTCOLOR);
  345.         tft.setFont( & Segment48pt7b);
  346.         //als er een 1 in e temperatuur zit dan is er een andere uitlijning nodig
  347.         if (TempString == TempString2) {
  348.             tft.setCursor(posX - 86, posY + 75);
  349.         } else {
  350.             tft.setCursor(posX - 70, posY + 75);
  351.         }
  352.         tft.print(TsetT);
  353.         int curposx = tft.getCursorX();
  354.         int curposy = tft.getCursorY();
  355.         tft.setCursor(curposx + 2, curposy - 35);
  356.         tft.setFont( & Segment22pt7b);
  357.         tft.print("o");
  358.         PrevTempSet = TempSet;
  359.     }
  360. }
  361.  
  362. void DrawMenuBtn(int posX, int posY) {
  363.     if (DisplayScreen != PrevDisplayScreen) {
  364.         tft.fillRoundRect(posX, posY, 35, 22, 5, BUTTONBG);
  365.         tft.drawRoundRect(posX, posY, 35, 22, 5, BUTTONEDGE);
  366.         tft.setCursor(posX + 5, posY + 10);
  367.         tft.setTextColor(TEXTCOLOR);
  368.         tft.setFont( & Segment22pt7b);
  369.         tft.print("...");
  370.     }
  371. }
  372.  
  373. void DrawMenu() {
  374.     DrawMenuBtn(MenuBtnX, MenuBtnY);
  375.     tft.setTextColor(TEXTCOLOR);
  376.     tft.setFont( & Segment10pt7b);
  377.     int xpos = 5;
  378.     int ypos = 10;
  379.  
  380.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  381.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  382.     tft.setCursor(xpos + 10, ypos + 32);
  383.     tft.print(MenuOption1);
  384.     ypos += 65;
  385.  
  386.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  387.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  388.     tft.setCursor(xpos + 10, ypos + 32);
  389.     tft.print(MenuOption2);
  390.     ypos += 65;
  391.  
  392.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  393.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  394.     tft.setCursor(xpos + 10, ypos + 32);
  395.     tft.print(MenuOption3);
  396.     ypos += 65;
  397.  
  398.     xpos = 165;
  399.     ypos = 10;
  400.  
  401.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  402.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  403.     tft.setCursor(xpos + 10, ypos + 32);
  404.     tft.print(MenuOption4);
  405.     ypos += 65;
  406.  
  407.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  408.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  409.     tft.setCursor(xpos + 10, ypos + 32);
  410.     tft.print(MenuOption5);
  411.     ypos += 65;
  412.  
  413.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  414.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  415.     tft.setCursor(xpos + 10, ypos + 32);
  416.     tft.print(MenuOption6);
  417.     ypos += 65;
  418. }
  419.  
  420. void DrawToggle1(int PosX, int PosY) {
  421.     if ((Toggle1 != PrevToggle1) || (DisplayScreen != PrevDisplayScreen)) {
  422.         tft.fillRoundRect(PosX, PosY, 80, 80, 15, BUTTONBG);
  423.         tft.drawRoundRect(PosX, PosY, 80, 80, 15, BUTTONEDGE);
  424.         tft.setFont( & icons30pt7b);
  425.         tft.setCursor(PosX + 16, PosY + 55);
  426.         if (Toggle1 == 0) {
  427.             tft.setTextColor(TOGGLEOFF);
  428.             PrevToggle1 = 0;
  429.             tft.print(Toggle1Icon);
  430.         } else {
  431.             tft.setTextColor(TOGGLEON);
  432.             PrevToggle1 = 1;
  433.             tft.print(Toggle1Icon);
  434.         }
  435.     }
  436. }
  437.  
  438. void SetToggle1() {
  439.     if (Toggle1 == 0) {
  440.         Toggle1 = 1;
  441.     } else {
  442.         Toggle1 = 0;
  443.     }
  444.     DrawToggle1(Toggle1X, Toggle1Y);
  445. }
  446. void DrawToggle2(int PosX, int PosY) {
  447.     if ((Toggle2 != PrevToggle2) || (DisplayScreen != PrevDisplayScreen)) {
  448.         tft.fillRoundRect(PosX, PosY, 80, 80, 15, BUTTONBG);
  449.         tft.drawRoundRect(PosX, PosY, 80, 80, 15, BUTTONEDGE);
  450.         tft.setFont( & icons30pt7b);
  451.         tft.setCursor(PosX + 16, PosY + 55);
  452.         if (Toggle2 == 0) {
  453.             tft.setTextColor(TOGGLEOFF);
  454.             PrevToggle2 = 0;
  455.             tft.print(Toggle2Icon);
  456.         } else {
  457.             tft.setTextColor(TOGGLEON);
  458.             PrevToggle2 = 1;
  459.             tft.print(Toggle2Icon);
  460.         }
  461.     }
  462. }
  463.  
  464. void SetToggle2() {
  465.     if (Toggle2 == 0) {
  466.         Toggle2 = 1;
  467.     } else {
  468.         Toggle2 = 0;
  469.     }
  470.     DrawToggle2(Toggle2X, Toggle2Y);
  471. }
  472.  
  473. void DrawUpBtn(int posX, int posY) {
  474.     if (DisplayScreen != PrevDisplayScreen) {
  475.         tft.fillRoundRect(posX, posY, 50, 50, 5, BUTTONBG);
  476.         tft.drawRoundRect(posX, posY, 50, 50, 5, BUTTONEDGE);
  477.         tft.setCursor(posX + 12, posY + 37);
  478.         tft.setTextColor(TEXTCOLOR);
  479.         tft.setFont( & Segment22pt7b);
  480.         tft.print("+");
  481.     }
  482. }
  483. void DrawDwnBtn(int posX, int posY) {
  484.     if (DisplayScreen != PrevDisplayScreen) {
  485.         tft.fillRoundRect(posX, posY, 50, 50, 5, BUTTONBG);
  486.         tft.drawRoundRect(posX, posY, 50, 50, 5, BUTTONEDGE);
  487.         tft.setCursor(posX + 15, posY + 40);
  488.         tft.setTextColor(TEXTCOLOR);
  489.         tft.setFont( & Segment22pt7b);
  490.         tft.print("-");
  491.     }
  492. }
  493.  
  494. void Clock() {
  495.     currentTimeMillis = millis();
  496.     if ((M) > (159)) {
  497.         M = 100;
  498.         H += 1;
  499.     }
  500.     if ((H) > (123)) {
  501.         H = 100;
  502.     }
  503.     HH = String(H);
  504.     MM = String(M);
  505.     if ((H) > (109)) {
  506.         DisplayTime = String(HH.substring(2 - 1, 3) + String(":") + MM.substring(2 - 1, 3));
  507.     } else {
  508.         DisplayTime = String(HH.substring(3 - 1, 3) + String(":") + MM.substring(2 - 1, 3));
  509.     }
  510. //    DisplayTime = DD + " "+DisplayTime;
  511.     if (currentTimeMillis - previousTimeMillis > 60000) {
  512.         previousTimeMillis = previousTimeMillis + 60000;
  513.         M += 1;
  514.     }
  515. }
  516.  
  517. void DrawTime(int posX, int posY) {
  518.     Clock();
  519.     if ((DisplayTime != PrevDisplayTime) || (DisplayScreen != PrevDisplayScreen)) {
  520.         tft.fillRect(posX, posY, 100, 35, SCREENCOLOR);
  521.         tft.setCursor(posX + 7, posY + 30);
  522.         tft.setTextColor(TEXTCOLOR);
  523.         tft.setFont( & Segment22pt7b);
  524.         tft.print(DisplayTime);
  525.         PrevDisplayTime = DisplayTime;
  526.     }
  527. }
  528.  
  529. void DrawHumid(int posX, int posY) {
  530.     if ((Humid != PrevHumid) || (DisplayScreen != PrevDisplayScreen)) {
  531.         Humid = dht.readHumidity() - 1.4;
  532.         tft.fillRect(posX, posY, 80, 35, SCREENCOLOR);
  533.         tft.setCursor(posX + 6, posY + 22);
  534.         tft.setTextColor(TEXTCOLOR);
  535.         tft.setFont( & icons14pt7b);
  536.         tft.print(HumidIcon);
  537.         tft.setFont( & Segment10pt7b);
  538.         tft.print(" ");
  539.         tft.print(Humid);
  540.         tft.print("%");
  541.         PrevHumid = Humid;
  542.     }
  543. }
  544.  
  545. void TempUp() {
  546.     TempSet = TempSet + 0.5;
  547. }
  548.  
  549. void TempDown() {
  550.     TempSet = TempSet - 0.5;
  551. }
  552.  
  553. void getTemp() {
  554.     Temp = dht.readTemperature() - 1.4;
  555. }
  556.  
  557. void loop() {
  558.  
  559.     RecTime();
  560.     GetTime();
  561.     ToggleHeating();
  562.     GetWifiStatus();
  563.     ScreenTimeOut();
  564.     if (DisplayScreen != PrevDisplayScreen) {
  565.         tft.fillScreen(SCREENCOLOR);
  566.     }
  567.  
  568.     unsigned long currentMillis = millis();
  569.     unsigned long previousMillis;
  570.     if (DisplayScreen == 0) {
  571.         if (DisplayScreen != PrevDisplayScreen) {
  572.             DrawMenu();
  573.             PrevDisplayScreen = 0;
  574.         }
  575.     } else if (DisplayScreen == 1) {
  576.         GetWifiStatus();
  577.         DrawWifiStatus(WifiPosX, WifiPosY);
  578.         DrawMenuBtn(MenuBtnX, MenuBtnY);
  579.         DrawCurTemp(CurTempX, CurTempY);
  580.         DrawSetTemp(SetTempX, SetTempY);
  581.         //         DrawUpBtn(UpBtnX,UpBtnY);
  582.         //         DrawDwnBtn(DwnBtnX,DwnBtnY);
  583.         DrawToggle1(Toggle1X, Toggle1Y);
  584.         DrawToggle2(Toggle2X, Toggle2Y);
  585.         DrawTime(TimeDisplX, TimeDisplY);
  586.         DrawHumid(HumidX, HumidY);
  587.         DrawHeatState(HeatStateX, HeatStateY);
  588.         PrevDisplayScreen = 1;
  589.     } else if (DisplayScreen == 2) {
  590.         GetWifiStatus();
  591.         DrawWifiStatus(WifiPosX, WifiPosY);
  592.         //        DrawMenuBtn(MenuBtnX, MenuBtnY);
  593.         DrawCurTemp(CurTemp2X, CurTemp2Y);
  594.         DrawSetTemp(SetTemp2X, SetTemp2Y);
  595.         DrawUpBtn(UpBtnX, UpBtnY);
  596.         DrawDwnBtn(DwnBtnX, DwnBtnY);
  597.         //         DrawToggle1(Toggle1X,Toggle1Y);
  598.         //         DrawToggle2(Toggle2X,Toggle2Y);
  599.         DrawTime(TimeDisplX, TimeDisplY);
  600.         DrawHumid(HumidX, HumidY);
  601.         DrawHeatState(HeatStateX, HeatStateY);
  602.         PrevDisplayScreen = 2;
  603.     } else if (DisplayScreen == 3) {
  604.         GetWifiStatus();
  605.         DrawWifiStatus(WifiPosX, WifiPosY);
  606.         //        DrawMenuBtn(MenuBtnX, MenuBtnY);
  607.         DrawCurTemp(CurTemp2X, CurTemp2Y);
  608.         DrawSetTemp(SetTemp2X, SetTemp2Y);
  609.         DrawUpBtn(UpBtnX, UpBtnY);
  610.         DrawDwnBtn(DwnBtnX, DwnBtnY);
  611.         //         DrawToggle1(Toggle1X,Toggle1Y);
  612.         //         DrawToggle2(Toggle2X,Toggle2Y);
  613.         DrawTime(TimeDisplX, TimeDisplY);
  614.         DrawHumid(HumidX, HumidY);
  615.         DrawHeatState(HeatStateX, HeatStateY);
  616.         PrevDisplayScreen = 2;
  617.         int BackLight = 0;
  618.     for(int __i__=0;__i__<10;++__i__)
  619.     {
  620.         analogWrite(D1,BackLight);
  621.         BackLight += 25;
  622.         delay(150);
  623.     }
  624.     for(int __i__=0;__i__<10;++__i__)
  625.     {
  626.         analogWrite(D1,BackLight);
  627.         BackLight += -25;
  628.         delay(150);
  629.     }
  630.     analogWrite(D1,255);
  631.     delay(2000);
  632.     digitalWrite(D1,1);
  633.     DisplayScreen = 1;    
  634.     }
  635.    
  636.        
  637.  
  638.  
  639.     //Als het scherm wordt aangeraakt
  640.     if (ts.touched()) {
  641.         previousScreenTimeOutMillis = millis();
  642.         digitalWrite (D1,1);
  643.         TS_Point p = ts.getPoint();
  644.         delay(150);
  645.         //Vertaal de input van het touchscreen naar coordinaten op het scherm
  646.         p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
  647.         p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
  648.  
  649.         //Knoppen van de Menuopties
  650.  
  651.         if (DisplayScreen == 0) {
  652.             int xpos = 5;
  653.             int ypos = 10;
  654.  
  655.             if (((p.x) > (ypos)) && ((p.x) < (ypos + 50)) && ((p.y) > (xpos)) && ((p.y) < (xpos + 150))) {
  656.                 DisplayScreen = 1;
  657.             }
  658.  
  659.             ypos += 65;
  660.             if (((p.x) > (ypos)) && ((p.x) < (ypos + 50)) && ((p.y) > (xpos)) && ((p.y) < (xpos + 150))) {
  661.                 DisplayScreen = 2;
  662.             }
  663.  
  664.             ypos += 65;
  665.             if (((p.x) > (ypos)) && ((p.x) < (ypos + 50)) && ((p.y) > (xpos)) && ((p.y) < (xpos + 150))) {
  666.                 DisplayScreen = 3;
  667.             }
  668.             xpos = 165;
  669.             ypos = 10;
  670.  
  671.             if (((p.x) > (ypos)) && ((p.x) < (ypos + 50)) && ((p.y) > (xpos)) && ((p.y) < (xpos + 150))) {
  672.                 DisplayScreen = 3;
  673.             }
  674.  
  675.             ypos += 65;
  676.             if (((p.x) > (ypos)) && ((p.x) < (ypos + 50)) && ((p.y) > (xpos)) && ((p.y) < (xpos + 150))) {
  677.                 DisplayScreen = 1;
  678.             }
  679.  
  680.             ypos += 65;
  681.             if (((p.x) > (ypos)) && ((p.x) < (ypos + 50)) && ((p.y) > (xpos)) && ((p.y) < (xpos + 150))) {
  682.                 DisplayScreen = 1;
  683.             }
  684.         }
  685.  
  686.         //Startscherm met temperatuur en Toggle knoppen
  687.         if (DisplayScreen == 1) {
  688.             //Temp omhoog knop wordt aangeraakt
  689.  
  690.             if (((p.x) > (SetTempY)) && ((p.x) < (SetTempY + 100)) && ((p.y) > (SetTempX)) && ((p.y) < (SetTempX + 200))) {
  691.                 DisplayScreen = 2;
  692.             }
  693.  
  694.             if (((p.x) > (Toggle1Y)) && ((p.x) < (Toggle1Y + 80)) && ((p.y) > (Toggle1X)) && ((p.y) < (Toggle1X + 80))) {
  695.                 SetToggle1();
  696.             }
  697.             if (((p.x) > (Toggle2Y)) && ((p.x) < (Toggle2Y + 80)) && ((p.y) > (Toggle2X)) && ((p.y) < (Toggle2X + 80))) {
  698.                 SetToggle2();
  699.             }
  700.  
  701.             if (((p.x) > (MenuBtnY)) && ((p.x) < (MenuBtnY + 50)) && ((p.y) > (MenuBtnX)) && ((p.y) < (MenuBtnX + 50))) {
  702.                 DisplayScreen = 0;
  703.             }
  704.  
  705.         }
  706.  
  707.         //Knoppen scherm 2
  708.         else if (DisplayScreen == 2) {
  709.             //Temp omhoog knop wordt aangeraakt
  710.             if (((p.x) > (SetTemp2Y)) && ((p.x) < (SetTemp2Y + 100)) && ((p.y) > (SetTemp2X+20)) && ((p.y) < (SetTemp2X + 200))) {
  711.                 DisplayScreen = 1;
  712.             }
  713.  
  714.             if (((p.x) > (UpBtnY)) && ((p.x) < (UpBtnY + 50)) && ((p.y) > (UpBtnX)) && ((p.y) < (UpBtnX + 50))) {
  715.                 TempUp();
  716.                 DrawSetTemp(SetTemp2X, SetTemp2Y);
  717.                 DrawHeatState(HeatStateX, HeatStateY);
  718.             }
  719.  
  720.             //Temp omlaag knop wordt aangeraakt      
  721.             if (((p.x) > (DwnBtnY)) && ((p.x) < (DwnBtnY + 50)) && ((p.y) > (DwnBtnX)) && ((p.y) < (DwnBtnX + 50))) {
  722.                 TempDown();
  723.                 DrawSetTemp(SetTemp2X, SetTemp2Y);
  724.                 DrawHeatState(HeatStateX, HeatStateY);
  725.             }
  726.         }
  727.         Serial.print("(");
  728.         Serial.print(p.x);
  729.         Serial.print(", ");
  730.         Serial.print(p.y);
  731.         Serial.println(")");
  732.     }
  733.  
  734. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement