RTC module question


greetings.

i've purchased rtc implement clock system growbox.

however im not sure if im programming right.

the goal turn on light @ 5am, turn off 7pm. while pump runs 15mins, off 45mins, concurrently.

code: [select]
#include "wire.h"
#define ds1307_i2c_address 0x68

////convert normal decimal numbers binary coded decimal.
byte dectobcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

////convert binary coded decimal normal decimal numbers.
byte bcdtodec(byte val)
{
return ( (val/16*10) + (val%16) );
}

////stops clock, has side effect of setting seconds 0.
/*void stopds1307()
{
wire.begintransmission(ds1307_i2c_address);
wire.write(0);
wire.writewire.writewire.write(0x80);
wire.endtransmission();
}*/

////1) sets date , time on clock.
////2) starts clock.
////3) sets hour mode 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 bit 7 starts clock
wire.write(dectobcd(minute));
wire.write(dectobcd(hour)); ////if want 12 hours (am/pm) need set
////bit 6 (also need change getdateds1307)
wire.write(dectobcd(dayofweek));
wire.write(dectobcd(dayofmonth));
wire.write(dectobcd(month));
wire.write(dectobcd(year));
wire.endtransmission();
}

////reads date , time clock,
void getdateds1307(
byte *second,
byte *minute,
byte *hour,
byte *dayofweek,
byte *dayofmonth,
byte *month,
byte *year)
{

////reset 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 bits control bits.
*second = bcdtodec(wire.read() & 0x7f);
*minute = bcdtodec(wire.read());
*hour = bcdtodec(wire.read() & 0x3f); ////need change 12 hours (am/pm)
*dayofweek = bcdtodec(wire.read());
*dayofmonth = bcdtodec(wire.read());
*month = bcdtodec(wire.read());
*year = bcdtodec(wire.read());
}

////define light , pump pin outs.
int light1 = 8; //light relay
int light2 = 9;
int pump1 = 5; //waterpump relay
int pump2 = 6;

void setup() ////one time run @ 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 , time on clock.
////you need run first time setup rtc.
////set correct value below , un comment 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 @ 5am , off @ 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 im wrong. many in advance.

hi, welcome forum.

we can't answer question, since random code found somewhere on internet. have no idea if code good. did code ?


Arduino Forum > Using Arduino > Programming Questions > RTC module question


arduino

Comments