need help using rtc

hello all this is my first project using arduino uno,and pls forgive my super English skills.

i am making a timer switch using rtcDS3231 , relay module and a 20x4 lcd using i2c.

i have written a program to start a relay at certain time and turn it off ,
but my problem is when the relay is on at given time but when power goes off and comes back mean it is not turning on
Example:-

i programmed to on on 6am in morning and turn off at 4pm , if the power goes off in between the on time it is not turning of after the power comes

my code follows:

#include <TimeLib.h>

#include <Wire.h>

#include “RTClib.h”
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
RTC_DS1307 RTC;

void setup () {

Wire.begin();
RTC.begin();
lcd.init(); // initialize the lcd
lcd.backlight();
if (! RTC.isrunning())
{
Serial.println(“RTC is NOT running!”);
}
//Any pin. I have used Pin 4
pinMode(4, OUTPUT);
}

void loop () {

DateTime now = RTC.now();

lcd.clear();

lcd.setCursor(0, 0);

lcd.print(now.year(), DEC);

lcd.print(’/’);

lcd.print(now.month(), DEC);

lcd.print(’/’);

lcd.print(now.day(), DEC);

lcd.setCursor(0, 1);

lcd.print(now.hour(), DEC);

lcd.print(’:’);

lcd.print(now.minute(), DEC);

lcd.print(’:’);

lcd.print(now.second(), DEC);

delay(1000);
if (now.hour() == 6 && now.minute() == 10& now.second() == 0)
{

digitalWrite(4,1);

}

else if (now.hour() == 16 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(4, 0);

}

}

please help meeeeeeeeee

   if (now.hour()  == 6 && now.minute() == 10& now.second() == 0)
    {
      digitalWrite(4,1);
    }
    else if (now.hour() == 16 & now.minute() == 0 & now.second() == 0)
    {
      digitalWrite(4, 0);
    }

I don't know what your fascination with useless white space is, but, this is the code that diddles with the relay. Note that it does NOT turn the relay on anytime between 6:10 and 16:00. It (tries to) turn the relay on AT 6:10, and off AT 16:00.

First, you need to use && everywhere. Second you need to turn the pin on when the time is greater than or equal to 6:10 and less than or equal 16:00.

please teach me or give an example

PaulS:

   if (now.hour()  == 6 && now.minute() == 10& now.second() == 0)

{
      digitalWrite(4,1);
    }
    else if (now.hour() == 16 & now.minute() == 0 & now.second() == 0)
    {
      digitalWrite(4, 0);
    }



I don't know what your fascination with useless white space is, but, this is the code that diddles with the relay. Note that it does NOT turn the relay on anytime between 6:10 and 16:00. It (tries to) turn the relay on AT 6:10, and off AT 16:00.

First, you need to use && everywhere. Second you need to turn the pin on when the time is [u]greater than or[/u] equal to 6:10 [u]and less than or[/u] equal 16:00.

give an example

   if(now.hour() >= 6 && now.minute() >= 10 && now.hour() < 16)
   {
      digitalWrite(relayPin, HIGH);
   }
   else
   {
      digitalWrite(relayPin, LOW);
   }

i need to make a planted aquarium so i need a timer switch code

i need to make a planted aquarium so i need a timer switch code

I have no idea what this statement means, in relation to the code you posted.

syedthedevil:
but my problem is when the relay is on at given time but when power goes off and comes back mean it is not turning on

The switching conditions you are using are only working if the device is powered-on at that time and you pass the exact switching time.

Alternative switching logic:

int minuteToday=now.hour() *60 + now.minute();
byte onState= (minuteToday>=6*60+10) && (minuteToday<16*60); // on from 06:10 until 15:59
digitalWrite(4,onState);

sorry but can u explain me the code

PaulS:

   if(now.hour() >= 6 && now.minute() >= 10 && now.hour() < 16)

{
      digitalWrite(relayPin, HIGH);
  }
  else
  {
      digitalWrite(relayPin, LOW);
  }

can u explain me the code

No. There is nothing to explain. Look up, on the reference page, any part that is not clear.

thank you thank you thank you

the code works :smiley:

and i can understand the code(so simple and perfect)