// * This calls alarm functions at 8:30 am and at 8:45 pm (20:45)

// * turning lights on at night and off in the morning

// *

// * At startup the time is set to Feb 5 2014 8:29 am

// */

//***********************************************************************/

#include <Time.h> //http://www.pjrc.com/teensy/td_libs_Time.html

#include <TimeAlarms.h> //http://www.pjrc.com/teensy/td_libs_TimeAlarms.html

#include <dht11.h>

#define Relay_1 22 //12V Lighting Rail (10W COB LED, WHITES)

#define Relay_2 23 //36V Lighting Rail (50W COB LED)

#define Relay_3 24 //INTAKE 12V 200mA Fan

#define Relay_4 25 //INTAKE 12V 1.74A Fan

#define Relay_5 26 //EXHAUST 12V 1.74A Fan

#define Relay_6 27 //

#define Relay_7 28 //

#define Relay_8 29 //12V 3L/min Water Pump

#define DHT11PIN 2 //DHT11 Digital Temp/Humidity Sensor

#define RELAY_ON 0

#define RELAY_OFF 1

dht11 DHT11;

int chk = DHT11.read(DHT11PIN);

//***********************************************************************

void setup()

{

Serial.begin(9600);

setTime(8,29,0,2,5,14); // set time to Saturday 8:29:00am Feb 5 2014

pinMode(Relay_1, OUTPUT); digitalWrite(Relay_1, RELAY_OFF);

pinMode(Relay_2, OUTPUT); digitalWrite(Relay_2, RELAY_OFF);

pinMode(Relay_3, OUTPUT); digitalWrite(Relay_3, RELAY_OFF);

pinMode(Relay_4, OUTPUT); digitalWrite(Relay_4, RELAY_OFF);

pinMode(Relay_5, OUTPUT); digitalWrite(Relay_5, RELAY_OFF);

pinMode(Relay_6, OUTPUT); digitalWrite(Relay_6, RELAY_OFF);

pinMode(Relay_7, OUTPUT); digitalWrite(Relay_7, RELAY_OFF);

pinMode(Relay_8, OUTPUT); digitalWrite(Relay_8, RELAY_OFF);

// ********************create the alarms *******************************

Alarm.alarmRepeat(8,30,0, Morning); // 8:30am every day

Alarm.alarmRepeat(20,45,0, Evening); // 8:45pm every day

Alarm.timerRepeat(60, CheckTemp); // timer every 60 seconds, reads DHT11

Alarm.timerRepeat(3600, WaterTime); // timer every 60 min, turns on pump for 5 seconds

}

//***********************************************************************

//***********************************************************************

void loop()

{

digitalClockDisplay();

Alarm.delay(60000); // wait one minute between clock display

}

//***********************************************************************

//***********************************************************************

// ALARM FUNCTIONS

//***********************************************************************

// Morning Alarm********************************************************

void Morning(){

Serial.println("Alarm: - turn lights off");

digitalWrite(Relay_1, RELAY_OFF);

digitalWrite(Relay_2, RELAY_OFF);

digitalWrite(Relay_3, RELAY_OFF);

digitalWrite(Relay_4, RELAY_OFF);}

//Evening Alarm*********************************************************

void Evening(){

Serial.println("Alarm: - turn lights on");

digitalWrite(Relay_1, RELAY_ON);

digitalWrite(Relay_2, RELAY_ON);

digitalWrite(Relay_3, RELAY_ON);

digitalWrite(Relay_4, RELAY_ON);}

//***********************************************************************

// WATER PUMP ***********************************************************

//***********************************************************************

void WaterTime()

{

digitalWrite(Relay_8, RELAY_ON);

delay(5000);

digitalWrite(Relay_8, RELAY_OFF);

}

//***********************************************************************

// READS TEMPERATURE SENSOR*********************************************

//***********************************************************************

void CheckTemp(){

Serial.print("Read sensor: ");

switch (chk){

case 0:

Serial.println("OK");

break;

case -1:

Serial.println("Checksum error");

break;

case -2:

Serial.println("Time out error");

break;

default:

Serial.println("Unknown error");

break;}

Serial.print("Temperature (C): ");

Serial.println((float)DHT11.temperature, 1);

Serial.println("<br />");

Serial.print("Temperature (F): ");

Serial.println(Fahrenheit(DHT11.temperature), 1);

Serial.println("<br />");

Serial.print("Humidity (%): ");

Serial.println((float)DHT11.humidity, 0);

Serial.println("<br />");

Serial.print("Temperature (K): ");

Serial.println(Kelvin(DHT11.temperature), 1);

Serial.println("<br />");

Serial.print("Dew Point (C): ");

Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

Serial.println("<br />");

}

//*********************Displays Time*******************************************

void digitalClockDisplay()

{

Serial.print(hour());

printDigits(minute());

printDigits(second());

Serial.println();

}

void printDigits(int digits)

{

Serial.print(":");

if(digits < 10)

Serial.print('0');

Serial.print(digits);

}

//***********************************************************************

//Celsius to Fahrenheit conversion

//***********************************************************************

double Fahrenheit(double celsius)

{

return 1.8 * celsius + 32;

}

//***********************************************************************

//Celsius to Kelvin conversion

//***********************************************************************

double Kelvin(double celsius)

{

return celsius + 273.15;

}

//***********************************************************************

// dewPoint function NOAA

// reference: http://wahiduddin.net/calc/density_algorithms.htm

//***********************************************************************

double dewPoint(double celsius, double humidity)

{

double A0= 373.15/(273.15 + celsius);

double SUM = -7.90298 * (A0-1);

SUM += 5.02808 * log10(A0);

SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;

SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;

SUM += log10(1013.246);

double VP = pow(10, SUM-3) * humidity;

double T = log(VP/0.61078); // temp var

return (241.88 * T) / (17.558-T);