#include Arduino.h #include Wire.h #include "DS1307.h" #include "LiquidCrystal.h" #include "iddht11.h"

/* * * * * * * * * * * * */ Thermostat R 0.1 16.11.2013 Project by: Georg Tornqvist Connection spec: LCD lcd(12, 11, 10, 7, 8, 9, 6); RELAY (D4) RTC ...
Author: Dora Charles
0 downloads 0 Views 38KB Size
/* * * * * * * * * * * * */

Thermostat R 0.1 16.11.2013 Project by: Georg Tornqvist Connection spec: LCD lcd(12, 11, 10, 7, 8, 9, 6); RELAY (D4) RTC i2C (A4,A5) DHT11 temp, hum (D2) 10K pot (A2) RGB LED (D3 green, D5 red) LCD backlight (D13)

#include #include #include #include #include

“Arduino.h” “Wire.h” "DS1307.h" "LiquidCrystal.h" "idDHT11.h"

int relay = 4; int pot_pin = 1; int red_led = 5; int green_led = 3; int lcd_bkl = 13; float act_temp = 0.0; float set_temp = 15.0; int hum = 0; int last_time = 0; int hys = 1; int night_temp = 17; int prev_temp = 0; int prev_time = 0; int delay_set = 5; byte degree[8] = { B01000, B10100, B01000, B00011, B00100, B00100, B00011, }; //INITS LiquidCrystal lcd(12, 11, 10, 7, 8, 9, 6); //LCD init int idDHT11pin = 2; //Digital pin for comunications DHT11 int idDHT11intNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above) DHT11 void dht11_wrapper(); // must be declared before the lib initialization idDHT11 DHT11(idDHT11pin,idDHT11intNumber,dht11_wrapper); DS1307 clock; //define a object of DS1307 class

void setup() { Serial.begin(9600); pinMode(relay, OUTPUT); pinMode(green_led, OUTPUT); pinMode(red_led, OUTPUT); pinMode(lcd_bkl, OUTPUT); clock.begin(); lcd.createChar(0, degree); /* *******************SET DATE-TIME****************** clock.fillByYMD(2013,11,20);//Jan 19,2013 clock.fillByHMS(02,03,00);//15:28 30" clock.fillDayOfWeek(TUE);//Saturday clock.setTime();//write time to the RTC chip // ****************************************************/

}

lcd.begin(16, 2); lcd.setCursor(0,0);

// start the LCD library // set cursor to 0 pos

void dht11_wrapper() //necessary to initialize the idDHT11 library { DHT11.isrCallback(); } void stop_pump() //turn off the relay and set the LED to RED { digitalWrite(relay, 0); analogWrite(red_led, 255); analogWrite(green_led, 0); } void start_pump() //turn on the relay and set the LED to GREEN { digitalWrite(relay, 1); analogWrite(red_led, 0); analogWrite(green_led, 255); } void print_LCD() //Print status on 16x2 LCD display { //line0 lcd.setCursor(0,0); lcd.print(act_temp, 0); lcd.setCursor(2,0); lcd.write(byte(0)); lcd.setCursor(4,0); lcd.print("|"); lcd.setCursor(5,0); if(clock.hour < 10) // print 0 before hour < 10 { lcd.print("0"); lcd.setCursor(6,0); lcd.print(clock.hour, DEC); } else (lcd.print(clock.hour, DEC)); lcd.setCursor(7,0); lcd.print(":"); lcd.setCursor(8,0); if(clock.minute < 10) // print 0 before minutes < 10

