Hello. It is my very first project and I am using Arduino nano + ds1307 + 8 channel relay module for that. The code turns on the lights but it does not turn them off. and if I turn the Arduino on at any time between "Alarms" relay does not work properly. please help me to fix the code. Thank you.
PS: Why there is 6min difference between my system time and 1307??
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h>
const int Relay1 = 2;
const int Relay2 = 3;
const int Relay3 = 4;
const int Relay4 = 5;
const int Relay5 = 6;
void setup() {
// prepare pin as output
pinMode(Relay1, OUTPUT);
digitalWrite(Relay1, HIGH);
pinMode(Relay2, OUTPUT);
digitalWrite(Relay2, HIGH);
pinMode(Relay3, OUTPUT);
digitalWrite(Relay3, HIGH);
pinMode(Relay4, OUTPUT);
digitalWrite(Relay4, HIGH);
pinMode(Relay5, OUTPUT);
digitalWrite(Relay5, HIGH);
Serial.begin(9600);
// wait for Arduino Serial Monitor
while (!Serial) ;
// get and set the time from the RTC
setSyncProvider(RTC.get);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// to test your project, you can set the time manually
//setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
// create the alarms, to trigger functions at specific times
Alarm.alarmRepeat(23,1,0,Light1On); // xx:xxam every day
Alarm.alarmRepeat(23,3,0,Light2On);
Alarm.alarmRepeat(23,5,0,Light3On);
Alarm.alarmRepeat(23,7,0,Light4On);
Alarm.alarmRepeat(0,34,0,FanOn);
Alarm.alarmRepeat(7,0,0,Light1Off); // xx:xx -> x:xx every day
Alarm.alarmRepeat(7,0,0,Light2Off);
Alarm.alarmRepeat(7,0,0,Light3Off);
Alarm.alarmRepeat(9,0,0,Light4Off);
Alarm.alarmRepeat(0,30,0,FanOff);
}
void loop() {
digitalClockDisplay();
// wait one second between each clock display in serial monitor
Alarm.delay(3000);
}
// functions to be calRelay1 when an alarm triggers
void Light1On() {
// write here the task to perform every morning
Serial.println("Turn light1 on");
digitalWrite(Relay1, LOW);
}
void Light2On() {
Serial.println("Turn light2 on");
digitalWrite(Relay2, LOW);
}
void Light3On() {
Serial.println("Turn light3 on");
digitalWrite(Relay3, LOW);
}
void Light4On() {
Serial.println("Turn light4 on");
digitalWrite(Relay4, LOW);
}void FanOn() {
Serial.println("Turn light Veg. on");
digitalWrite(Relay5, LOW);
}
void Light1Off() {
// write here the task to perform every evening
Serial.println("Turn light1 off");
digitalWrite(Relay1, HIGH);
}
void Light2Off() {
Serial.println("Turn light2 off");
digitalWrite(Relay2, HIGH);
}
void Light3Off() {
Serial.println("Turn light3 off");
digitalWrite(Relay3, HIGH);
}
void Light4Off() {
Serial.println("Turn light4 off");
digitalWrite(Relay4, HIGH);
}
void FanOff() {
Serial.println("Turn Veg. off");
digitalWrite(Relay5, HIGH);
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}