Microclimate bubble

Again my project leads me back to the forums for advice.

I am working on building a climate control module that will regulate humidity, fresh air exchange, and light. I am running this on a Mega2650 and using a DHT11 and DS3231 RTC. These two inputs will control a 4 relay shield that activates outlets that control the corresponding climate controls.

I wrote a sketch that activated the fresh air exchange fans for 5 minute periods every hour over a 24 hour period while running lights on a 12 hour on/off cycle. I did this with if statements. What I am really interested in is how do I simultaneously read from the DHT11 sensor, and use the data collected from that to activate the #1 relay that activates a fog pump.

Is this something I can achieve with 1 device? I'm still very new at this so please excuse the basic nature of this hahaha

Post the code you have already written for the fan/lights and someone will suggest how you integrate the DHT11 sensor into it.

Does this help?

6v6gt:
Post the code you have already written for the fan/lights and someone will suggest how you integrate the DHT11 sensor into it.

Here is a bit of the code that I've written so far. This is not everything as there are a total of 24x4 if statements for each hour block of the day. If this is a totally redundant or inefficient way of doing this I am up for any and all suggestions.

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;



const int RELAY1 = 4;
const int RELAY2 = 5;
const int RELAY3 = 6;
const int RELAY4 = 7;





void setup() {
    int second,minute,hour,date,month,year,temperature; 
  
	// Start the I2C interface
	Wire.begin();
        Clock.setSecond(00);//Set the second 
        Clock.setMinute(00);//Set the minute 
        Clock.setHour(12);  //Set the hour 
        Clock.setDoW(5);    //Set the day of the week
        Clock.setDate(12);  //Set the date of the month
        Clock.setMonth(2);  //Set the month of the year
        Clock.setYear(16);  //Set the year (Last two digits of the year)
	// Start the serial interface
	Serial.begin(115200);
}

void ReadDS3231()
{ 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();k
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  
}
void loop() {ReadDS3231();delay(1000);
{

if (hour == 12 && minute == 17 && second == 0)
{
  digitalWrite(RELAY1, LOW);
}
else if (hour == 12 && minute == 17 && second == 30)
{
  digitalWrite(RELAY1, HIGH);
}
if (hour == 12 && minute == 17 && second == 0)
{
  digitalWrite(RELAY2, LOW);
}
else if (hour == 12 && minute == 17 && second == 30)
{
  digitalWrite(RELAY2, HIGH);
}
if (hour == 12 && minute == 17 && second == 0)
{
  digitalWrite(RELAY3, LOW);
}
else if (hour == 12 && minute == 17 && second == 30)
{
  digitalWrite(RELAY3, HIGH);
}
if (hour == 12 && minute == 17 && second == 0)
{
  digitalWrite(RELAY4, LOW);
}
else if (hour == 12 && minute == 17 && second == 30)
{
  digitalWrite(RELAY4, HIGH);
}

	
}
}


I am also including the text sketch that I'm using for the DHT11 so you can tell which library and base code I'm using.

[code]
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 11     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}

Thank you for the input folks!

[/code]

OK, that’s a start.

  1. Use the code auto-format function to format your code properly. Putting the read() and delay() on the same line is legal in C++ code but it’s not a good idea for readability. It’s on the Tools menu.

  2. Every time you find yourself typing out numbers like “17” and “30” repeatedly, it’s time to make a constant. Then you can refer to that everywhere in the code and when you need to change “17” to “18” next week, you only have to change one place.

Give the constants recognisable names like OnMinute, OffMinute, OnSecond and so on.

  1. If you do the same thing every hour, you don’t need 24 if() statements. If you want to do different things in daylight hours, use a range instead of ==

  2. Don’t look for seconds exactly equal to any constant. Your delay takes one second and there’s a little bit of other processing, so you might not take a reading from the clock every single second. Use greater-than type operators, just in case you miss that critical second.

I'll just focus on two potential optimisations.

The first, which has not already been mentioned, concerns the clock module.

I guess this has a backup battery and once set, it retains its time (and a DS3231 has usually an accuracy of seconds per year).

I recommend that you set it once in a separate sketch (program) and in your microclimate bubble program you only read it (not set it).

All that code you have in setup() like "Clock.setMonth(2);" etc. should go.

The second is, which has already partly been covered in a previous answer, is that if you are doing something say every 30 seconds like:

if (hour == 12 && minute == 17 && second == 0)
{
  digitalWrite(RELAY1, LOW);
}
else if (hour == 12 && minute == 17 && second == 30)
{
  digitalWrite(RELAY1, HIGH);
}
. . .
. . .

Then you should consider maintaining a variable with the current time in seconds from midnight (say secsFromMidnight) and testing that.
For example, and to show how compact the code could be, you could have a relay come on and off at alternate 30 second intervals throughout the day with the following code:

digitalWrite ( RELAY2, ((long)(secsFromMidnight/30))%2);

I really appreciate all of the feedback. I need to kind of absorb all of this though so bear with me hahaha.

First.

MorganS:
OK, that's a start.

  1. Use the code auto-format function to format your code properly. Putting the read() and delay() on the same line is legal in C++ code but it's not a good idea for readability. It's on the Tools menu.

  2. Every time you find yourself typing out numbers like "17" and "30" repeatedly, it's time to make a constant. Then you can refer to that everywhere in the code and when you need to change "17" to "18" next week, you only have to change one place.

Give the constants recognisable names like OnMinute, OffMinute, OnSecond and so on.

  1. If you do the same thing every hour, you don't need 24 if() statements. If you want to do different things in daylight hours, use a range instead of ==

  2. Don't look for seconds exactly equal to any constant. Your delay takes one second and there's a little bit of other processing, so you might not take a reading from the clock every single second. Use greater-than type operators, just in case you miss that critical second.

The block of code I posted was a test block so here are the real parameters. If I initially set the clock to run on a 24 hr schedule the #2 Relay should activate for the first 5 minutes of every hour regardless of conditions in the room. Also, #3 Relay should activate at 0500 and deactivate at 1700 hours.

Should I be creating constants for these specific time parameters and using them in my "if" statements?

Great advice on the auto formatting also. Most of that sketch is bits and pieces of a library test sketch.

6v6gt:
I'll just focus on two potential optimisations.

The first, which has not already been mentioned, concerns the clock module.

I guess this has a backup battery and once set, it retains its time (and a DS3231 has usually an accuracy of seconds per year).

I recommend that you set it once in a separate sketch (program) and in your microclimate bubble program you only read it (not set it).

All that code you have in setup() like "Clock.setMonth(2);" etc. should go.

The second is, which has already partly been covered in a previous answer, is that if you are doing something say every 30 seconds like:

if (hour == 12 && minute == 17 && second == 0)

{
  digitalWrite(RELAY1, LOW);
}
else if (hour == 12 && minute == 17 && second == 30)
{
  digitalWrite(RELAY1, HIGH);
}
. . .
. . .



Then you should consider maintaining a variable with the current time in seconds from midnight (say secsFromMidnight) and testing that. 
For example, and to show how compact the code could be, you could have a relay come on and off at alternate 30 second intervals throughout the day with the following code:


digitalWrite ( RELAY2, ((long)(secsFromMidnight/30))%2);

So my first question to your response is how do I go about loading a separate sketch? Would I just have 2 sketches written out on the same arduino sketch window with the clock setup running first and no loop? Sorry if that's a severe noob question.

Now for the condensed block of code you wrote. Instead of 30 second intervals I'm trying to get the first 5 minutes of every hour. Could you break that code down into its individual functions and explain how that would apply to my application?

Again thank everyone so much!

cmwilliams:
. . .
So my first question to your response is how do I go about loading a separate sketch? Would I just have 2 sketches written out on the same arduino sketch window with the clock setup running first and no loop? Sorry if that's a severe noob question.
. . .

It is not the case that 2 sketches run simultaneously. You'd open a new Arduino IDE sketch window, load the new sketch to update the real time clock, update it to reflect any new time settings, compile it and upload it. This will correct the real time clock module. Then close the sketch window. You should not have to repeat this for a long time.

The real time clock module should function, with reasonable accuracy for a year or so, so it does not make sense in your sketch to set it. Further, if you were to reset your arduino sometime after loading your original sketch, it would revert to the time you have hard-coded in the setup() function.

        Clock.setSecond(00);//Set the second
        Clock.setMinute(00);//Set the minute
        Clock.setHour(12);  //Set the hour
        Clock.setDoW(5);    //Set the day of the week
        Clock.setDate(12);  //Set the date of the month
        Clock.setMonth(2);  //Set the month of the year
        Clock.setYear(16);  //Set the year (Last two digits of the year)

My suggestion is a completely different sketch, dedicated to setting the real time clock, which is run only very occasionally.

cmwilliams:
. . .
Now for the condensed block of code you wrote. Instead of 30 second intervals I’m trying to get the first 5 minutes of every hour. Could you break that code down into its individual functions and explain how that would apply to my application?
. . .

If it is the first 5 minutes of every hour, then it is very simple, without any tricks with modulus arithmetic:

if ( minute >= 0 && minute < 5 ) {
        digitalWrite ( RELAY2, HIGH ) ;
}
else {
        digitalWrite ( RELAY2, LOW ) ;
}

Which could even be reduces to:

digitalWrite ( RELAY2, ( minute >= 0 && minute < 5 ) ) ;

6v6gt:
It is not the case that 2 sketches run simultaneously. You'd open a new Arduino IDE sketch window, load the new sketch to update the real time clock, update it to reflect any new time settings, compile it and upload it. This will correct the real time clock module. Then close the sketch window. You should not have to repeat this for a long time.

The real time clock module should function, with reasonable accuracy for a year or so, so it does not make sense in your sketch to set it. Further, if you were to reset your arduino sometime after loading your original sketch, it would revert to the time you have hard-coded in the setup() function.

        Clock.setSecond(00);//Set the second

Clock.setMinute(00);//Set the minute
        Clock.setHour(12);  //Set the hour
        Clock.setDoW(5);    //Set the day of the week
        Clock.setDate(12);  //Set the date of the month
        Clock.setMonth(2);  //Set the month of the year
        Clock.setYear(16);  //Set the year (Last two digits of the year)



My suggestion is a completely different sketch, dedicated to setting the real time clock, which is run only very occasionally.

So in regards to this. I would write a sketch setting time date and whatnot and upload in the IDE.
Then in a separate window I would upload my actual controller sketch? If this is the case should I hit the reset button on the Mega2650 before loading the sketches?

cmwilliams:
So in regards to this. I would write a sketch setting time date and whatnot and upload in the IDE.
Then in a separate window I would upload my actual controller sketch? If this is the case should I hit the reset button on the Mega2650 before loading the sketches?

Exactly. The new sketch to do the initial configuration/time setting of your DS3231 real time module is completely separate and is effectively run only once. You might also want to get this sketch to print out the current date/time just to confirm it works. Once it has run, you can reset your mega and load and run your original sketch (naturally with the RTC setting code now deleted from it) as many times as you like.

6v6gt:
Exactly. The new sketch to do the initial configuration/time setting of your DS3231 real time module is completely separate and is effectively run only once. You might also want to get this sketch to print out the current date/time just to confirm it works. Once it has run, you can reset your mega and load and run your original sketch (naturally with the RTC setting code now deleted from it) as many times as you like.

So as advised I ran my DS3231 Test Sketch to set my RTC module’s time. That looks something like this.

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h24;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A24h, Apm;

byte year, month, date, DoW, hour, minute, second;

void setup() {
	// Start the I2C interface
	Wire.begin();
        Clock.setSecond(00);//Set the second 
        Clock.setMinute(03);//Set the minute 
        Clock.setHour(23);  //Set the hour 
        Clock.setDoW(6);    //Set the day of the week
        Clock.setDate(19);  //Set the date of the month
        Clock.setMonth(2);  //Set the month of the year
        Clock.setYear(16);  //Set the year (Last two digits of the year)
	// Start the serial interface
	Serial.begin(115200);
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h24, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature); 
  Serial.print('\n');
}
void loop() 
{
  ReadDS3231();
  delay(1000);

Then I unplugged from my laptop and plugged in the power supply, reset, and unplugged. Replugged into the computer and uploaded my run sketch. That looks like this.

#include <DS3231.h>
#include <Wire.h>

const int RELAY1 = 4;
const int RELAY2 = 5;
const int RELAY3 = 6;
const int RELAY4 = 7;

DS3231 Clock;
bool Century=false;
bool h24;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A24h, Apm;

byte year, month, date, DoW, hour, minute, second;


void setup() {
  Serial.begin(115200);

}

void ReadDS3231()
{
  // Initialize variables for time
  int second,minute,hour,date,month,year,temperature;
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h24, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  // Print time
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  
}


void loop() {
  // Turn on relay for the first 5 minutes of the hour
  // Sets fan interval for FRESH AIR EXCHANGE
  if (minute >= 0 && minute < 5) 
  {
    digitalWrite(RELAY2, HIGH);
  }
  else 
  {
    digitalWrite(RELAY2, LOW);
  }
  
  // Sets light timer interval for 12 HOUR INTERVALS
  if (hour >= 5 && hour < 17)
  {
    digitalWrite(RELAY3, HIGH);
  }
  else
  {
    digitalWrite(RELAY3, LOW);
  }  
}

The first problem at hand is that I’m not getting any time printing in my serial monitor.
Also, my relay 2 has now turned on and not shut off since. I checked my pins and everything is wired correctly so I’m not sure why anything is activated since it’s almost 2400 hours. Nothing should be activated.
Any ideas/input?

You must be nearly there. Just add the statements back into the loop in your main sketch, which appear to have got lost:

ReadDS3231();
delay(1000);

6v6gt:
You must be nearly there. Just add the statements back into the loop in your main sketch, which appear to have got lost:

ReadDS3231();

delay(1000);

ReadDS3231();
delay(1000);
[/code]
[/quote]

So I have a couple updates. I added back the ReadDS3231(); ad delay(1000);. This compiled but for some reason I still have the problem where regardless of the time RELAY2 remains activated.
Another problem was that the clock readout on the Serial Monitor gave bad value. The date printed correctly but the time was just a constant repeat of the same nonsense numbers. I decided to add back the bit of code to set the clock but This is still giving me the problem of the relay activating incorrectly.

I wondered if this was maybe a problem with NO/NC mixup I switched the "HIGH"/"LOW" to see if that had an affect and now my RELAY3 is constantly active.

My question is could this be a bug with the clock. The original test code has a section of code like this.

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

whereas in my code I have written.

DS3231 Clock;
bool Century=false;
bool h24;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A24h, Apm;

If you look I have switched the 12h with 24h. I am wondering if this is causing a problem but it poses my question of does the 12h variable only cycle on a 12hour clock, and do I have to specify running on a 24hour clock? I would really like to solve this problem, this project is becoming very disappointing hahaha :confused:

If reading from the clock is giving unusable results, focus there first before hacking in your main sketch, since everything else is dependent on time.
When you run your “test” sketch in post #11 (for configuring the real time clock) does that print out the time correctly ?
Some of your time handling statements are unusual eg month=Clock.getMonth(Century);
Please provide a link to the guide/example you have used for creating the command to access the clock module, and also a link to the library <DS3231.h> you have used.

6v6gt:
If reading from the clock is giving unusable results, focus there first before hacking in your main sketch, since everything else is dependent on time.
When you run your “test” sketch in post #11 (for configuring the real time clock) does that print out the time correctly ?
Some of your time handling statements are unusual eg month=Clock.getMonth(Century);
Please provide a link to the guide/example you have used for creating the command to access the clock module, and also a link to the library <DS3231.h> you have used.

I finally got home from work so I have a few minutes to work on the project!

When I run my “test” sketch I get a good time print out. I will attach the test code that I am using for this. It was a DS3231 Library test that I had downloaded.

/*
DS3231_test.pde
Eric Ayars
4/11

Test/demo of read routines for a DS3231 RTC.

Turn on the serial monitor after loading this to check if things are
working as they should.

*/

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

void setup() {
	// Start the I2C interface
	Wire.begin();
        Clock.setSecond(50);//Set the second 
        Clock.setMinute(59);//Set the minute 
        Clock.setHour(11);  //Set the hour 
        Clock.setDoW(5);    //Set the day of the week
        Clock.setDate(31);  //Set the date of the month
        Clock.setMonth(5);  //Set the month of the year
        Clock.setYear(13);  //Set the year (Last two digits of the year)
	// Start the serial interface
	Serial.begin(115200);
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature); 
  Serial.print('\n');
}
void loop() {ReadDS3231();delay(1000);
	// send what's going on to the serial monitor.
	// Start with the year
/*	Serial.print("2");
	if (Century) {			// Won't need this for 89 years.
		Serial.print("1");
	} else {
		Serial.print("0");
	}
	Serial.print(Clock.getYear(), DEC);
	Serial.print('-');
	// then the month
	Serial.print(Clock.getMonth(Century), DEC);
	Serial.print('-');
	// then the date
	Serial.print(Clock.getDate(), DEC);
	Serial.print(' ');*/
	// and the day of the week
	/*Serial.print(Clock.getDoW(), DEC);
	Serial.print(' ');*/
	// Finally the hour, minute, and second
	/*Serial.print(Clock.getHour(h12, PM), DEC);
	Serial.print(':');
	Serial.print(Clock.getMinute(), DEC);
	Serial.print(':');
	Serial.print(Clock.getSecond(), DEC);
	// Add AM/PM indicator
	if (h12) {
		if (PM) {
			Serial.print(" PM ");
		} else {
			Serial.print(" AM ");
		}
	} else {
		Serial.print(" 24h ");
	}
	// Display the temperature
	Serial.print("T=");
	Serial.print(Clock.getTemperature(), 2);
	// Tell whether the time is (likely to be) valid
	if (Clock.oscillatorCheck()) {
		Serial.print(" O+");
	} else {
		Serial.print(" O-");
	}*/
	// Indicate whether an alarm went off
	/*if (Clock.checkIfAlarm(1)) {
		Serial.print(" A1!");
	}
	if (Clock.checkIfAlarm(2)) {
		Serial.print(" A2!");
	}*/
	// New line on display
	//Serial.print('\n');
       // delay(1000);
	// Display Alarm 1 information
/*	Serial.print("Alarm 1: ");
	Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);
	Serial.print(ADay, DEC);
	if (ADy) {
		Serial.print(" DoW");
	} else {
		Serial.print(" Date");
	}
	Serial.print(' ');
	Serial.print(AHour, DEC);
	Serial.print(' ');
	Serial.print(AMinute, DEC);
	Serial.print(' ');
	Serial.print(ASecond, DEC);
	Serial.print(' ');
	if (A12h) {
		if (Apm) {
			Serial.print('pm ');
		} else {
			Serial.print('am ');
		}
	}
	if (Clock.checkAlarmEnabled(1)) {
		Serial.print("enabled");
	}
	Serial.print('\n');
	// Display Alarm 2 information
	Serial.print("Alarm 2: ");
	Clock.getA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);
	Serial.print(ADay, DEC);
	if (ADy) {
		Serial.print(" DoW");
	} else {
		Serial.print(" Date");
	}
	Serial.print(' ');
	Serial.print(AHour, DEC);
	Serial.print(' ');
	Serial.print(AMinute, DEC);
	Serial.print(' ');
	if (A12h) {
		if (Apm) {
			Serial.print('pm');
		} else {
			Serial.print('am');
		}
	}
	if (Clock.checkAlarmEnabled(2)) {
		Serial.print("enabled");
	}*/
	/* display alarm bits
	Serial.print('\n');
	Serial.print('Alarm bits: ');
	Serial.print(ABits, DEC);
	*/
/*
	Serial.print('\n');
	Serial.print('\n');
	delay(1000);

	// Display the time once more as a test of the getTime() function
	Clock.getTime(year, month, date, DoW, hour, minute, second);
	
        Serial.print(year, DEC);
        Serial.print("/");
	Serial.print(month, DEC);
        Serial.print("/");
	Serial.print(date, DEC);
        Serial.print("day of the week :");
	Serial.println(DoW, DEC);
	Serial.print(hour, DEC);
        Serial.print(":");
	Serial.print(minute, DEC);
        Serial.print(":");
	Serial.println(second, DEC);*/
}

That is an exact copy/paste from the library examples.

6v6gt:
If reading from the clock is giving unusable results, focus there first before hacking in your main sketch, since everything else is dependent on time.
When you run your “test” sketch in post #11 (for configuring the real time clock) does that print out the time correctly ?
Some of your time handling statements are unusual eg month=Clock.getMonth(Century);
Please provide a link to the guide/example you have used for creating the command to access the clock module, and also a link to the library <DS3231.h> you have used.

In an odd series of events I decided to load that test sketch into the IDE, enter my constants for my relays, and entered my if statements and time.
Time displayed correctly, but again my relay turns on regardless of what time it is. Is there a chance that there is a problem with the fact that it’s returning an incorrect value for the time? Or just not returning a value at all? I do not know much about programming but logically it would seem that regardless of time, the processor is making it to the loop() portion of the program and just getting stuck at that first step.

month=Clock.getMonth(Century);

Ah yes. Now I understand. It appears that the DS3231 timer library you have found, has a particularly useful feature builtin to the function which determines what month it is. Function getMonth(), as well as telling you what month it is, sets a boolean variable to inform you if the century has rolled over during the time it has taken to process the function call. Some of the younger Arduino hobbyists will certainly be able to benefit from this.

Just quickly looking over the code you have submitted in post #15, it is clear that large chunks of it are commented out ( not effective ) using the multi-line comment feature where /* starts a comment and */ ends it. If you are unsure if a certain section of code is 'hidden' from the compiler by these comments, just type RUBBISH in it and see if the compiler accepts it.

6v6gt:

month=Clock.getMonth(Century);

Ah yes. Now I understand. It appears that the DS3231 timer library you have found, has a particularly useful feature builtin to the function which determines what month it is. Function getMonth(), as well as telling you what month it is, sets a boolean variable to inform you if the century has rolled over during the time it has taken to process the function call. Some of the younger Arduino hobbyists will certainly be able to benefit from this.

Just quickly looking over the code you have submitted in post #15, it is clear that large chunks of it are commented out ( not effective ) using the multi-line comment feature where /* starts a comment and */ ends it. If you are unsure if a certain section of code is ‘hidden’ from the compiler by these comments, just type RUBBISH in it and see if the compiler accepts it.

I’m going to insert a post that I had in another section of this forum, I think you may be surprised by the results!

[quote author=Blue Eyes date=1456110514 link=msg=2627307]
Could you try removing this line:

  int second, minute, hour, date, month, year, temperature;

from ReadDS3231() and see what happens?

I had tried that before and in fact had zero luck. I was playing with the order of some of the things in the code just now, particularly this block

    int second, minute, hour, date, month, year, temperature;
  second = Clock.getSecond();
  minute = Clock.getMinute();
  hour = Clock.getHour(h12, PM);
  date = Clock.getDate();
  month = Clock.getMonth(Century);
  year = Clock.getYear();
  
    Serial.print("20");
  Serial.print(year, DEC);
  Serial.print('-');
  Serial.print(month, DEC);
  Serial.print('-');
  Serial.print(date, DEC);
  Serial.print(' ');
  Serial.print(hour, DEC);
  Serial.print(':');
  Serial.print(minute, DEC);
  Serial.print(':');
  Serial.print(second, DEC);
  Serial.print('\n');

This was initially in the "void ReadDS3231() portion. When I moved the serial prints to the void loop() all i got were 0 values for the time. Also, I had tried moving the “int” block out of the void ReadDS3231() but this caused some compiling errors.
Out of desperation to make this project work, I decided to try moving that entire block above into the void loop() section right after the delay.

The code now looks like this

#include <DS3231.h>
#include <Wire.h>

const int RELAY1 = 4;
const int RELAY2 = 5;
const int RELAY3 = 6;
const int RELAY4 = 7;

DS3231 Clock;
bool Century = false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;


void setup() {

  // Start I2C interface
  Wire.begin();
  Clock.setSecond(00);//Set the second
  Clock.setMinute(53);//Set the minute
  Clock.setHour(18);  //Set the hour
  Clock.setDoW(7);    //Set the day of the week
  Clock.setDate(20);  //Set the date of the month
  Clock.setMonth(2);  //Set the month of the year
  Clock.setYear(16);  //Set the year (Last two digits of the year)

  Serial.begin(115200);

}

void ReadDS3231()
{
  // Initialize variables for time

  // Print time

}


void loop() {
  ReadDS3231();
  delay(1000);
  
    int second, minute, hour, date, month, year, temperature;
  second = Clock.getSecond();
  minute = Clock.getMinute();
  hour = Clock.getHour(h12, PM);
  date = Clock.getDate();
  month = Clock.getMonth(Century);
  year = Clock.getYear();
  
    Serial.print("20");
  Serial.print(year, DEC);
  Serial.print('-');
  Serial.print(month, DEC);
  Serial.print('-');
  Serial.print(date, DEC);
  Serial.print(' ');
  Serial.print(hour, DEC);
  Serial.print(':');
  Serial.print(minute, DEC);
  Serial.print(':');
  Serial.print(second, DEC);
  Serial.print('\n');

  // Turn on relay for the first 5 minutes of the hour
  // Sets fan interval for FRESH AIR EXCHANGE

  if ( second >= 0 && second < 5 )
  {
    digitalWrite ( RELAY2, HIGH );
  }
  else
  {
    digitalWrite ( RELAY2, LOW );
  }
  if ( second >= 5 && second < 17 )
  {
    digitalWrite ( RELAY3, HIGH );
  }
  else
  {
    digitalWrite ( RELAY3, LOW );
  }


}

Miraculously this seems to be working! The correct relays are turning on and off in their correct time!

Now another thing I’m curious of is does this clock run on “military time” ex. 13:30 - 1:30pm?

Also, how would I go about adding my DHT11 data into this sketch, and draw from that data to then write another if statement like this

if (HUMIDITY <= 90%) 
{
  digitalWrite ( RELAY1, HIGH);
}
else 
{ 
  digitalWrite ( RELAY1, LOW);
}

[/quote]

cmwilliams:
. . .
Miraculously this seems to be working! The correct relays are turning on and off in their correct time!

Now another thing I'm curious of is does this clock run on "military time" ex. 13:30 - 1:30pm?
. . .

Very good. When you set the clock, you can configure whether the time is represented by the 12 hour or 24 hour system. From your library:

void setClockMode(bool h12); 
// Set 12/24h mode. True is 12-h, false is 24-hour.

So, for example, if you want to use the 24 hour system, you call setClockMode(false);