Hi There,
I'm pretty bad at arduino, but trying to learn.
This is an aquarium monitor I am working on.
The problem I am having is that I'd like to have two relays that control lights based on an RTC timer. The first relay works fine with the timer, the second does not. I've tried a few different things, but can't figure out what I am doing wrong... I think I might need a second pair of eyes, or maybe I am just doing something completely wrong.
I've attached my code below. Its heavily commented and indented in a way that makes sense to me, apologies if its over complicated or unclear. I will work on that.
Thank you. I really appreciate this community and forum. Its been a great place to learn.
//Paludarium Monitor V.004a
//This Sketch works as of August 29
//RTC is working, 2 DS18B20 temp probes, 1 DHT11 (though it shit), 2 relays (Lights1 & Lights2 work),
//Not working yet is the flow rate sensor, ph and the .print functions on serial and lcd.
//2nd relay is not working independantly of the first. can't figure out why.
#include <SimpleDHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h> // For I2C
#include <RTClib.h>
//Establishes pin for DHT11 sensor:
int pinDHT11 = 5;
SimpleDHT11 dht11;
//Establishes the pins for Lights relays..
int Lights1 = 6;
int Lights2 = 11;
//Establishes on & off times for Lights1
const int OnHour1 = 5; // Hour Value of Lights1 On Time (In 24 hour format)
const int OnMin1 = 31; // Minute Value of Lights1 On Time
const int OffHour1 = 5; // Hour Value of Lights1 Off Time (In 24 hour format)
const int OffMin1 = 33; // Minute Value of Lights1 On Time
//Establishes on & off times for Lights2
const int OnHour2 = 5; // Hour Value of Lights2 On Time (In 24 hour format)
const int OnMin2 = 35; // Minute Value of Lights2 On Time
const int OffHour2 = 5; // Hour Value of Lights2 Off Time (In 24 hour format)
const int OffMin2 = 3; // Minute Value of Lights2 On Time
RTC_DS1307 rtc;
//DS18b20:
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int deviceCount = 0;
float tempC;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Serial.begin(9600);
sensors.begin();
lcd.init();
lcd.init();
lcd.backlight();
// Setting up Lights1 & Lights2 relay switches...
pinMode(Lights1, OUTPUT);
digitalWrite(Lights1, LOW);
pinMode(Lights2, OUTPUT);
digitalWrite(Lights2, LOW);
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if(! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
}
void loop()
{
Serial.println();
Serial.print("======================="); Serial.println();
Serial.print("|-.+.-.-.+.-.-.+.-.-.-|"); Serial.println();
Serial.print("|75 Gallon Paludarium:|"); Serial.println();
Serial.print("|||||||||||||||||||||||"); Serial.println();
Serial.print("======================="); Serial.println();
Serial.println();
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.println(now.second(), DEC);
//myFile.close();
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
Serial.print("Read DHT11 failed"); Serial.println();
sensors.requestTemperatures();
//Serial.println("");
}
//lcd.setCursor(0,0); lcd.print("75 Gallon Paludarium");
lcd.setCursor(0,0); lcd.print(now.year(), DEC);
lcd.setCursor(4,0); lcd.print("/");
lcd.setCursor(5,0); lcd.print(now.month(), DEC);
lcd.setCursor(7,0); lcd.print('/');
lcd.setCursor(8,0); lcd.print(now.day(), DEC);
lcd.setCursor(10,0); lcd.print(" | ");
lcd.setCursor(12,0); lcd.print(now.hour(), DEC);
lcd.setCursor(14,0); lcd.print(':');
lcd.setCursor(15,0); lcd.print(now.minute(), DEC);
lcd.setCursor(17,0); lcd.print(':');
lcd.setCursor(18,0); lcd.println(now.second(), DEC);
lcd.setCursor(0,1); lcd.print("Air:"); lcd.print(sensors.getTempCByIndex(1));
lcd.setCursor(11,1); lcd.print("RH%:"); lcd.print((int)humidity);
lcd.setCursor(0,2); lcd.print("h20:"); lcd.print(sensors.getTempCByIndex(0));
lcd.setCursor(11,2); lcd.print("LPH:");
lcd.setCursor(0,3); lcd.print("L1:"); if (Lights1, HIGH) {lcd.setCursor(3,3); lcd.print(" Off"); } else (Lights1, LOW); { lcd.print(" On"); }
lcd.setCursor(12,3); lcd.print("pH:");
Serial.print("Air Temperature: "); Serial.print(sensors.getTempCByIndex(0)); Serial.print("C"); Serial.println();
Serial.print("Relative Humidity: "); Serial.print((int)humidity); Serial.print("%"); Serial.println();
sensors.requestTemperatures();
Serial.print("H20 Temperature: "); Serial.print(sensors.getTempCByIndex(0)); Serial.print("C"); Serial.println();
Serial.print("Flow Rate:"); Serial.print((int)temperature); Serial.println(" Litres/hour"); //Serial.println();
Serial.print("Co2:"); Serial.println("N/A");
Serial.print("Light #1:"); if (Lights1, HIGH) { Serial.print(" On"); } else { Serial.print(" Off"); }
Serial.println();
Serial.print(".......................");
Serial.println();
if(now.hour() == OnHour1 && now.minute() == OnMin1) {digitalWrite(Lights1, HIGH); }
else if (now.hour() == OffHour1 && now.minute() == OffMin1) {digitalWrite(Lights1, LOW);}
//if(now.hour() == OffHour1 && now.minute() == OffMin1) {digitalWrite(Lights1, LOW); }
if(now.hour() == OnHour2 && now.minute() == OnMin2) {digitalWrite(Lights1, HIGH); }
else if (now.hour() == OffHour2 && now.minute() == OffMin2) {digitalWrite(Lights2, LOW);}
//else if {digitalWrite(Lights2, LOW);}
//if(now.hour() == OffHour2 && now.minute() == OffMin2) {digitalWrite(Lights1, LOW); }
delay(8000);
}