code for automatic on off light with RTC

hello guys ....i uploaded some questions earlier but this time i got one step forward and reach to the end of my coding. the code is

#include <Arduino.h>
#include <Wire.h>
#include <RTClib.h>

const int light = 13;

int y1;
int y2;
int y3;

int z1;
int z2;
int z3;

int k1;
int k2;
int k3;

int buttonState1;
int buttonState2;
int buttonState3;

const int buttonPin[] = {2, 3, 4};
int buttonState = 0;


RTC_DS1307 rtc;


void setup()
{

  Serial.begin(9600);
  Wire.begin();
  rtc.begin();


  k1 = 0;

  k2 = 0;
  k3 = 0;
  buttonState1 = 1;
  buttonState2 = 1;
  buttonState3 = 1;
  pinMode(light, OUTPUT);
  for (int x = 0; x < 3; x++)
  {
    pinMode(buttonPin[x], INPUT);

  }

}

void RESET()
{
  DateTime now = rtc.now();
 
   if (now.hour() == 2   && now.minute() == 27 &&  now.second() == 0)
     {
    digitalWrite(light, HIGH);
     }

  if (now.hour() == 14 && now.minute() == 34 &&  now.second() == 0)
  {
    digitalWrite(light, LOW);

  }
     k1 = 1;               
}

void loop() {
  DateTime now = rtc.now();
  while (1)
  {

    buttonState1 = digitalRead(2);
    if (buttonState1 == LOW)
    {
      k1 = 1;
    }

    if (k1 == 1)
      RESET();

    buttonState2 = digitalRead(3);

    if (buttonState2 == LOW  )
    { int z1 = now.hour();
      int z2 = now.minute();
      int z3 = now.second();
      digitalWrite(light, HIGH);
      k2 = 1;
      k1 = 0;
      k3 = 0;
    }
 
    if (k2 == 1)
    {
      DateTime now = rtc.now();
      if (now.hour() == 5   &&  now.second() ==0)
      {
        digitalWrite(light, LOW);
      }
      if (now.hour() == z1  &&  now.second() == z3 )
      {
        digitalWrite(light, HIGH);
      }
                        k2 = 1;
    }

    buttonState3 = digitalRead(4);
    if (buttonState3 == LOW)
    {  
      int y1 = now.hour();
      int y2 = now.minute();
      int y3 = now.second();
      digitalWrite(light, LOW);
      k3 = 1;
      k1 = 0;
      k2 = 0;
    }
    if (k3 == 1)
    {
      DateTime now = rtc.now();
      if (now.hour() == y1 && now.minute() == y2 && now.second() == y3)
      {
        digitalWrite(light, LOW);
      }
      if (now.hour() == z1 && now.minute() == z2 && now.second() == z3)
      {
        digitalWrite(light, HIGH);
      }
              k3 = 1;
    }

  }

}

i am facing problem in storing time from RTC to uno board i.e. pin 3

Read about variable scope (int z1 or int y1 for example)

Why the redundant while(1) loop inside loop? Don't you know what the loop function does?

just tell me how can i store the time in z1 and y1 from RTC

pranav2016:
just tell me how can i store the time in z1 and y1 from RTC

Just learn the difference between global and local variables, as J-M-L hints.
Maybe something to do with your redeclaration of the variables in question?
(Your attitude could do with improvement, too. You were given perfectly good responses, and should have taken notice of them, rather than making demands.)

ok sry for that....
can u tell me where to declare those variables z ,y and HOW...?

JML already pointed about it.

