yellowsnow2

Furnace Thermostat

Oct 28th, 2016
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #include "Wire.h"
  5. #define DS3231_I2C_ADDRESS 0x68
  6. #define ONE_WIRE_BUS 7
  7. #define backLight 9
  8. #define relay 6
  9. #define upButton A1
  10. #define downButton A0
  11. int lightCount = 0;
  12. int HR = 0;
  13. int HRP = 0;
  14. int MN = 0;
  15. int workOnOff = 0;
  16. int normTemp = 60;
  17. int workTemp = 60;
  18. int workHourStart = 12;
  19. int workHourEnd = 18;
  20. float temp1 = 0;
  21. int up = 0;
  22. int upls = 0;
  23. int dn = 0;
  24. int dnls = 0;
  25. int timeCount = 0;
  26. int screen = 0;
  27. int sc1Spot = 0;
  28. int sc2Spot = 0;
  29. int DW = 0;
  30.  
  31. OneWire oneWire(ONE_WIRE_BUS);
  32. DallasTemperature sensors(&oneWire);
  33. DeviceAddress Probe01 = {0x28, 0xD9, 0x8A, 0x52, 0x07, 0x00, 0x00, 0x9F };
  34. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  35. // Convert normal decimal numbers to binary coded decimal
  36. byte decToBcd(byte val)
  37. {
  38.   return( (val/10*16) + (val%10) );
  39. }
  40. // Convert binary coded decimal to normal decimal numbers
  41. byte bcdToDec(byte val)
  42. {
  43.   return( (val/16*10) + (val%16) );
  44. }
  45.  
  46.  
  47. void setup()
  48. {
  49.   Wire.begin();
  50.   sensors.begin();
  51.   sensors.setResolution(Probe01, 10);
  52.   lcd.begin(16, 2);
  53.   lcd.clear();
  54.   // set the initial time here:
  55.   // DS3231 seconds, minutes, hours, day, date, month, year
  56.  setDS3231time(10,12,2,6,21,10,16);
  57.  pinMode(backLight, OUTPUT);
  58.  pinMode(upButton, INPUT);
  59.  pinMode(downButton, INPUT);
  60.  pinMode(relay,OUTPUT);
  61.  digitalWrite(backLight, HIGH);
  62. }
  63.  
  64.  
  65. void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
  66. dayOfMonth, byte month, byte year)
  67. {
  68.   // sets time and date data to DS3231
  69.   Wire.beginTransmission(DS3231_I2C_ADDRESS);
  70.   Wire.write(0); // set next input to start at the seconds register
  71.   Wire.write(decToBcd(second)); // set seconds
  72.   Wire.write(decToBcd(minute)); // set minutes
  73.   Wire.write(decToBcd(hour)); // set hours
  74.   Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  75.   Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  76.   Wire.write(decToBcd(month)); // set month
  77.   Wire.write(decToBcd(year)); // set year (0 to 99)
  78.   Wire.endTransmission();
  79. }
  80. void readDS3231time(byte *second,
  81. byte *minute,
  82. byte *hour,
  83. byte *dayOfWeek,
  84. byte *dayOfMonth,
  85. byte *month,
  86. byte *year)
  87. {
  88.   Wire.beginTransmission(DS3231_I2C_ADDRESS);
  89.   Wire.write(0); // set DS3231 register pointer to 00h
  90.   Wire.endTransmission();
  91.   Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  92.   // request seven bytes of data from DS3231 starting from register 00h
  93.   *second = bcdToDec(Wire.read() & 0x7f);
  94.   *minute = bcdToDec(Wire.read());
  95.   *hour = bcdToDec(Wire.read() & 0x3f);
  96.   *dayOfWeek = bcdToDec(Wire.read());
  97.   *dayOfMonth = bcdToDec(Wire.read());
  98.   *month = bcdToDec(Wire.read());
  99.   *year = bcdToDec(Wire.read());
  100. }
  101. void getTime()
  102. {
  103.   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  104.   // retrieve data from DS3231
  105.   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  106.   &year);
  107.   // send it to the serial monitor
  108.   HR = hour;
  109.   MN = minute;
  110.   DW = dayOfWeek;
  111.   if(HR >= workHourStart && HR < workHourEnd && DW > 1 && DW < 7){
  112.      workOnOff = 1;
  113.   }else { workOnOff = 0;
  114.   }
  115.  
  116.  
  117. }
  118. void screenPick(){
  119.  
  120.   if(screen == 0){
  121.     screen0();  
  122.   }
  123.   if(screen == 1){
  124.     screen1();  
  125.   }
  126.   if(screen == 2){
  127.     screen2();  
  128.   }
  129.   if(lightCount > 100 && screen == 0){
  130.     digitalWrite(backLight, LOW);
  131.   }
  132.   if(lightCount == 0){
  133.     digitalWrite(backLight, HIGH);
  134.   }
  135.   }
  136.  
  137. void screen0(){  
  138.  
  139.  
  140.   if(HR > 12){
  141.     HRP = HR - 12;
  142.     while(HRP < 10){
  143.       lcd.setCursor(0, 0);
  144.       lcd.print("0");
  145.       lcd.setCursor(1, 0);
  146.       lcd.print(HRP);
  147.       break;
  148.     }
  149.     while(HRP > 9){
  150.     lcd.setCursor(0, 0);  
  151.     lcd.print(HRP);
  152.     break;
  153.     }
  154.   }else {
  155.       while(HR < 10){
  156.       lcd.setCursor(0, 0);  
  157.       lcd.print("0");
  158.       lcd.setCursor(1, 0);
  159.       lcd.print(HR);
  160.       break;
  161.     }
  162.     while(HR > 9){
  163.     lcd.setCursor(0, 0);  
  164.     lcd.print(HR);
  165.     break;
  166.     }
  167.    
  168.   }
  169.      
  170.   lcd.setCursor(2, 0);
  171.   lcd.print(":");  
  172.  
  173.   if(MN < 10){
  174.    lcd.setCursor(3, 0);  
  175.       lcd.print("0");
  176.       lcd.setCursor(4, 0);
  177.       lcd.print(MN);
  178.   }else {
  179.  
  180.  
  181.   lcd.print(MN);}
  182.  
  183.   lcd.setCursor(0, 1);
  184.   lcd.print(temp1);
  185.   lcd.setCursor(9, 0);
  186.   lcd.print("norm");
  187.   lcd.setCursor(14, 0);
  188.   lcd.print(normTemp);
  189.   lcd.setCursor(9, 1);
  190.   lcd.print("work");
  191.   lcd.setCursor(14, 1);
  192.   lcd.print(workTemp);
  193.  
  194.  
  195.  
  196.   up = digitalRead(upButton);
  197.   dn = digitalRead(downButton);
  198.  
  199.   if(up != upls && dn != dnls && up == dn){
  200.     if(up == HIGH){
  201.       lightCount = 0;
  202.       lcd.clear();
  203.       screen = 1;
  204.     }
  205.   }
  206.   if(up != upls && up != dn){
  207.       if(up == HIGH){
  208.         lightCount = 0;
  209.         lcd.clear();
  210.         screenPick()
  211.         }
  212.     }
  213.     if(dn != dnls && dn != up){
  214.       if(dn == HIGH){
  215.         lightCount = 0;
  216.         lcd.clear();
  217.         screenPick()
  218.        }
  219.     }
  220.   upls = up;
  221.   dnls = dn;
  222. }
  223.  
  224.  
  225. void screen1(){
  226.  
  227.   lcd.setCursor(0, 0);
  228.   lcd.print("setNorm  setWork");
  229.   if(sc1Spot == 0){
  230.    lcd.setCursor(0, 1);
  231.    lcd.print(">");
  232.   }
  233.   lcd.setCursor(1, 1);
  234.   lcd.print(normTemp);
  235.   lcd.setCursor(5, 1);
  236.   lcd.print("Temp");
  237.   lcd.setCursor(13, 1);
  238.   lcd.print(workTemp);
  239.   up = digitalRead(upButton);
  240.   dn = digitalRead(downButton);
  241.  
  242.   if(up != upls && dn != dnls && up == dn){
  243.     if(up == HIGH){
  244.       lightCount = 0;
  245.       if(sc1Spot == 0){
  246.       sc1Spot = 1;
  247.       lcd.setCursor(0, 1);
  248.       lcd.print(" ");
  249.       lcd.setCursor(12, 1);
  250.       lcd.print(">");
  251.       }else{
  252.         lcd.clear();
  253.         screen = 2;
  254.         sc1Spot = 0;
  255.      }
  256.     }
  257.   }
  258.  
  259.   if(sc1Spot == 0){
  260.     if(up != upls && up != dn){
  261.       if(up == HIGH){
  262.         lightCount = 0;
  263.         normTemp = normTemp + 1;
  264.       }
  265.     }
  266.     if(dn != dnls && dn != up){
  267.       if(dn == HIGH){
  268.         lightCount = 0;
  269.         normTemp = normTemp - 1;
  270.       }
  271.     }
  272.   }
  273.  
  274.   if(sc1Spot == 1){
  275.     if(up != upls && up != dn){
  276.       if(up == HIGH){
  277.         lightCount = 0;
  278.         workTemp = workTemp + 1;
  279.       }
  280.     }
  281.     if(dn != dnls && dn != up){
  282.       if(dn == HIGH){
  283.         lightCount = 0;
  284.         workTemp = workTemp - 1;
  285.       }
  286.     }
  287.   }
  288.  
  289.   upls = up;
  290.   dnls = dn;
  291. }
  292.  
  293. void screen2(){
  294.  
  295.   lcd.setCursor(0, 0);
  296.   lcd.print("set work time");
  297.   if(sc2Spot == 0){
  298.     lcd.setCursor(0, 1);
  299.     lcd.print(">");
  300.   }
  301.   lcd.setCursor(1, 1);
  302.   lcd.print(workHourStart);
  303.   lcd.setCursor(3, 1);
  304.   lcd.print("start");
  305.   lcd.setCursor(10, 1);
  306.   lcd.print("end");
  307.   lcd.setCursor(14, 1);
  308.   lcd.print(workHourEnd);
  309.   up = digitalRead(upButton);
  310.   dn = digitalRead(downButton);
  311.  
  312.   if(up != upls && dn != dnls && up == dn){
  313.     if(up == HIGH){
  314.       lightCount = 0;
  315.       if(sc2Spot == 0){
  316.       sc2Spot = 1;
  317.       lcd.setCursor(0, 1);
  318.       lcd.print(" ");
  319.       lcd.setCursor(13, 1);
  320.       lcd.print(">");
  321.       } else{
  322.         lcd.clear();
  323.         screen = 0;
  324.         sc2Spot = 0;
  325.      }
  326.     }
  327.   }
  328.  
  329.   if(sc2Spot == 0){
  330.     if(up != upls && up != dn){
  331.       if(up == HIGH){
  332.         lightCount = 0;
  333.         workHourStart = workHourStart + 1;
  334.       }
  335.     }
  336.     if(dn != dnls && dn != up){
  337.       if(dn == HIGH){
  338.         lightCount = 0;
  339.         workHourStart = workHourStart - 1;
  340.       }
  341.     }
  342.   }
  343.  
  344.   if(sc2Spot == 1){
  345.     if(up != upls && up != dn){
  346.       if(up == HIGH){
  347.         lightCount = 0;
  348.         workHourEnd = workHourEnd + 1;
  349.       }
  350.     }
  351.     if(dn != dnls && dn != up){
  352.       if(dn == HIGH){
  353.         lightCount = 0;
  354.         workHourEnd = workHourEnd - 1;
  355.       }
  356.     }
  357.   }
  358.  
  359.   upls = up;
  360.   dnls = dn;
  361.  
  362. }
  363.  
  364.  
  365. void checkRelay(){
  366.   if(workOnOff == 1){
  367.       if(temp1 < (workTemp - 1)) {
  368.       digitalWrite(relay, HIGH);
  369.      
  370.     }
  371.     if(temp1 >= (workTemp + 1)) {
  372.       digitalWrite(relay, LOW);
  373.      
  374.     }
  375.   }
  376.  
  377.   if(workOnOff == 0){
  378.     if(temp1 < (normTemp -1)) {
  379.       digitalWrite(relay, HIGH);
  380.      
  381.     }
  382.     if(temp1 >= (normTemp + 1)) {
  383.       digitalWrite(relay, LOW);
  384.      
  385.    
  386.   }
  387. }
  388. }
  389.  
  390.  
  391.  
  392.  
  393. void getTemp(){
  394.   sensors.requestTemperatures();
  395. temp1 = sensors.getTempF(Probe01);
  396.  
  397. }
  398.  
  399.  
  400. void loop(){
  401.  
  402.   if(timeCount > 19){
  403.     getTime(); // display the real-time clock data on the Serial Monitor,
  404.     getTemp();
  405.     timeCount = 0;
  406.     checkRelay();
  407.   }
  408.  
  409.  
  410.   screenPick();
  411.   delay(50);
  412.   lightCount = lightCount + 1;
  413.   timeCount = timeCount + 1;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment