Hi,
I'm trying to build an automated greenhouse with the help of an RTC to trigger actions at certain times. I'm using a DS3231 with the DS1307 library and relays so thats why the HIGHs and LOWs are inverted.
What I want the program to do :
1 . Make a LightPin LOW if time is between a certain interval, regardless if the unit should be reset or lose power. What happens now with LightPin is that it turns on at the right time, but if the unit loses power the pin will not go LOW until the RTC is at the starting time again.
- Make AirPin LOW for 15 minutes every other hour and HumPin LOW for 15 minutes every hour.
For goal 1 I've tried using while instead of if, but to no success.
And for goal 2 I realise that it could be done with a lot of "if" statements for the specific times but there has got to be a better way to do this. I've thought about using an integer with the value of the current HOUR/2 to see if the value is even in order to only trigger every other hour, and if it is even then digitalWrite LOW.
USING WHILE FOR GOAL 1
while (now.hour() >= 8 & now.hour() <= 18 & now.minute() == 00 & now.second() == 00)
{
digitalWrite(LightPin, LOW);
}
COMPLETE SKETCH
#include <Wire.h>
#include "RTClib.h"
#include <DHT.h>;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
int LightPin = 5;
int AirPin = 6;
int HumPin = 7;
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float hum;
float temp;
void setup() {
Serial.begin(9600);
lcd.begin();
Wire.begin();
dht.begin();
rtc.begin();
lcd.backlight();
if (! rtc.isrunning())
{
lcd.println("RTC is NOT running!");
}
pinMode (AirPin, OUTPUT);
pinMode (LightPin, OUTPUT);
pinMode (HumPin, OUTPUT);
}
void loop() {
hum = dht.readHumidity();
temp = dht.readTemperature();
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Humi: ");
lcd.print(hum);
lcd.print(" %");
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C ");
if (now.hour() == 8 & now.minute() == 00 & now.second() == 00)
{
digitalWrite(LightPin, LOW);
}
else if (now.hour() == 20 & now.minute() == 00 & now.second() == 00)
{
digitalWrite(LightPin, HIGH);
}
if (now.minute() == 15 & now.second() == 0)
{
digitalWrite(HumPin, LOW);
}
else if (now.minute() == 30 & now.second() == 0)
{
digitalWrite(HumPin, HIGH);
}
if (now.minute() == 45 & now.second() == 0)
{
digitalWrite(AirPin, LOW);
}
else if (now.minute() == 00 & now.second() == 0)
{
digitalWrite(AirPin, HIGH);
} }
Is there anyone here who could point me in the right direction?
I'm still an arduino beginner, so any help and criticism is welcome.
Thanks in advance