#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <Wire.h>
#include <Keypad.h>
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
LiquidCrystal_I2C lcd(0x27,16,2); // pins on arduino on which lcd pins are connected:
int fan1 =11,fan2=12,fan3=10; // for 3 speeds of fan
int CoolON = 13; // for opening valve of cool water from chiler
int temp; // DegC from temperature sensor
int value = 0;
int c = 0;
int setpoint = 0;
const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {2, 3, 4, 5}; //Rows 0 to 3
byte colPins[numCols] = {6, 7, 8, 9}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup()
{
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
lcd.backlight();
lcd.init();
pinMode(CoolON,OUTPUT);
pinMode(fan1,OUTPUT);
pinMode(fan2,OUTPUT);
pinMode(fan3,OUTPUT);
Serial.begin(9600);
lcd.setCursor(5,0);
lcd.println("Welcome");
delay(2000);
}
void loop()
{
DateTime now = rtc.now();
int sensorValue=analogRead(A0); // for reading temperature sensor:
int temp = ((sensorValue*34)/1024); // for converting analog value to temperature
lcd.setCursor(0,0);
lcd.print("Temp : ");
lcd.print(temp);// for reading temp value
lcd.print(" DegC ");
if (!((now.hour() > 7 && now.hour() <= 19 )))
{
setpoint =24 ;
lcd.setCursor(0,1);
lcd.print("SP : ");
lcd.print(setpoint);
}
else
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
//Serial.println(keypressed);
if ( keypressed >= '0' && keypressed <= '9' ) {
if (c < 2) {
value = value * 10 + (keypressed - '0');
//Serial.print("Value : ");
//Serial.println(value);
c++;
if (c == 2) {
if (value >= 9 && value <=30)
{
setpoint = value;
lcd.setCursor(0,1);
lcd.print("SP : ");
Serial.print(setpoint);
lcd.print(setpoint);
lcd.println(" DegC ");
}
else{
lcd.setCursor(0,0);
lcd.println("SP:Invalid!!!");
lcd.setCursor(0,1);
lcd.println("Typ Val b/w 9&30");
delay(2000);
}
value = 0;
c = 0;
}
}
}
}
}
sensorValue=analogRead(A0); // for reading temperature sensor:
temp = ((sensorValue*34)/1024); // for converting analog value to temperature
lcd.setCursor(0,0);
lcd.print("Temp : ");
lcd.print(temp);// for reading temp value
lcd.print(" DegC ");
digitalWrite(fan1,LOW);
digitalWrite(fan2,LOW);
digitalWrite(fan3,LOW);
digitalWrite(CoolON,LOW);
if(setpoint == 0 ) return;
if (setpoint >= (.9 * temp) && setpoint <=(1.25*temp))
{
digitalWrite(fan1,HIGH);
digitalWrite(CoolON,HIGH);
}
else if (setpoint >= (.8 * temp) && setpoint < (0.9 * temp)){
digitalWrite(fan2,HIGH);
digitalWrite(CoolON,HIGH);
}
else if (setpoint <= (.8*temp)){
digitalWrite(fan3,HIGH);
digitalWrite(CoolON,HIGH);
}
else if (setpoint <= (.70*temp))
{
digitalWrite(CoolON,HIGH);
digitalWrite(fan3,HIGH);
}
}