Hello peeps! So I am trying to troubleshoot a problem that I'm having with a program I have been trying to fine tune for a future project. Currently the program runs on an Arduino Mega 2560 and controls 4 relays using a DHT11 temp/humidity sensor and an RTC3231.
The issue that I'm having is when I try to create two separate scenarios in the same hour. I am trying to get one of my relays to switch on during the first 10 minutes of the hour, and a 10 minute block in the middle of the hour (XX:30-XX:40). The way I currently have it written causes a complete failure of the function all together and the relay just doesn't turn on at all. What I would like to know is why would that happen, and how can I change what I currently have to make this work?
Any help would be appreciated as I am trying to eventually use this as the root program for the same controller but with an LCD button display that will allow me to manipulate these options as well as the humidity/temp.
Thanks Everybody!
#include <DS3231.h>
#include <Wire.h>
#include "DHT.h"
// Define the "data in" pin for the DHT11
#define DHTPIN A3
// Define which type of sensor
#define DHTTYPE DHT11 //DHT 11
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
// Set Relay constants to digital pins 4-7
const int RELAY1 = 8;
const int RELAY2 = 9;
const int RELAY3 = 10;
const int RELAY4 = 11;
// Identify clock variables
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() {
// Set pinMode for RELAYs 1-4 to OUTPUT
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
// Start I2C interface
Wire.begin();
Clock.setSecond(20);//Set the second
Clock.setMinute(18);//Set the minute
Clock.setHour(13); //Set the hour
Clock.setDoW(3); //Set the day of the week
Clock.setDate(24); //Set the date of the month
Clock.setMonth(8); //Set the month of the year
Clock.setYear(16); //Set the year (Last two digits of the year)
// Begin serial interface
Serial.begin(115200);
Serial.println("DHT11 test!");
dht.begin();
}
void ReadDS3231()
{
// Initialize variables for time
// Print time
}
void loop() {
// Read the RTC module
ReadDS3231();
delay(1000);
// Set local variables for RTC relay logic
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 readout for RTC module
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');
// Sets fan interval
if ( minute >= 0 && minute < 10 )
{
digitalWrite ( RELAY2, HIGH );
}
else
{
digitalWrite ( RELAY2, LOW );
}
if ( minute >= 30 && minute < 35 )
{
digitalWrite ( RELAY2, HIGH );
}
else
{
digitalWrite ( RELAY2, LOW );
}
*/
// Turn on relay from 6am to 6pm
if ( hour >= 6 && hour < 18 )
{
digitalWrite ( RELAY3, HIGH );
}
else
{
digitalWrite ( RELAY3, LOW );
}
// 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);
// Print readout from DHT11 sensor
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");
// Sets parameter for Humidity RELAY1 switch
if (h < 89.00)
{
digitalWrite(RELAY1, HIGH);
}
else
{
digitalWrite(RELAY1, LOW);
}
// Sets parameter for Temperature RELAY4 switch
/* if (t <= 70.00)
{
digitalWrite(RELAY4, HIGH);
}
else
{
digitalWrite(RELAY4, LOW);
}
*/