greetings.
I've recently purchased a RTC to implement clock system for my growbox.
However im not sure if im programming it right.
The goal is to turn on light at 5am, turn it off 7pm. while pump runs 15mins, off 45mins, concurrently.
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
////Convert normal decimal numbers to binary coded decimal.
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
////Convert binary coded decimal to normal decimal numbers.
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
////Stops the clock, but it has the side effect of setting seconds to 0.
/*void stopDs1307()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.writeWire.writeWire.write(0x80);
Wire.endTransmission();
}*/
////1) Sets date and time on the clock.
////2) Starts the clock.
////3) Sets hour mode to 24 hours.
void setDateDs1307(
byte second, ////0-59
byte minute, ////0-59
byte hour, ////1-23
byte dayOfWeek, ////1-7
byte dayOfMonth, ////1-28/29/30/31
byte month, ////1-12
byte year) ////0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); ////0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); ////If you want 12 hours (am/pm) you need to set
////bit 6 (also need to change getDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
////Reads date and time from the clock,
void getDateDs1307(
byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
////Reset the register pointer.
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
////A few of these need masks because certain bits are control bits.
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); ////Need to change this for 12 hours (am/pm)
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
////Define light and pump pin outs.
int light1 = 8; //light relay
int light2 = 9;
int pump1 = 5; //waterpump relay
int pump2 = 6;
void setup() ////One time run at each execution.
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
pinMode(light1, OUTPUT);
pinMode(light2, OUTPUT);
pinMode(pump1, OUTPUT);
pinMode(pump2, OUTPUT);
Wire.begin();
Serial.begin(9600);
////Set date and time on the clock.
////You only need to run this the first time you setup your RTC.
////Set the correct value below and un comment it to run it.
/*
second = 01;
minute = 32;
hour = 21;
dayOfWeek = 3;
dayOfMonth = 19;
month = 3;
year = 14;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
*/
}
////Main programm loop.
void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
////Serial Monitor Output.
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print("Date: ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.println(" ");
Serial.print("Day of week: ");
Serial.print(dayOfWeek, DEC);
Serial.println(" ");
Serial.print("Time: ");
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.println(" ");
delay(500);
//Turn On light at 5am and Off at 7pm daily.
if((dayOfWeek == 1) && (hour == 05) && (minute ==00) && (second == 00))
{
digitalWrite(light1,HIGH);
digitalWrite(light2,HIGH);
}
else { if((dayOfWeek == 1) && (hour == 19) && (minute ==00) && (second == 00))
{
digitalWrite(light1,LOW);
digitalWrite(light2,LOW);
}
}
if((dayOfWeek == 2) && (hour == 05) && (minute ==00) && (second == 00))
{
digitalWrite(light1,HIGH);
digitalWrite(light2,HIGH);
}
else { if((dayOfWeek == 2) && (hour == 19) && (minute ==00) && (second == 00))
{
digitalWrite(light1,LOW);
digitalWrite(light2,LOW);
}
}
if((dayOfWeek == 3) && (hour == 05) && (minute ==00) && (second == 00))
{
digitalWrite(light1,HIGH);
digitalWrite(light2,HIGH);
}
else { if((dayOfWeek == 3) && (hour == 19) && (minute ==00) && (second == 00))
{
digitalWrite(light1,LOW);
digitalWrite(light2,LOW);
}
}
if((dayOfWeek == 4) && (hour == 05) && (minute ==00) && (second == 00))
{
digitalWrite(light1,HIGH);
digitalWrite(light2,HIGH);
}
else { if((dayOfWeek == 4) && (hour == 19) && (minute ==00) && (second == 00))
{
digitalWrite(light1,LOW);
digitalWrite(light2,LOW);
}
}
if((dayOfWeek == 5) && (hour == 05) && (minute ==00) && (second == 00))
{
digitalWrite(light1,HIGH);
digitalWrite(light2,HIGH);
}
else { if((dayOfWeek == 5) && (hour == 19) && (minute ==00) && (second == 00))
{
digitalWrite(light1,LOW);
digitalWrite(light2,LOW);
}
}
if((dayOfWeek == 6) && (hour == 05) && (minute ==00) && (second == 00))
{
digitalWrite(light1,HIGH);
digitalWrite(light2,HIGH);
}
else { if((dayOfWeek == 6) && (hour == 19) && (minute ==00) && (second == 00))
{
digitalWrite(light1,LOW);
digitalWrite(light2,LOW);
}
}
if((dayOfWeek == 7) && (hour == 05) && (minute ==00) && (second == 00))
{
digitalWrite(light1,HIGH);
digitalWrite(light2,HIGH);
}
else { if((dayOfWeek == 7) && (hour == 19) && (minute ==00) && (second == 00))
{
digitalWrite(light1,LOW);
digitalWrite(light2,LOW);
}
}
for(int j=0;j<100;j++){
digitalWrite(pump1,HIGH);
digitalWrite(pump2,HIGH);
delay(9000);
}
for(int k=0;k<100;k++){
digitalWrite(pump1,LOW);
digitalWrite(pump2,LOW);
delay(27000);
}
delay(1000);
}
please enlighten me where im wrong. many thanks in advance.