For some reason I can not get any of the alarms working but the Alarm.timerRepeat(15, RepeatPhoto); // timer for every 15 seconds
here is the code
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include "RTClib.h"
//Photoresistor
int lightPin = A0; //define a pin for Photo resistor
int ledPin = 11; //define a pin for LED
int PhotoValue; // This is a varibe to hold the PhotoSensor value
// Relay
int relayClosePin = 10; // This is drill forward "close door"
int relayOpenPin = 9; // This is drill backward "open door"
int relayLightsPin = 8; // All lights on
int relayNightPin = 7; // Night light on
// ------------------------------------------------------DateTime Function
RTC_DS1307 RTC;
int varYear, varMonth, varDay, varHour, varMin, varSec, varHour24;
char yearChar[5]; // set up the char varibles containers
char monthChar[5];
char dayChar[5];
char hourChar[5];
char minChar[5];
char secChar[5];
int varIsAmPm; // 1 is PM 0 is AM
// ------------------------------------------------------End DateTime Function
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
pinMode( ledPin, OUTPUT ); // Photoresistor
pinMode (relayClosePin, OUTPUT); // Relay1 as output pin
pinMode (relayOpenPin, OUTPUT); // Relay2 as output pin
pinMode (relayLightsPin, OUTPUT); // Relay3 as output pin
pinMode (relayNightPin, OUTPUT); // Relay4 as output pin
// ---------------------------- DateTime Function
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(DATE, TIME));
}
runDateTime(); // Show date and time once
// ---------------------------- End DateTime Function
// ---------------------------- Alarms Function
Alarm.alarmRepeat(5,30,0, NightOnAlarm); // 5:30am every day
Alarm.alarmRepeat(6,15,0, NightOffAlarm); // 6:15am every day
Alarm.alarmRepeat(6,0,0, AllLightsOnAlarm); // 6:00am every day
Alarm.alarmRepeat(9,0,0, AllLightsOffAlarm); // 9:00am every day Make sure that if you change these alarms that you change "Alarms" PhotoOnAlarm, PhotoOffAlarm times to match
Alarm.alarmRepeat(19,0,0, NightOn2Alarm); // 7:00pm every day Make sure that if you change these alarms that you change "Alarms" PhotoOnAlarm, PhotoOffAlarm times to match
Alarm.alarmRepeat(21,0,0, NightOff2Alarm); // 9:00pm every day
Alarm.alarmRepeat(16,47,0, DoorOpenAlarm); // 8:00am every day
Alarm.alarmRepeat(21,35,0, DoorCloseAlarm); // 9:30pm every day
Alarm.timerRepeat(15, RepeatPhoto); // timer for every 15 seconds
Alarm.alarmRepeat(17,41,0, EveningAlarm); // 5:45pm every day
}
void loop() {
runDateTimeUpDate ();
Alarm.delay(1000); // wait just one second between alarm checks
}
void AllLightsOnAlarm(){
runAllLights(1);
}
void AllLightsOffAlarm(){
runAllLights(0);
}
void NightOnAlarm(){
runNightLight(1);
}
void NightOffAlarm(){
runNightLight(0);
}
void NightOn2Alarm(){
runAllLights(1);
}
void NightOff2Alarm(){
runAllLights(0);
}
void DoorOpenAlarm(){
runOpenDoor (1);
}
void DoorCloseAlarm(){
runCloseDoor(1);
}
void PhotoOnAlarm(){
runPhoto ();
runDateTimeUpDate ();
if (PhotoValue < 500 && varHour24 < 19 && varHour24 > 9){ // change the times to match the varibles "Alarm.alarmRepeat"
runAllLights(1); // so that the alarm only turns the lights on during the day
}
}
void PhotoOffAlarm(){
runPhoto ();
runDateTimeUpDate ();
if (PhotoValue > 500 && varHour24 < 19 && varHour24 > 9){ // This is less then 7pm -and- greater than 9am
runAllLights(0); // PhotoValue > 400 if there is light outside greater then 500?
}
}
void RepeatPhoto(){
PhotoOnAlarm();
PhotoOffAlarm();
// DoorOpenAlarm ();
}
void Repeats(){
Serial.println("15 second timer");
}
void EveningAlarm(){
Serial.println("Alarm: - turn lights on");
}
void runPhoto(){
PhotoValue = analogRead(lightPin); //Write the value of the photoresistor to the varible PhotoValue
Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
analogWrite(ledPin, analogRead(lightPin)/2); //send the value to the ledPin. Depending on value of resistor
//you have to divide the value. for example,
//with a 10k resistor divide the value by 2, for 100k resistor divide by 4.
delay(10); //short delay for faster response to light.
}
void runDateTime (){
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
varYear = (now.year());
Serial.print('/');
Serial.print(now.month(), DEC);
varMonth = (now.month());
Serial.print('/');
Serial.print(now.day(), DEC);
varDay = (now.day());
Serial.print(' ');
Serial.print(now.hour(), DEC);
varHour = (now.hour());
varHour24 = (now.hour());
if (varHour >= 12){
varIsAmPm = 1; //this is Pm
}
else{
varIsAmPm = 0; // This is Am
}
if (varHour >= 13){
varHour = (varHour-12);
}
if (varHour < 1){
varHour = 12;
}
Serial.print(':');
Serial.print(now.minute(), DEC);
varMin = (now.minute());
Serial.print(':');
Serial.print(now.second(), DEC);
varSec = (now.second());
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
// -------------------------------Put Date and Time in Globle varibles and convert to chars
itoa (varYear, yearChar, 10);
itoa (varMonth, monthChar, 10);
itoa (varDay, dayChar, 10);
itoa (varHour, hourChar, 10);
itoa (varMin, minChar, 10);
itoa (varSec, secChar, 10);
}
void runDateTimeUpDate (){
DateTime now = RTC.now();
varYear = (now.year());
varMonth = (now.month());
varDay = (now.day());
varHour = (now.hour());
varHour24 = (now.hour());
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
if (varHour >= 12){
varIsAmPm = 1; //this is Pm
}
else{
varIsAmPm = 0; // This is Am
}
if (varHour >= 13){
varHour = (varHour-12);
}
if (varHour < 1){
varHour = 12;
}
varMin = (now.minute());
varSec = (now.second());
// -------------------------------Put Date and Time in Globle varibles and convert to chars
itoa (varYear, yearChar, 10);
itoa (varMonth, monthChar, 10);
itoa (varDay, dayChar, 10);
itoa (varHour, hourChar, 10);
itoa (varMin, minChar, 10);
itoa (varSec, secChar, 10);
}
void runCloseDoor (int onOff){
if (onOff == 1){
digitalWrite(relayClosePin, HIGH);
Serial.println("Closing Chicken Door");
}
else{
digitalWrite(relayClosePin, LOW);
Serial.println("Door Close Off");
}
}
void runOpenDoor (int onOff){
if (onOff == 1){
digitalWrite(relayOpenPin, HIGH);
Serial.println("Opening Chicken Door");
delay (3000);
}
else{
digitalWrite(relayClosePin, LOW);
Serial.println("Door Open Off");
}
}
void runAllLights (int onOff){
if (onOff == 1){
digitalWrite(relayLightsPin, HIGH);
Serial.println("All Coop Lights Are On");
}
else{
digitalWrite(relayLightsPin, LOW);
Serial.println("All Coop Lights Are Off");
}
}
void runNightLight (int onOff){
if (onOff == 1){
digitalWrite(relayNightPin, LOW);
Serial.println("Coop Night Lihgt Is On");
}
else{
digitalWrite(relayNightPin, LOW);
Serial.println("Coop Night Light Is Off");
}
}
Thanks for your help