Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DS3231.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #define ONE_WIRE_BUS 10
- OneWire oneWire(ONE_WIRE_BUS);
- #include <MCUFRIEND_kbv.h>
- MCUFRIEND_kbv tft;
- #include "TouchScreen.h"
- DS3231 Clock;
- bool Century=false;
- bool h12;
- bool PM;
- byte ADay, AHour, AMinute, ASecond, ABits;
- bool ADy, A12h, Apm;
- byte Year;
- byte Month;
- byte Date;
- byte DoW = 1;
- byte Hour = 2;
- byte Minute = 9;
- byte Second;
- byte ml;
- byte hl;
- byte dowl;
- int H;
- int M;
- int HP;
- int dw;
- int wsh = 13;
- int weh = 19;
- int offset = 1; //temp offset
- int normTemp = 69;
- int workTemp = 65;
- int work;
- int relayState;
- int mPage = 1;
- int Select = 1;
- //Screen set up
- #define BMPIMAGEOFFSET 54
- #define PALETTEDEPTH 8
- #define BUFFPIXEL 50
- #define YP A3 // must be an analog pin, use "An" notation!
- #define XM A2 // must be an analog pin, use "An" notation!
- #define YM 9 // can be a digital pin9 48
- #define XP 8 // can be a digital pin8 49
- #define MINPRESSURE 25
- #define MAXPRESSURE 1000
- #define relay 12
- extern uint8_t SmallFont[5];
- TouchScreen ts = TouchScreen(XP, YP, XM, YM, 335);
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define WHITE 0xFFFF
- float temp;
- float templs;
- bool resting = false;
- bool first = true;
- int restm;
- int resth;
- DallasTemperature sensors(&oneWire);
- DeviceAddress Probe = { 0x28, 0x4C, 0x1A, 0x08, 0x00, 0x00, 0x80, 0x47 };
- void pTime(){
- H = Clock.getHour(h12, PM), DEC;
- M = Clock.getMinute(), DEC; //3=20 4=30
- dw = Clock.getDoW(), DEC;
- if(H >= wsh && H < weh && dw != 7 && dw != 1){
- work = 1;
- }else{ work = 0;}
- }
- void pinFix(){
- pinMode(XM, OUTPUT);
- digitalWrite(XM, LOW);
- pinMode(YP, OUTPUT);
- digitalWrite(YP, HIGH);
- pinMode(YM, OUTPUT);
- digitalWrite(YM, LOW);
- pinMode(XP, OUTPUT);
- digitalWrite(XP, HIGH);
- }
- void gettemp(){
- sensors.requestTemperatures();
- temp = sensors.getTempF(Probe);
- if(resting == false && work == 1 && temp < (workTemp - offset)){
- digitalWrite(relay, HIGH); relayState = 1;
- if(first == true){
- if(60 > (M + 20)){resth = H; restm = M + 20; }
- if(60 < (M + 20) && H == 23){ resth = 0; restm = (M + 20) - 60;}
- if(H != 23 && 60 < (M + 20)){ resth = H + 1; restm = (M + 20) - 60;}
- first = false;}
- }
- if(resting == false && work == 0 && temp < (normTemp - offset)){
- digitalWrite(relay, HIGH); relayState = 1;
- if(first == true){
- if(60 > (M + 20)){resth = H; restm = M + 20; }
- if(60 < (M + 20) && H == 23){ resth = 0; restm = (M + 20) - 60;}
- if(H != 23 && 60 < (M + 20)){ resth = H + 1; restm = (M + 20) - 60;}
- first = false;}
- }
- if(work == 1 && temp > (workTemp + offset)){
- digitalWrite(relay, LOW); relayState = 0; first = true; resting = false;
- }
- if(work == 0 && temp > (normTemp + offset)){
- digitalWrite(relay, LOW); relayState = 0; first = true; resting = false;
- }
- if( relayState == 1 && resting == false && resth == H && M > restm){
- resting = true; digitalWrite(relay, LOW);
- if(60 > (M + 5)){resth = H; restm = M + 5; }
- if(60 < (M + 5) && H == 23){ resth = 0; restm = (M + 5) - 60;}
- if(H != 23 && 60 < (M + 5)){ resth = H + 1; restm = (M + 5) - 60;}
- }
- if( relayState == 1 && resting == true && resth == H && M > restm ){
- resting = false; first = true; digitalWrite(relay, HIGH);
- if(60 > (M + 20)){resth = H; restm = M + 20; }
- if(60 < (M + 20) && H == 23){ resth = 0; restm = (M + 20) - 60;}
- if(H != 23 && 60 < (M + 20)){ resth = H + 1; restm = (M + 20) - 60;}
- }
- }
- void mainPage(){
- tft.fillScreen(BLACK);
- tft.setTextSize(4); //3=20 4=30
- tft.setTextColor(BLUE);
- tft.setCursor(70, 10);
- tft.print("Temp ");
- tft.setCursor(35, 65);
- tft.setTextSize(5);
- if(relayState == 1 && resting == false){ tft.setTextColor(GREEN);}
- if(relayState == 1 && resting == true){ tft.setTextColor(RED);}
- if(relayState == 0){ tft.setTextColor(BLUE);}
- tft.print(temp);
- tft.setCursor(195, 70);
- tft.setTextSize(4);
- tft.print("F");
- tft.setTextColor(BLUE);
- templs = temp;
- pTime();
- tft.setTextSize(4);
- tft.setTextColor(BLUE);
- tft.setCursor(30, 120);
- HP = H;
- if(HP == 0){ HP = 12; }
- if(HP > 12){
- HP = HP - 12;
- if(HP > 9){ tft.print(HP);}else{ tft.setCursor(60, 120); tft.print(HP);}
- }
- else{ if(HP > 9){ tft.print(HP); }else{
- tft.setCursor(60,120); tft.print(HP);
- }}
- tft.setCursor(85, 120);
- tft.print(":");
- tft.setCursor(115, 120);
- if(M > 9){ tft.print(M);}else{ tft.print("0"); tft.setCursor(145, 120); tft.print(M);}
- ml = M;
- hl = H;
- tft.setCursor(180,125);
- tft.setTextSize(3);
- if(H == 0 || H >= 12){ tft.print("pm");}else{ tft.print("am");}
- tft.setTextColor(BLUE);
- tft.setCursor(50, 160);
- switch(dw){
- case 2: tft.print("Monday"); break;
- case 3: tft.print("Tuesday"); break;
- case 4: tft.print("wednesday"); break;
- case 5: tft.print("Thursday"); break;
- case 6: tft.print("Firday"); break;
- case 7: tft.print("Saturday"); break;
- case 1: tft.print("Sunday"); break;
- }
- tft.setTextSize(2);
- tft.setCursor(5, 260);
- tft.print("Work time");
- tft.setCursor(140, 260);
- tft.print(wsh);
- tft.setCursor(170, 260);
- tft.print("-");
- tft.setCursor(190, 260);
- tft.print(weh);
- tft.setCursor(5, 280);
- tft.print("work temp");
- tft.setCursor(140, 280);
- tft.print(workTemp);
- tft.setCursor(5, 300);
- tft.print("norm temp");
- tft.setCursor(140, 300);
- tft.print(normTemp);
- tft.drawRoundRect(10,50,220,145, 5,RED);
- }
- void setPage(){
- tft.fillScreen(BLACK);
- tft.drawLine(0,160, 240, 160, YELLOW);
- tft.drawLine(120,0,120,320,YELLOW);
- tft.setTextSize(3); //3=20 4=30
- tft.setTextColor(BLUE);
- tft.setCursor(0, 15);
- tft.print("Hour ");
- tft.setCursor(170, 15);
- tft.print(H);
- tft.setCursor(0, 45);
- tft.print("Minute ");
- tft.setCursor(170, 45);
- tft.print(M);
- tft.setCursor(0, 75);
- tft.print("DoW ");
- tft.setCursor(170, 75);
- tft.print(DoW);
- tft.setCursor(0, 105);
- tft.print("Work SH ");
- tft.setCursor(170, 105);
- tft.print(wsh);
- tft.setCursor(0, 135);
- tft.print("Work EH ");
- tft.setCursor(170, 135);
- tft.print(weh);
- tft.setCursor(0, 175);
- tft.print("offset ");
- tft.setCursor(170, 175);
- tft.print(offset);
- tft.setCursor(0, 205);
- tft.print("workTemp ");
- tft.setCursor(170, 205);
- tft.print(workTemp);
- tft.setCursor(0, 235);
- tft.print("normTemp ");
- tft.setCursor(170, 235);
- tft.print(normTemp);
- tft.setCursor(0, 275);
- tft.print("back to menu");
- tft.setTextColor(RED);
- switch(Select){
- case 1:
- tft.setCursor(220, 15);
- tft.print("]");
- break;
- case 2:
- tft.setCursor(220, 45);
- tft.print("]");
- break;
- case 3:
- tft.setCursor(220, 75);
- tft.print("]");
- break;
- case 4:
- tft.setCursor(220, 105);
- tft.print("]");
- break;
- case 5:
- tft.setCursor(220, 135);
- tft.print("]");
- break;
- case 6:
- tft.setCursor(220, 175);
- tft.print("]");
- break;
- case 7:
- tft.setCursor(220, 205);
- tft.print("]");
- break;
- case 8:
- tft.setCursor(220, 235);
- tft.print("]");
- break;
- case 9:
- tft.setCursor(220, 275);
- tft.print("]");
- break;
- }
- }
- void touch(){
- TSPoint p = ts.getPoint();
- pinFix();
- if (p.z > MINPRESSURE && p.z < MAXPRESSURE && p.y > 100 && p.y < 800 && p.x > 100 && p.x < 800) {
- if(mPage == 1 ){
- pTime();
- mPage = 0; Select = 1; setPage();
- }
- if(mPage == 0 && p.y < 358 && Select < 9){
- Select = Select + 1; setPage(); }
- if(mPage == 0 && p.y > 680 && Select > 1){
- Select = Select - 1; setPage();}
- if(mPage == 0 && p.x > 358 && p.y < 680 && p.y > 358){ //down
- switch(Select){
- case 1:
- Hour = Hour - 1;
- Clock.setHour(Hour);
- pTime();
- setPage();
- break;
- case 2:
- Minute = Minute - 1;
- Clock.setMinute(Minute);
- pTime();
- setPage();
- break;
- case 3:
- if(DoW > 1){ DoW = DoW - 1;}
- Clock.setDoW(DoW);
- pTime();
- setPage();
- break;
- case 4:
- wsh = wsh - 1;
- setPage();
- break;
- case 5:
- weh = weh - 1;
- setPage();
- break;
- case 6:
- offset = offset - 1;
- setPage();
- break;
- case 7:
- workTemp = workTemp - 1;
- setPage();
- break;
- case 8:
- normTemp = normTemp - 1;
- setPage();
- break;
- case 9:
- mPage = 1; mainPage();
- break;
- }
- }
- if(mPage == 0 && p.x < 358 && p.y < 680 && p.y > 358){ //up
- switch(Select){
- case 1:
- Hour = Hour + 1;
- Clock.setHour(Hour);
- pTime();
- setPage();
- break;
- case 2:
- Minute = Minute + 1;
- Clock.setMinute(Minute);
- pTime();
- setPage();
- break;
- case 3:
- if(DoW < 7){ DoW = DoW + 1;}
- Clock.setDoW(DoW);
- pTime();
- setPage();
- break;
- case 4:
- wsh = wsh + 1;
- setPage();
- break;
- case 5:
- weh = weh + 1;
- setPage();
- break;
- case 6:
- offset = offset + 1;
- setPage();
- break;
- case 7:
- workTemp = workTemp + 1;
- setPage();
- break;
- case 8:
- normTemp = normTemp + 1;
- setPage();
- break;
- case 9:
- mPage = 1; mainPage();
- break;
- }
- }
- }}
- void setup() {
- uint16_t ID;
- pinMode(relay, OUTPUT);
- Serial.begin(9600);
- ID = tft.readID();
- tft.begin(ID);
- tft.setRotation(2);
- Wire.begin();
- sensors.begin();
- sensors.setResolution(Probe, 10);
- Clock.setClockMode(false);
- H = Clock.getHour(h12, PM), DEC;
- M = Clock.getMinute(), DEC; //3=20 4=30
- dw = Clock.getDoW(), DEC;
- mainPage();
- }
- void loop() {
- if(mPage == 1){
- pTime();
- gettemp();
- if(H != hl || M != ml || temp != templs){ mainPage();}
- }
- touch();
- delay(200);
- }
Advertisement
Add Comment
Please, Sign In to add comment