#include <Wire.h>
#include "RTClib.h"
#include <math.h> //loads the more advanced math functions
#define Relay 4 // relay is connected to pin 4
#define Pir 8 // Pir is connected to pin 8
RTC_DS1307 rtc;
int TIME_STATE = 0 ;
int TEMP_STATE = 0;
int PIR_STATE = 0;
int COMBI_STATE= 0;
int RELAY_WAIT=0;
int RELAY_STATE=0;
int Counter =0;
void setup() {
delay(10000); // always wait x seconds when powering on. give the compressor time to equalise
Serial.begin(9600);
Wire.begin();
rtc.begin();
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
}
pinMode(Pir,INPUT); //Pir sensor is set as a INPUT (sensor stays high for 5 min when movment is detected)
pinMode(Relay, OUTPUT);
digitalWrite(Pir,LOW);
digitalWrite(Relay,LOW);
}
double Thermister(int RawADC) { //Function to perform the fancy math of the Steinhart-Hart equation
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celsius
// Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius
return Temp;
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
int val; //Create an integer variable
double temp; //Variable to hold a temperature value
val=analogRead(0); //Read the analog port 0 and store the value in val
temp=Thermister(val); //Runs the fancy math on the raw analog value
Serial.println("Temp");
Serial.println(temp); //Print the value to the serial port
Serial.println("Temp_STATE");
Serial.println(TEMP_STATE);
Serial.println("TIME_STATE");
Serial.println(TIME_STATE);
Serial.println("PIR_STATE");
Serial.println(PIR_STATE);
Serial.println("RELAY_STATE");
Serial.println(RELAY_STATE);
Serial.println("RELAY_WAIT");
Serial.println(RELAY_WAIT);
Serial.println();
delay(1000);
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// the if statements below set TIME_STATE to 0 between 20:00-8:59 and from 9:00-19.59 to 1
if (now.hour() == 20){
TIME_STATE=0;
}
if (now.hour() == 21){
TIME_STATE=0;
}
if (now.hour() == 22){
TIME_STATE=0;
}
if (now.hour() == 23){
TIME_STATE=0;
}
if (now.hour() == 0){
TIME_STATE=0;
}
if (now.hour() == 1){
TIME_STATE=0;
}
if (now.hour() == 2){
TIME_STATE=0;
}
if (now.hour() == 3){
TIME_STATE=0;
}
if (now.hour() == 4){
TIME_STATE=0;
}
if (now.hour() == 5){
TIME_STATE=0;
}
if (now.hour() == 6){
TIME_STATE=0;
}
if (now.hour() == 7){
TIME_STATE=0;
}
if (now.hour() == 9){
TIME_STATE=1;
}
if (now.hour() == 10){
TIME_STATE=1;
}
if (now.hour() == 11){
TIME_STATE=1;
}
if (now.hour() == 12){
TIME_STATE=1;
}
if (now.hour() == 13){
TIME_STATE=1;
}
if (now.hour() == 14){
TIME_STATE=1;
}
if (now.hour() == 15){
TIME_STATE=1;
}
if (now.hour() == 16){
TIME_STATE=1;
}
if (now.hour() == 17){
TIME_STATE=1;
}
if (now.hour() == 18){
TIME_STATE=1;
}
if (now.hour() == 19){
TIME_STATE=1;
}
/////////////////////////////////////////////////////////////////////////////
//the statement below sets PIR_STATE to 1 when movement in the room is detected(PIR stays high for 5 minutes)
if (digitalRead(Pir)==HIGH){
PIR_STATE=1;
}
else{PIR_STATE=0;}
/////////////////////////////////////////////////////////////////////////////
//the statement below sets TEMP_STATE to 0 when the temperature exceeds 65 degrees celius
if (temp > 65){
TEMP_STATE=0;
}
else{TEMP_STATE=1;
}
///////////////////////////////////////////////////////////////////////////////////
//the statement below puts RELAY_STATE to 1 when PIR and TIME STATE are 1
if (PIR_STATE==1 & TIME_STATE==1){
COMBI_STATE=1;
}
else {COMBI_STATE=0;}
/////////////////////////////////////////////////////////////////////////////////////
if (COMBI_STATE==1){
RELAY_STATE=1;
}
//////////////////////////////////////////////////////////////////////////////
if (COMBI_STATE==1){
digitalWrite(Relay,HIGH);
}
else{digitalWrite(Relay,LOW);}
///////////////////////////////////////
while(COMBI_STATE==0 & RELAY_STATE==1){
Counter = Counter+1;
Serial.println("RELAY WAIT LOOP ACTIVATED");
Serial.println("Counter");
Serial.println(Counter);
delay(1000);
if(Counter >= 35){
RELAY_STATE=0;
Counter=0;
}
}
///////////////////////////////////////////////////////////////////////////////////
//the statement below starts a cool down period when TEMP_STATE is 1. It also waits at least x seconds before it can kick out of the loop
while (TEMP_STATE==0){
digitalWrite(Relay,LOW);
Counter = Counter+1;
Serial.println("COOLDOWN LOOP ACTIVATED");
Serial.println("Counter");
Serial.println(Counter);
delay(1000);
if(Counter >= 35){
TEMP_STATE=1;
Counter=0;
}
}
}
Created April 28, 2018
Comments
Please log in or sign up to comment.