int z1 = now.hour();[color=#222222][/color]
int z2 = now.minute();[color=#222222][/color]
int z3 = now.second();

you already declared z1, z2 and z3 as global scope, again you redeclared in local scope. Learn a little more about variable scopes.

I would say, you start with simple e.g. say reading the RTC and just on/off LED in say every 2 mins. This simple programs also help other members to quickly give some pointers rather than scanning through a large code base.

Also try to name the variables bit more clearly instead of z1, z2, etc.

pranav2016:
ok sry for that....
can u tell me where to declare those variables z ,y and HOW...?

You have them declared as globals at the top of the program, so you don't need to declare them agin in the function. That creates new variables of the same name, that go out of scope at the end of the function.
ie Since you have these global declarations:-

int y1;
int y2;
int y3;

int z1;
int z2;
int z3;

If you want to store the values in them for global use, you need to change these:-

int z1 = now.hour();
int z2 = now.minute();
int z3 = now.second();

to this:-

z1 = now.hour();
z2 = now.minute();
z3 = now.second();

and these:-

int y1 = now.hour();
int y2 = now.minute();
int y3 = now.second();

to this:-

y1 = now.hour();
y2 = now.minute();
y3 = now.second();

if you want the values to be stored in the global variables.
(That's what you want, if I understand your question.)
As your code stands, the stored values are only within scope until the end of each respective 'if' statement.

I think you'll have other problems, but that addresses the immediate one.

*sarouje just answered as I was typing, and said much the same, but now that I've typed this.....

yes i made changes as u said... i have used that function of reading the RTC time and blinking Led as per RTC time.....BUT now I want to store RTC time. my modified code is.....

</> #include <Arduino.h>
#include <Wire.h>
#include <RTClib.h>

const int light = 13;

int offbuttonhour;
int offbuttonminute;
int offbuttonsecond;

int onbuttonhour;
int onbuttonminute;
int onbuttonsecond;

int k1;
int k2;
int k3;

int buttonState1;
int buttonState2;
int buttonState3;

const int buttonPin[] = {2, 3, 4};
int buttonState = 0;

RTC_DS1307 rtc;

void setup()
{

Serial.begin(9600);
Wire.begin();
rtc.begin();

k1 = 0;

k2 = 0;
k3 = 0;
buttonState1 = 1;
buttonState2 = 1;
buttonState3 = 1;
pinMode(light, OUTPUT);
for (int x = 0; x < 3; x++)
{
pinMode(buttonPin[x], INPUT);

}

}

void RESET()
{
DateTime now = rtc.now();

if (now.hour() == 2 && now.minute() == 27 && now.second() == 0)
{
digitalWrite(light, HIGH);
}

if (now.hour() == 14 && now.minute() == 34 && now.second() == 0)
{
digitalWrite(light, LOW);

}
k1 = 1;
}

void loop() {
DateTime now = rtc.now();
while (1)
{

buttonState1 = digitalRead(2);
if (buttonState1 == LOW)
{
k1 = 1;
k2 = 0;
k3 = 0;
}

if (k1 == 1)
RESET();

buttonState2 = digitalRead(3);

if (buttonState2 == LOW )
{ onbuttonhour = now.hour();
onbuttonminute = now.minute();
onbuttonsecond = now.second();
digitalWrite(light, HIGH);
k2 = 1;
k1 = 0;
k3 = 0;
}

if (k2 == 1)
{
DateTime now = rtc.now();
if (now.hour() == 5 && now.minute() == 6 && now.second() ==0)
{
digitalWrite(light, LOW);
}
if (now.hour() == onbuttonhour&& now.minute() == onbuttonminute && now.second() == onbuttonsecond )
{
digitalWrite(light, HIGH);
}
k2 = 1;
}

buttonState3 = digitalRead(4);
if (buttonState3 == LOW)
{
offbuttonhour = now.hour();
offbuttonminute = now.minute();
offbuttonsecond = now.second();
digitalWrite(light, LOW);
k3 = 1;
k1 = 0;
k2 = 0;
}
if (k3 == 1)
{
DateTime now = rtc.now();
if (now.hour() == offbuttonhour && now.minute() == offbuttonminute && now.second() == offbuttonsecond)
{
digitalWrite(light, LOW);
}
if (now.hour() == onbuttonhour&& now.minute() == onbuttonminute && now.second() == onbuttonsecond)
{
digitalWrite(light, HIGH);
}
k3 = 1;
}

}

}

You already know you have to use code tags, yet you posted that mess above.
And you don't mention when or where you want to store it.
Furthermore, you didn't make the suggested changes - you just completely deleted all of those variables.

Maybe someone else will have more patience.

BUT now I want to store RTC time

Where you want to store the time?

With this code are you trying to learn about RTC? if not you dont need an RTC here, you already have two buttons which can control the LED anyway.

all buttons have their seprate functions dude...
i want to on/off led as per RTC timing. (for button 1 i.e. [pin 2])
i want to on led as button is pressed and off as per RTC timing. (for button 2 i.e. [pin 3])
i want to off led as button is pressed and on at the time when button 2 was pressed earlier. (for button 3 i.e. [pin 4])
i am only comparing hour, minute and second from RTC, not the date and year, so hour , minute and second will automatically repeats next days

This is much easier if you use UNIX timestamps. I'm not sure you are really ready for this demonstration, but you can see how easy it becomes when you don't worry about the time of day in terms of HH:MM:SS

If you can work you way through this, you may find it valuable.

You will have to add your library to the code and use its timestamp function:

you can test this with a few LEDs (or just use the on-board led on your arduino with the Serial monitor)

The clock starts at midnight...

#include <Time.h>

struct Timer{
  byte tHour, tMinute;
};

Timer redOnTime = {0,1};   //  on at 00:01 (times used for demonstration
Timer redOffTime = {0,10}; // off at 00:10  <<<<<see note below

Timer alarmSetTime = {0,3}; // 00:03 the alarm is set
unsigned long colorSwitchTime = 2 * 60UL; //two minutes to colorchange

bool lastRedState = false;
bool lastAlarmState = false;
unsigned long lastUpdateTime = 0;

const byte redPin = 2;
const byte greenPin = 3;
const byte bluePin = 4;

void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  delay(2000);
}

void loop() 
{
  bool redState = timerEvaluate(redOnTime.tHour, redOnTime.tMinute, redOffTime.tHour, redOffTime.tMinute);
  if (redState != lastRedState)
  {
    if (redState)
    {
      digitalWrite(13, HIGH);
      digitalWrite(redPin, HIGH);
      Serial.println("Red LED ON");
    }
    else
    {
      digitalWrite(13, LOW);
      digitalWrite(redPin, LOW);
      Serial.println("Red LED OFF");
    }
  }
  lastRedState = redState;
  
  bool alarmState = timerEvaluate(alarmSetTime.tHour, alarmSetTime.tMinute, colorSwitchTime);
  if(lastAlarmState != alarmState)
  {
    if(alarmState)
    {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin,HIGH);
      Serial.println("Blue LED ON");
    }
    else
    {
      digitalWrite(bluePin, LOW);
      digitalWrite(greenPin,HIGH);
      Serial.println("Green LED ON");
    }
  }
  lastAlarmState = alarmState;
  
  if (millis() - lastUpdateTime >1000UL)
  {
    char dateTime[125] = "";
    sprintf(dateTime, "Date: %02d/%02d/%d Time: %02d:%02d:%02d", month(), day(), year(), hour(), minute(), second());
    Serial.println(dateTime);
    lastUpdateTime += 1000;
  }
}

bool timerEvaluate(const byte startHour, const byte startMinute, const byte endHour, const byte endMinute)  // comparing time here is easier with Unix timestamps...
{
  time_t on_time = tmConvert_t(0, startMinute, startHour, day(), month(), year());
  time_t off_time = tmConvert_t(0, endMinute, endHour, day(), month(), year());
  time_t now_time = tmConvert_t(second(), minute(), hour(), day(), month(), year());
  //
  if (on_time < off_time)
  {
    return (now_time > on_time && now_time < off_time);
  }
  else if (off_time < on_time)
  {
    return (now_time > on_time || now_time < off_time);
  }
  else // if both on and off are set to the same time, I'm confused...
  {
    return false;
  }
}

bool timerEvaluate(const byte startHour, const byte startMinute, const unsigned long duration)  // comparing time here is easier with Unix timestamps...
{
  time_t on_time = tmConvert_t(0, startMinute, startHour, day(), month(), year());
  time_t off_time = tmConvert_t(0, startMinute, startHour, day(), month(), year()) + duration;
  time_t now_time = tmConvert_t(second(), minute(), hour(), day(), month(), year());  // or just use your RTC's unix timestamp
  //
  if (on_time < off_time)
  {
    return (now_time > on_time && now_time < off_time);
  }
  else if (off_time < on_time)
  {
    return (now_time > on_time || now_time < off_time);
  }
  else // if both on and off are set to the same time, I'm confused...
  {
    return false;
  }
}

time_t tmConvert_t(const byte ss, const byte mm, const byte hh, const byte DD, const byte MM, const byte YYYY)
{
  tmElements_t tm;
  tm.Year = YYYY-1970;
  tm.Month = MM;
  tm.Day = DD;
  tm.Hour = hh;
  tm.Minute = mm;
  tm.Second = ss;
  tm.Wday = NULL; // not needed for makeTime() but just to show you that is is part of the tmElements_t struct
  time_t timeStamp = makeTime(tm);
  return timeStamp;
}

time.h library is not installing on my pc thats why i am using RTClib.
is there any other way.

pranav2016:
time.h library is not installing on my pc thats why i am using RTClib.
is there any other way.

What do you mean by "not installing?

Can you give us more details? How did you try to install it?

@pranav2016, do not cross-post. Other thread removed.

i downloaded the time library.. unziped it in in the library storage folder. but still i cant see it in the IDE. and program containing time,h library are showing errors like "there is no such library such as time.h'

pranav2016:
i downloaded the time library.. unziped it in in the library storage folder. but still i cant see it in the IDE. and program containing time,h library are showing errors like "there is no such library such as time.h'

Enable verbose output during compilation, then show the actual error message, (and make sure it's in code tags).
There will be more clues than you've given.

But first, in the sketch, change this:-#include <Time.h>to this:-#include <TimeLib.h>

hey guys i made some code, can u tell me whats the the problem in there....

</>

#include <Arduino.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>

int x;

int i;
int j;
int k;

int val1;
int val2;
int val3;

const int light = 13;

int offbuttonhour;


int onbuttonhour;


int s1 ;
int s2 ;
int s3  ;

int buttonState1;
int buttonState2;
int buttonState3;

const int buttonPin[] = {2, 3, 4};


RTC_DS1307 rtc;


void setup()
{

  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  EEPROM.begin();

  s1 = 0;
  s2 = 0;
  s3 = 0;
  buttonState1 = 0;
  buttonState2 = 0;
  buttonState3 = 0
                 ;
  pinMode(light, OUTPUT);
  for ( x = 0; x < 3; x++)
  {
    pinMode(buttonPin[x], INPUT);
  }
  for ( i = 0; i <615; i++)
     {
      EEPROM.write(i,byte());
      }
  for ( j = 616; j < 819; j++)
      {
      EEPROM.write(j,int());
      }
  for ( k = 820; k < 1023; k++)
      {
      EEPROM.write(k,int());
      }
  val1 = EEPROM.read(i);
  val2 = EEPROM.read(j);
  val3 = EEPROM.read(k);
  
}



void RESET()
{
  DateTime now = rtc.now();

  if (1 <= now.hour()  && now.hour() <= 12)
  {
    digitalWrite(light, HIGH);
  }
  else
  {
    digitalWrite(light, LOW);
  }
  s1 = 1;
}


void ONN()
{
  DateTime now = rtc.now();

  if ( onbuttonhour <= now.hour() && now.hour() <= onbuttonhour + 12 )
  {
    digitalWrite(light, HIGH);
  }
  else
  {
    digitalWrite(light, LOW);
  }
  s2 = 1;
}


void OFFF()
{
  DateTime now = rtc.now();
  if (offbuttonhour <= now.hour() && now.hour() <= onbuttonhour)
  {
    digitalWrite(light, LOW);
  }
  else
  {
    digitalWrite(light, HIGH);
  }

  s3 = 1;
}

void loop() {

  DateTime now = rtc.now();
  {
    buttonState1 = digitalRead(2);
    if (buttonState1 == LOW or val1 == 0)

    {
      EEPROM.write(i,0);
      s1 = 1;
      s2 = 0;
      s3 = 0;
    
    if (s1 == 1)
   
      RESET();}




    buttonState2 = digitalRead(3);
    if (buttonState2 == LOW  or val1 == 1)

    { EEPROM.write(i,1);
      onbuttonhour = now.hour();
      EEPROM.write(j, onbuttonhour);
      s2 = 1;
      s1 = 0;
      s3 = 0;
   
    if (s2 == 1)
    
      ONN();
    }


    buttonState3 = digitalRead(4);
    if (buttonState3 == LOW or val1 == 2)
   
    { EEPROM.write(i,2);
      offbuttonhour = now.hour();
      EEPROM.write(k, offbuttonhour);
      s3 = 1;
      s1 = 0;
      s2 = 0;
    
    if (s3 == 1)
    
      OFFF();
      }
  }

  
}
[code]


all buttons have their seprate functions dude...
   i want to on/off led as per RTC timing. (for button 1 i.e. [pin 2])   
   i want to on led as button is pressed and off as per RTC timing. (for button 2 i.e. [pin 3])  
    i want to off led as button is pressed and on  at the time when button 2 was pressed earlier. (for button 3 i.e. [pin 4]) 
                                i am only comparing hour, minute and second from RTC, not the date and year, so hour , minute and second will automatically repeats next days