{ lcd.print("0"); lcd.setCursor(9,0); lcd.print(clock.minute, DEC);

} else (lcd.print(clock.minute, DEC)); lcd.setCursor(10,0); lcd.print("|"); lcd.setCursor(12,0); lcd.setCursor(12,1); lcd.print(set_temp, 1); //line1 lcd.setCursor(2,1); lcd.print("%"); lcd.setCursor(0,1); lcd.print(DHT11.getHumidity(), 0); lcd.setCursor(4,1); lcd.print("|"); lcd.setCursor(5,1); lcd.print(clock.dayOfMonth, DEC); lcd.setCursor(7,1); lcd.print("/"); lcd.setCursor(8,1); lcd.print(clock.month, DEC); lcd.setCursor(10,1); lcd.print("|"); lcd.setCursor(12,1); lcd.print(set_temp, 1); lcd.setCursor(4,0); } void DHT11_acquiring() // get informations from DHT11 sensor { DHT11.acquire(); while (DHT11.acquiring()); int result = DHT11.getStatus(); switch (result) { case IDDHTLIB_OK: Serial.println("OK"); break; case IDDHTLIB_ERROR_CHECKSUM: Serial.println("Error\n\r\tChecksum error"); break; case IDDHTLIB_ERROR_ISR_TIMEOUT: Serial.println("Error\n\r\tISR Time out error"); break; case IDDHTLIB_ERROR_RESPONSE_TIMEOUT: Serial.println("Error\n\r\tResponse time out error"); break; case IDDHTLIB_ERROR_DATA_TIMEOUT: Serial.println("Error\n\r\tData time out error"); break; case IDDHTLIB_ERROR_ACQUIRING: Serial.println("Error\n\r\tAcquiring"); break; case IDDHTLIB_ERROR_DELTA: Serial.println("Error\n\r\tDelta time to small"); break; case IDDHTLIB_ERROR_NOTSTARTED: Serial.println("Error\n\r\tNot started");

}

break; default: Serial.println("Unknown error"); break;

} int day_time() //set the value according to day - hour { int x = (clock.hour); if(x > 6 && x < 23) return 1; //if daytime set 1 else return 2; //if nighttime set 2 } int seconds_elapsed() { int elapsed_secs = 0; if(prev_time > clock.second) //necessary to work with 60 base count { elapsed_secs = (60-prev_time)+clock.second; } else { elapsed_secs = clock.second - prev_time; } return elapsed_secs; } int minutes_elapsed() { int elapsed_mins = 0; if(last_time > clock.minute) with 60 base count { elapsed_mins = (60-last_time)+clock.minute; } else { elapsed_mins = clock.minute - last_time; } return elapsed_mins; }

//necessary to work

void loop() { int x = 0; bool change_temp = 0; //get temp and date-time DHT11_acquiring(); act_temp = (DHT11.getCelsius()); clock.getTime(); //Read pot pin x = analogRead(pot_pin); x = map(x, 0, 1023, 15, 45); //if temp is set to the minumum turn off the control and backlight

if (x == 15) { stop_pump(); print_LCD(); lcd.setCursor(11,0); lcd.print("-OFF-"); digitalWrite(lcd_bkl, 0); goto stop; } //5 seconds after the last set turn off backlight if(seconds_elapsed() >= delay_set) digitalWrite(lcd_bkl, 0); //if the potentiometer is turn set a new target temp and turn on backlight if(x != prev_temp) { digitalWrite(lcd_bkl, 1); prev_time = (clock.second); change_temp = 1; delay(100); } //if is daytime set the target temp by the potentiometer if (day_time() == 1) { set_temp = (((x+15)*10)/2)/10.00; // set temperature by potentiometer in 0.5°C lcd.setCursor(11,0); lcd.print(" set"); lcd.setCursor(15,0); lcd.write(byte(0)); } //else if is night time set the temp to night_temp value else if (day_time() == 2) { set_temp = night_temp; lcd.setCursor(11,0); lcd.print("sleep"); } //if delay_time is past turn on the temp control if (minutes_elapsed() >= delay_set || change_temp == 1) { if (act_temp < (set_temp - hys)) //if room is cold turn on the heater { start_pump(); last_time = (clock.minute); } else //else if room is warm keep off the heater { stop_pump(); last_time = (clock.minute); } change_temp = 0; } // print SET on LCD, set prev_temp value print_LCD(); prev_temp = x;

delay(100); stop: delay(500); }