Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include "Wire.h"
- #define DS3231_I2C_ADDRESS 0x68
- #define ONE_WIRE_BUS 7
- #define backLight 9
- #define relay 6
- #define upButton A1
- #define downButton A0
- int lightCount = 0;
- int HR = 0;
- int HRP = 0;
- int MN = 0;
- int workOnOff = 0;
- int normTemp = 60;
- int workTemp = 60;
- int workHourStart = 12;
- int workHourEnd = 18;
- float temp1 = 0;
- int up = 0;
- int upls = 0;
- int dn = 0;
- int dnls = 0;
- int timeCount = 0;
- int screen = 0;
- int sc1Spot = 0;
- int sc2Spot = 0;
- int DW = 0;
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- DeviceAddress Probe01 = {0x28, 0xD9, 0x8A, 0x52, 0x07, 0x00, 0x00, 0x9F };
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- // Convert normal decimal numbers to binary coded decimal
- byte decToBcd(byte val)
- {
- return( (val/10*16) + (val%10) );
- }
- // Convert binary coded decimal to normal decimal numbers
- byte bcdToDec(byte val)
- {
- return( (val/16*10) + (val%16) );
- }
- void setup()
- {
- Wire.begin();
- sensors.begin();
- sensors.setResolution(Probe01, 10);
- lcd.begin(16, 2);
- lcd.clear();
- // set the initial time here:
- // DS3231 seconds, minutes, hours, day, date, month, year
- setDS3231time(10,12,2,6,21,10,16);
- pinMode(backLight, OUTPUT);
- pinMode(upButton, INPUT);
- pinMode(downButton, INPUT);
- pinMode(relay,OUTPUT);
- digitalWrite(backLight, HIGH);
- }
- void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
- dayOfMonth, byte month, byte year)
- {
- // sets time and date data to DS3231
- Wire.beginTransmission(DS3231_I2C_ADDRESS);
- Wire.write(0); // set next input to start at the seconds register
- Wire.write(decToBcd(second)); // set seconds
- Wire.write(decToBcd(minute)); // set minutes
- Wire.write(decToBcd(hour)); // set hours
- Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
- Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
- Wire.write(decToBcd(month)); // set month
- Wire.write(decToBcd(year)); // set year (0 to 99)
- Wire.endTransmission();
- }
- void readDS3231time(byte *second,
- byte *minute,
- byte *hour,
- byte *dayOfWeek,
- byte *dayOfMonth,
- byte *month,
- byte *year)
- {
- Wire.beginTransmission(DS3231_I2C_ADDRESS);
- Wire.write(0); // set DS3231 register pointer to 00h
- Wire.endTransmission();
- Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
- // request seven bytes of data from DS3231 starting from register 00h
- *second = bcdToDec(Wire.read() & 0x7f);
- *minute = bcdToDec(Wire.read());
- *hour = bcdToDec(Wire.read() & 0x3f);
- *dayOfWeek = bcdToDec(Wire.read());
- *dayOfMonth = bcdToDec(Wire.read());
- *month = bcdToDec(Wire.read());
- *year = bcdToDec(Wire.read());
- }
- void getTime()
- {
- byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- // retrieve data from DS3231
- readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
- &year);
- // send it to the serial monitor
- HR = hour;
- MN = minute;
- DW = dayOfWeek;
- if(HR >= workHourStart && HR < workHourEnd && DW > 1 && DW < 7){
- workOnOff = 1;
- }else { workOnOff = 0;
- }
- }
- void screenPick(){
- if(screen == 0){
- screen0();
- }
- if(screen == 1){
- screen1();
- }
- if(screen == 2){
- screen2();
- }
- if(lightCount > 100 && screen == 0){
- digitalWrite(backLight, LOW);
- }
- if(lightCount == 0){
- digitalWrite(backLight, HIGH);
- }
- }
- void screen0(){
- if(HR > 12){
- HRP = HR - 12;
- while(HRP < 10){
- lcd.setCursor(0, 0);
- lcd.print("0");
- lcd.setCursor(1, 0);
- lcd.print(HRP);
- break;
- }
- while(HRP > 9){
- lcd.setCursor(0, 0);
- lcd.print(HRP);
- break;
- }
- }else {
- while(HR < 10){
- lcd.setCursor(0, 0);
- lcd.print("0");
- lcd.setCursor(1, 0);
- lcd.print(HR);
- break;
- }
- while(HR > 9){
- lcd.setCursor(0, 0);
- lcd.print(HR);
- break;
- }
- }
- lcd.setCursor(2, 0);
- lcd.print(":");
- if(MN < 10){
- lcd.setCursor(3, 0);
- lcd.print("0");
- lcd.setCursor(4, 0);
- lcd.print(MN);
- }else {
- lcd.print(MN);}
- lcd.setCursor(0, 1);
- lcd.print(temp1);
- lcd.setCursor(9, 0);
- lcd.print("norm");
- lcd.setCursor(14, 0);
- lcd.print(normTemp);
- lcd.setCursor(9, 1);
- lcd.print("work");
- lcd.setCursor(14, 1);
- lcd.print(workTemp);
- up = digitalRead(upButton);
- dn = digitalRead(downButton);
- if(up != upls && dn != dnls && up == dn){
- if(up == HIGH){
- lightCount = 0;
- lcd.clear();
- screen = 1;
- }
- }
- if(up != upls && up != dn){
- if(up == HIGH){
- lightCount = 0;
- lcd.clear();
- screenPick()
- }
- }
- if(dn != dnls && dn != up){
- if(dn == HIGH){
- lightCount = 0;
- lcd.clear();
- screenPick()
- }
- }
- upls = up;
- dnls = dn;
- }
- void screen1(){
- lcd.setCursor(0, 0);
- lcd.print("setNorm setWork");
- if(sc1Spot == 0){
- lcd.setCursor(0, 1);
- lcd.print(">");
- }
- lcd.setCursor(1, 1);
- lcd.print(normTemp);
- lcd.setCursor(5, 1);
- lcd.print("Temp");
- lcd.setCursor(13, 1);
- lcd.print(workTemp);
- up = digitalRead(upButton);
- dn = digitalRead(downButton);
- if(up != upls && dn != dnls && up == dn){
- if(up == HIGH){
- lightCount = 0;
- if(sc1Spot == 0){
- sc1Spot = 1;
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(12, 1);
- lcd.print(">");
- }else{
- lcd.clear();
- screen = 2;
- sc1Spot = 0;
- }
- }
- }
- if(sc1Spot == 0){
- if(up != upls && up != dn){
- if(up == HIGH){
- lightCount = 0;
- normTemp = normTemp + 1;
- }
- }
- if(dn != dnls && dn != up){
- if(dn == HIGH){
- lightCount = 0;
- normTemp = normTemp - 1;
- }
- }
- }
- if(sc1Spot == 1){
- if(up != upls && up != dn){
- if(up == HIGH){
- lightCount = 0;
- workTemp = workTemp + 1;
- }
- }
- if(dn != dnls && dn != up){
- if(dn == HIGH){
- lightCount = 0;
- workTemp = workTemp - 1;
- }
- }
- }
- upls = up;
- dnls = dn;
- }
- void screen2(){
- lcd.setCursor(0, 0);
- lcd.print("set work time");
- if(sc2Spot == 0){
- lcd.setCursor(0, 1);
- lcd.print(">");
- }
- lcd.setCursor(1, 1);
- lcd.print(workHourStart);
- lcd.setCursor(3, 1);
- lcd.print("start");
- lcd.setCursor(10, 1);
- lcd.print("end");
- lcd.setCursor(14, 1);
- lcd.print(workHourEnd);
- up = digitalRead(upButton);
- dn = digitalRead(downButton);
- if(up != upls && dn != dnls && up == dn){
- if(up == HIGH){
- lightCount = 0;
- if(sc2Spot == 0){
- sc2Spot = 1;
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(13, 1);
- lcd.print(">");
- } else{
- lcd.clear();
- screen = 0;
- sc2Spot = 0;
- }
- }
- }
- if(sc2Spot == 0){
- if(up != upls && up != dn){
- if(up == HIGH){
- lightCount = 0;
- workHourStart = workHourStart + 1;
- }
- }
- if(dn != dnls && dn != up){
- if(dn == HIGH){
- lightCount = 0;
- workHourStart = workHourStart - 1;
- }
- }
- }
- if(sc2Spot == 1){
- if(up != upls && up != dn){
- if(up == HIGH){
- lightCount = 0;
- workHourEnd = workHourEnd + 1;
- }
- }
- if(dn != dnls && dn != up){
- if(dn == HIGH){
- lightCount = 0;
- workHourEnd = workHourEnd - 1;
- }
- }
- }
- upls = up;
- dnls = dn;
- }
- void checkRelay(){
- if(workOnOff == 1){
- if(temp1 < (workTemp - 1)) {
- digitalWrite(relay, HIGH);
- }
- if(temp1 >= (workTemp + 1)) {
- digitalWrite(relay, LOW);
- }
- }
- if(workOnOff == 0){
- if(temp1 < (normTemp -1)) {
- digitalWrite(relay, HIGH);
- }
- if(temp1 >= (normTemp + 1)) {
- digitalWrite(relay, LOW);
- }
- }
- }
- void getTemp(){
- sensors.requestTemperatures();
- temp1 = sensors.getTempF(Probe01);
- }
- void loop(){
- if(timeCount > 19){
- getTime(); // display the real-time clock data on the Serial Monitor,
- getTemp();
- timeCount = 0;
- checkRelay();
- }
- screenPick();
- delay(50);
- lightCount = lightCount + 1;
- timeCount = timeCount + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment