Microclimate controller

Good Evening All!

I am having some issues troubleshooting an issue I’m having with a microclimate controller I’m building and was hoping somebody here might take a crack at it!

This project is running on a Mega2650 board I have and a prototype shield with a DS3231 RTC module and DHT11 Humidity/Temp sensor. These two will hopefully control a 4 Relay shield I have wired in. The code so far is designed to activate my #2 relay for the first 5 minutes of every hour, and activate the #3 relay from 5am to 5pm. I want to set this up so that it will also activate the first relay under the condition that the DHT11 reads <90% RH. Everything compiles so far but It seems to be glitching somewhere. Regardless of time, the #2 relay stays active and nothing else will some on or go off.

Code is as follows

#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
  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();
  // 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() {
  ReadDS3231();
  delay(1000);
  // 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 );
  }
  if ( hour >= 5 && hour < 17 )
  {
    digitalWrite ( RELAY3, HIGH );
  }
  else
  {
    digitalWrite ( RELAY3, LOW );
  }


}

You have the time variables defined as global and as local to ReadDS3231(). Move your serial prints to loop() and you should see that they are all zero.

Removing the local declarations in ReadDS3231() should fix the problem.

I moved the serial prints to loop() but that poses a couple of problems.

First is that the clock is just a 00:00:00 read, repeating. There is no actual time being kept.

Second is that my relay is still active. When the device starts it should be starting at the entered time which in this case is 18:53. No relays should be active at this point.

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);
}

Miraculously this seems to be working!

There is nothing miraculous about it. You have two variables named hour, two named second, etc. The types of the two variables are not the same. The scope is not the same. When you set the value of a variable in one scope, that has no affect on the value of the other variable with a different scope.

The hint was to DELETE, not move, the line:

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

so you only have:

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

with global scope.

PaulS:
There is nothing miraculous about it. You have two variables named hour, two named second, etc. The types of the two variables are not the same. The scope is not the same. When you set the value of a variable in one scope, that has no affect on the value of the other variable with a different scope.

The hint was to DELETE, not move, the line:

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

so you only have:

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

with global scope.

That was a suggestion in another forum and created several compiling errors. I used the word miraculously as an expression of my relief. I figured this was a matter of poorly defined variables but was having a hard time correcting the problem.

I would like to know though, how to tell where and when to define those variables and how to determine "scope"

Keep in mind, this is the most programming I have ever done :\

That was a suggestion in another forum and created several compiling errors.

So, post the code that causes the compiler errors AND the compiler errors.

I would like to know though, how to tell where and when to define those variables and how to determine "scope"

Well, I mentioned the word scope to give you a clue what to google for.

PaulS:
So, post the code that causes the compiler errors AND the compiler errors.
Well, I mentioned the word scope to give you a clue what to google for.

Notice how you have contributed nothing to this thread besides making vague hints and piggy-backing off of another user.
Please keep your condescending attitude to yourself it’s not needed here.