Hey guys, I've done some searching but no one's problem seems to match mine.
-Arduino IDE 1.6.5
-Arduino Uno
I'm building a simple greenhouse with an automated 12/12 light cycle and humidification every 30min for 10 min.
I'm using two different alarm types to -attempt- to achieve this.
-Alarm.alarmRepeat - To trigger the functions that turn the lights off and on
-Alarm.timerRepeat - To trigger the humidifier every 30min. I also have this at the end of the HumidifierON function to trigger the HumidifierOFF function after 10 min.
Alarm.alarmRepeat(20,00,0, LightsOff); // 8:00am every day
Alarm.alarmRepeat(23,52,50, LightsOn); // 8:00pm every day
Alarm.timerRepeat(humidPeriod, HumidifierON); // How long will the humidifier stay on for?
The thing is, anything under Alarm.alarmRepeat does not get called.
But the humidifier works like a charm! it is called by Alarm.timerRepeat.
I've tried several different ways to trick it into working, I tried moving my alarms into the loop, changing the trigger time, using one zero instead of two in the min and sec spots, uploading from the 1.0.6 IDE, nothing works. I know my function is good because if I call it directly it turns the lights on and prints the status.
I even tried running the sample code (adjusted the trigger time to 1 min after upload and deleted the line to manually set time since my RTC is already set and keeping time nicely) and it would print the 15 sec timer every 15 sec but it would not trigger any of the alarm.alarmRepeat functions.
I'm at a loss here, any advice?
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>
#include "DHT.h"
#define DHTTYPE DHT11 // Define the DHT 11 Humidity Sensor
//OUTPUTS
//RELAYS
const int humidRelay = 3;
const int lightRelay = 4;
//INPUTS
const int buttonOne = 6;
const int DHTPIN = 5;
// Variables:
const int humidActTime = 600; //How long will the Humidifier run before it shuts off (in seconds)
const int humidPeriod = 1800; //How often to run the humidifier (in seconds)
bool lights; //variable to hold the status of the lights
DHT dht(DHTPIN, DHTTYPE); //Initilize the DHT11
RTC_DS1307 RTC; //Initilize the RTC
time_t syncProvider(){ //this does the same thing as RTC_DS1307::get()
RTC.now().unixtime();
}
void setup() {
// initialize the digital pin as an output.
pinMode(lightRelay, OUTPUT);
pinMode(buttonOne, INPUT_PULLUP);
pinMode(humidRelay, OUTPUT);
Serial.begin(9600);
Wire.begin();
RTC.begin();
dht.begin();
syncProvider();
if (! RTC.isrunning()) {
Serial.println(F("RTC is NOT running!"));
}
else {
Serial.println(F("RTC is running!"));
}
Alarm.alarmRepeat(20,00,0, LightsOff); // 8:00pm every day
Alarm.alarmRepeat(8,00,0, LightsOn); // 8:00pm every day
Alarm.timerRepeat(humidPeriod, HumidifierON); // How long will the humidifier stay on for?
Serial.println(F("Startup Completed"));
}
// the loop routine runs over and over again forever:
void loop() {
Serial.print(F("Time: "));
printCurrentTime();
Serial.print(F("Temperature C: "));
Serial.println(getTempC());
Serial.print(F("Humidity: "));
Serial.println(getHumidity());
Serial.println();
Alarm.delay(1000); // wait one second between clock display
}
//FUNCTIONS
//Function to begin the humidifier sequence
void HumidifierON() {
digitalWrite(humidRelay, HIGH); // Activate the humidifier relay
Serial.println(F(" - Humidifier Activated"));
Alarm.timerOnce(humidActTime, HumidifierOFF);
Alarm.delay(1000);
}
//Function to end the humidifier sequence
void HumidifierOFF() {
digitalWrite(humidRelay, LOW); // Set Humidifier LED Off
Serial.println(F(" - Humidifier Deactivated"));
}
//Function to run all night transition events
void Night () {
LightsOff();
}
//Function to run all day transition events
void Day () {
LightsOn();
}
//Function to turn the lights on
void LightsOn() {
digitalWrite(lightRelay, HIGH); // Set Lights On
Serial.println(F(" - Lights On"));
lights = true;
}
//Function to turn the lights off
void LightsOff() {
digitalWrite(lightRelay, LOW); // Set Lights Off
Serial.println(F(" - Lights Off"));
lights = false;
}
//Function to print the current time
void printCurrentTime(){
DateTime now = RTC.now();
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
//Function to get the Temp Celcius reading from the DHT11
float getTempC() {
unsigned long previousMillis;
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > 250) { //The sensor needs at least 250 mills between readings
float t = dht.readTemperature(); //Get the current temperature from the DHT11 in Celcius
previousMillis = currentMillis;
return t;
}
else {
getTempC();
}
}
//Function to get the humidity reading from the DHT11
unsigned int getHumidity() {
unsigned long previousMillis;
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > 250) { //The sensor needs at least 250 mills between readings
unsigned int h = dht.readHumidity(); //Get the current Humidity reading from the DHT11 in Celcius
previousMillis = currentMillis;
return h;
}
else {
getHumidity();
}
}