yellowsnow2

furnace thermostat

Jun 21st, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.60 KB | None | 0 0
  1. #include <DS3231.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #include <Wire.h>
  5. #include <Adafruit_GFX.h>
  6. #define ONE_WIRE_BUS 10
  7. OneWire oneWire(ONE_WIRE_BUS);
  8. #include <MCUFRIEND_kbv.h>
  9. MCUFRIEND_kbv tft;
  10. #include "TouchScreen.h"
  11. DS3231 Clock;
  12. bool Century=false;
  13. bool h12;
  14. bool PM;
  15. byte ADay, AHour, AMinute, ASecond, ABits;
  16. bool ADy, A12h, Apm;
  17. byte Year;
  18. byte Month;
  19. byte Date;
  20. byte DoW = 1;
  21. byte Hour = 2;
  22. byte Minute = 9;
  23. byte Second;
  24. byte ml;
  25. byte hl;
  26. byte dowl;
  27. int H;
  28. int M;
  29. int HP;
  30. int dw;
  31. int wsh = 13;
  32. int weh = 19;
  33. int offset = 1; //temp offset
  34.  
  35. int normTemp = 69;
  36. int workTemp = 65;
  37. int work;
  38. int relayState;
  39. int mPage = 1;
  40. int Select = 1;
  41.  
  42. //Screen set up
  43. #define BMPIMAGEOFFSET 54
  44. #define PALETTEDEPTH 8
  45. #define BUFFPIXEL 50
  46. #define YP A3 // must be an analog pin, use "An" notation!
  47. #define XM A2 // must be an analog pin, use "An" notation!
  48. #define YM 9 // can be a digital pin9 48
  49. #define XP 8 // can be a digital pin8 49
  50. #define MINPRESSURE 25
  51. #define MAXPRESSURE 1000
  52.  
  53. #define relay 12
  54. extern uint8_t SmallFont[5];
  55. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 335);
  56. #define BLACK 0x0000
  57. #define BLUE 0x001F
  58. #define RED 0xF800
  59. #define GREEN 0x07E0
  60. #define CYAN 0x07FF
  61. #define MAGENTA 0xF81F
  62. #define YELLOW 0xFFE0
  63. #define WHITE 0xFFFF
  64. float temp;
  65. float templs;
  66. bool resting = false;
  67. bool first = true;
  68. int restm;
  69. int resth;
  70. DallasTemperature sensors(&oneWire);
  71. DeviceAddress Probe = { 0x28, 0x4C, 0x1A, 0x08, 0x00, 0x00, 0x80, 0x47 };
  72.  
  73. void pTime(){
  74. H = Clock.getHour(h12, PM), DEC;
  75. M = Clock.getMinute(), DEC; //3=20 4=30
  76. dw = Clock.getDoW(), DEC;
  77.  
  78.  
  79. if(H >= wsh && H < weh && dw != 7 && dw != 1){
  80. work = 1;
  81. }else{ work = 0;}
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. void pinFix(){
  89. pinMode(XM, OUTPUT);
  90. digitalWrite(XM, LOW);
  91. pinMode(YP, OUTPUT);
  92. digitalWrite(YP, HIGH);
  93. pinMode(YM, OUTPUT);
  94. digitalWrite(YM, LOW);
  95. pinMode(XP, OUTPUT);
  96. digitalWrite(XP, HIGH);
  97. }
  98.  
  99. void gettemp(){
  100. sensors.requestTemperatures();
  101. temp = sensors.getTempF(Probe);
  102. if(resting == false && work == 1 && temp < (workTemp - offset)){
  103. digitalWrite(relay, HIGH); relayState = 1;
  104. if(first == true){
  105. if(60 > (M + 20)){resth = H; restm = M + 20; }
  106. if(60 < (M + 20) && H == 23){ resth = 0; restm = (M + 20) - 60;}
  107. if(H != 23 && 60 < (M + 20)){ resth = H + 1; restm = (M + 20) - 60;}
  108. first = false;}
  109. }
  110. if(resting == false && work == 0 && temp < (normTemp - offset)){
  111. digitalWrite(relay, HIGH); relayState = 1;
  112. if(first == true){
  113. if(60 > (M + 20)){resth = H; restm = M + 20; }
  114. if(60 < (M + 20) && H == 23){ resth = 0; restm = (M + 20) - 60;}
  115. if(H != 23 && 60 < (M + 20)){ resth = H + 1; restm = (M + 20) - 60;}
  116. first = false;}
  117. }
  118. if(work == 1 && temp > (workTemp + offset)){
  119. digitalWrite(relay, LOW); relayState = 0; first = true; resting = false;
  120. }
  121. if(work == 0 && temp > (normTemp + offset)){
  122. digitalWrite(relay, LOW); relayState = 0; first = true; resting = false;
  123. }
  124. if( relayState == 1 && resting == false && resth == H && M > restm){
  125. resting = true; digitalWrite(relay, LOW);
  126. if(60 > (M + 5)){resth = H; restm = M + 5; }
  127. if(60 < (M + 5) && H == 23){ resth = 0; restm = (M + 5) - 60;}
  128. if(H != 23 && 60 < (M + 5)){ resth = H + 1; restm = (M + 5) - 60;}
  129. }
  130. if( relayState == 1 && resting == true && resth == H && M > restm ){
  131. resting = false; first = true; digitalWrite(relay, HIGH);
  132. if(60 > (M + 20)){resth = H; restm = M + 20; }
  133. if(60 < (M + 20) && H == 23){ resth = 0; restm = (M + 20) - 60;}
  134. if(H != 23 && 60 < (M + 20)){ resth = H + 1; restm = (M + 20) - 60;}
  135. }
  136.  
  137.  
  138.  
  139. }
  140. void mainPage(){
  141. tft.fillScreen(BLACK);
  142. tft.setTextSize(4); //3=20 4=30
  143. tft.setTextColor(BLUE);
  144. tft.setCursor(70, 10);
  145. tft.print("Temp ");
  146. tft.setCursor(35, 65);
  147. tft.setTextSize(5);
  148. if(relayState == 1 && resting == false){ tft.setTextColor(GREEN);}
  149. if(relayState == 1 && resting == true){ tft.setTextColor(RED);}
  150. if(relayState == 0){ tft.setTextColor(BLUE);}
  151. tft.print(temp);
  152. tft.setCursor(195, 70);
  153. tft.setTextSize(4);
  154. tft.print("F");
  155. tft.setTextColor(BLUE);
  156. templs = temp;
  157. pTime();
  158. tft.setTextSize(4);
  159. tft.setTextColor(BLUE);
  160. tft.setCursor(30, 120);
  161.  
  162. HP = H;
  163. if(HP == 0){ HP = 12; }
  164. if(HP > 12){
  165.  
  166. HP = HP - 12;
  167. if(HP > 9){ tft.print(HP);}else{ tft.setCursor(60, 120); tft.print(HP);}
  168. }
  169.  
  170. else{ if(HP > 9){ tft.print(HP); }else{
  171. tft.setCursor(60,120); tft.print(HP);
  172. }}
  173. tft.setCursor(85, 120);
  174. tft.print(":");
  175.  
  176.  
  177. tft.setCursor(115, 120);
  178. if(M > 9){ tft.print(M);}else{ tft.print("0"); tft.setCursor(145, 120); tft.print(M);}
  179. ml = M;
  180. hl = H;
  181. tft.setCursor(180,125);
  182. tft.setTextSize(3);
  183. if(H == 0 || H >= 12){ tft.print("pm");}else{ tft.print("am");}
  184.  
  185. tft.setTextColor(BLUE);
  186. tft.setCursor(50, 160);
  187. switch(dw){
  188. case 2: tft.print("Monday"); break;
  189. case 3: tft.print("Tuesday"); break;
  190. case 4: tft.print("wednesday"); break;
  191. case 5: tft.print("Thursday"); break;
  192. case 6: tft.print("Firday"); break;
  193. case 7: tft.print("Saturday"); break;
  194. case 1: tft.print("Sunday"); break;
  195. }
  196. tft.setTextSize(2);
  197. tft.setCursor(5, 260);
  198. tft.print("Work time");
  199. tft.setCursor(140, 260);
  200. tft.print(wsh);
  201. tft.setCursor(170, 260);
  202. tft.print("-");
  203. tft.setCursor(190, 260);
  204. tft.print(weh);
  205. tft.setCursor(5, 280);
  206. tft.print("work temp");
  207. tft.setCursor(140, 280);
  208. tft.print(workTemp);
  209. tft.setCursor(5, 300);
  210. tft.print("norm temp");
  211. tft.setCursor(140, 300);
  212. tft.print(normTemp);
  213. tft.drawRoundRect(10,50,220,145, 5,RED);
  214.  
  215. }
  216. void setPage(){
  217. tft.fillScreen(BLACK);
  218. tft.drawLine(0,160, 240, 160, YELLOW);
  219. tft.drawLine(120,0,120,320,YELLOW);
  220. tft.setTextSize(3); //3=20 4=30
  221. tft.setTextColor(BLUE);
  222. tft.setCursor(0, 15);
  223. tft.print("Hour ");
  224. tft.setCursor(170, 15);
  225. tft.print(H);
  226. tft.setCursor(0, 45);
  227. tft.print("Minute ");
  228. tft.setCursor(170, 45);
  229. tft.print(M);
  230. tft.setCursor(0, 75);
  231. tft.print("DoW ");
  232. tft.setCursor(170, 75);
  233. tft.print(DoW);
  234. tft.setCursor(0, 105);
  235. tft.print("Work SH ");
  236. tft.setCursor(170, 105);
  237. tft.print(wsh);
  238. tft.setCursor(0, 135);
  239. tft.print("Work EH ");
  240. tft.setCursor(170, 135);
  241. tft.print(weh);
  242. tft.setCursor(0, 175);
  243. tft.print("offset ");
  244. tft.setCursor(170, 175);
  245. tft.print(offset);
  246. tft.setCursor(0, 205);
  247. tft.print("workTemp ");
  248. tft.setCursor(170, 205);
  249. tft.print(workTemp);
  250. tft.setCursor(0, 235);
  251. tft.print("normTemp ");
  252. tft.setCursor(170, 235);
  253. tft.print(normTemp);
  254. tft.setCursor(0, 275);
  255. tft.print("back to menu");
  256.  
  257.  
  258. tft.setTextColor(RED);
  259. switch(Select){
  260. case 1:
  261. tft.setCursor(220, 15);
  262. tft.print("]");
  263. break;
  264. case 2:
  265. tft.setCursor(220, 45);
  266. tft.print("]");
  267. break;
  268. case 3:
  269. tft.setCursor(220, 75);
  270. tft.print("]");
  271. break;
  272. case 4:
  273. tft.setCursor(220, 105);
  274. tft.print("]");
  275. break;
  276. case 5:
  277. tft.setCursor(220, 135);
  278. tft.print("]");
  279. break;
  280. case 6:
  281. tft.setCursor(220, 175);
  282. tft.print("]");
  283. break;
  284. case 7:
  285. tft.setCursor(220, 205);
  286. tft.print("]");
  287. break;
  288. case 8:
  289. tft.setCursor(220, 235);
  290. tft.print("]");
  291. break;
  292. case 9:
  293. tft.setCursor(220, 275);
  294. tft.print("]");
  295. break;
  296. }
  297. }
  298. void touch(){
  299. TSPoint p = ts.getPoint();
  300. pinFix();
  301.  
  302. if (p.z > MINPRESSURE && p.z < MAXPRESSURE && p.y > 100 && p.y < 800 && p.x > 100 && p.x < 800) {
  303. if(mPage == 1 ){
  304. pTime();
  305. mPage = 0; Select = 1; setPage();
  306. }
  307. if(mPage == 0 && p.y < 358 && Select < 9){
  308.  
  309. Select = Select + 1; setPage(); }
  310. if(mPage == 0 && p.y > 680 && Select > 1){
  311.  
  312. Select = Select - 1; setPage();}
  313.  
  314.  
  315. if(mPage == 0 && p.x > 358 && p.y < 680 && p.y > 358){ //down
  316.  
  317. switch(Select){
  318. case 1:
  319. Hour = Hour - 1;
  320. Clock.setHour(Hour);
  321. pTime();
  322. setPage();
  323. break;
  324. case 2:
  325. Minute = Minute - 1;
  326. Clock.setMinute(Minute);
  327. pTime();
  328. setPage();
  329. break;
  330. case 3:
  331. if(DoW > 1){ DoW = DoW - 1;}
  332. Clock.setDoW(DoW);
  333. pTime();
  334. setPage();
  335. break;
  336. case 4:
  337. wsh = wsh - 1;
  338. setPage();
  339. break;
  340. case 5:
  341. weh = weh - 1;
  342. setPage();
  343. break;
  344. case 6:
  345. offset = offset - 1;
  346. setPage();
  347. break;
  348. case 7:
  349. workTemp = workTemp - 1;
  350. setPage();
  351. break;
  352. case 8:
  353. normTemp = normTemp - 1;
  354. setPage();
  355. break;
  356. case 9:
  357. mPage = 1; mainPage();
  358. break;
  359. }
  360. }
  361. if(mPage == 0 && p.x < 358 && p.y < 680 && p.y > 358){ //up
  362.  
  363. switch(Select){
  364. case 1:
  365. Hour = Hour + 1;
  366. Clock.setHour(Hour);
  367. pTime();
  368. setPage();
  369. break;
  370. case 2:
  371. Minute = Minute + 1;
  372. Clock.setMinute(Minute);
  373. pTime();
  374. setPage();
  375. break;
  376. case 3:
  377. if(DoW < 7){ DoW = DoW + 1;}
  378. Clock.setDoW(DoW);
  379. pTime();
  380. setPage();
  381. break;
  382. case 4:
  383. wsh = wsh + 1;
  384. setPage();
  385. break;
  386. case 5:
  387. weh = weh + 1;
  388. setPage();
  389. break;
  390. case 6:
  391. offset = offset + 1;
  392. setPage();
  393. break;
  394. case 7:
  395. workTemp = workTemp + 1;
  396. setPage();
  397. break;
  398. case 8:
  399. normTemp = normTemp + 1;
  400. setPage();
  401. break;
  402. case 9:
  403. mPage = 1; mainPage();
  404. break;
  405. }
  406. }
  407. }}
  408.  
  409. void setup() {
  410. uint16_t ID;
  411. pinMode(relay, OUTPUT);
  412. Serial.begin(9600);
  413. ID = tft.readID();
  414. tft.begin(ID);
  415.  
  416. tft.setRotation(2);
  417. Wire.begin();
  418. sensors.begin();
  419. sensors.setResolution(Probe, 10);
  420. Clock.setClockMode(false);
  421. H = Clock.getHour(h12, PM), DEC;
  422. M = Clock.getMinute(), DEC; //3=20 4=30
  423. dw = Clock.getDoW(), DEC;
  424. mainPage();
  425.  
  426.  
  427.  
  428. }
  429.  
  430.  
  431.  
  432. void loop() {
  433.  
  434.  
  435.  
  436. if(mPage == 1){
  437. pTime();
  438. gettemp();
  439. if(H != hl || M != ml || temp != templs){ mainPage();}
  440. }
  441. touch();
  442. delay(200);
  443. }
Advertisement
Add Comment
Please, Sign In to add comment