my code for timer from RTC

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

int hour1 = 2;
int min1 = 40;
int sec1 = 0;
int hour2 = 2;
int min2 = 39 ;
int sec2 = 10;

const int light = 13;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
   
RTC_DS1307 rtc;


void setup()
{
  
Serial.begin(9600);
Wire.begin();
rtc.begin();

pinMode(light, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
}

void loop()
{
  DateTime now = rtc.now();
   buttonState1 = digitalRead(2);
   buttonState2 = digitalRead(3);
   buttonState3 = digitalRead(4);
  if( buttonState1 == LOW)
    {   if(now.hour() == hour1 && now.minute()== min1 && now.second() == sec1)
        { 
          digitalWrite(light, HIGH);
        }
        if(now.hour() == hour2 && now.minute()== min2 && now.second() == sec2)
        {
          digitalWrite(light, LOW);
        }
    }

   if(buttonState2 == LOW) 
   
       { int x1 =  now.hour();
         int x2 =  now.minute();
        if (now.hour() == x1 && now.minute()== x2)
         {  
          digitalWrite(light, HIGH);
                          
        }
   if(now.hour() == hour2 && now.minute()== min2 && now.second() == sec2)
        {
          digitalWrite(light, LOW);
        }
    } 
     
    if(buttonState3 == LOW)
   
      {   int y1 =  now.hour();
          int y2 = now.minute();
       { if (now.hour() == y1 && now.minute()== y2)
         {  
          digitalWrite(light, LOW);
                          
        }
   if(now.hour() == hour1 && now.minute()== min1 && now.second() == sec1)
        {
          digitalWrite(light, HIGH);
        }
    } }
}   
[code]



pin 2 for pushbutton 1
pin 3 for pushbutton 2
pin 4 for pushbutton 3
   


my rerquirement:-
pin 2 for pushbutton 1: light should be on and off as per predefined time i.e. hour1, min1, sec1                  and hour2, min23 and sec2 respectively....

pin 3 for pushbutton 2: light should be on after pressing button and that time should be saved in x1, it should be off at hour2, min2, and sec2...and next time again it should be on at  x1...

pin 4 for pushbutton 3: light should be off after pressing button and that time should be saved in y1, it should be on at hour1, min1, and sec1...and next time again it should be off at y1 ...\



working of program:
pin 2 for pushbutton 1: working correctly
pin 3 for pushbutton 2: light is  starting immediately after it is in off condition...not on the time x1 and x2 saved
pin 4 for pushbutton 3  light is off immediately after it is in on condition...not on the time y1 and y2  saved

well what do you want ? comments?

ok

  • it's not great... You have some work ahead...

  • use code tag when you post

  • indent properly your code

  • use meaningful pin names, not pinMode(2, INPUT); what is connected to 2??

  • it's probably broken

  • learn how to code correctly by reading the manual for each function you call... for example   if(digitalRead(2 == HIGH)) is seen by the compiler as   if(digitalRead(0) == HIGH) probably not what you want... (same is true with if(digitalRead(3 == HIGH)))

what does this function definition do in the middle of the code?

 void loop2(){
        if (now.hour() == y)
         {  
          digitalWrite(light, LOW);
                          
        }

...

does this help?

thanx for your suggestion . can u plz check on this one also

check what?

why it is not working properly

where is the code?

and please properly formatted in the code tags so that it appears like this

// good code presentation here, properly indented
// extra kudos if it compiles

best to do understand how to post correctly is to read the article at the top of the forum Read this before posting a programming question ... and more importantly How to use this forum - please read.

You should fix the first post as well. as you can't click (at least not on my Mac) on the </> button to get the code tags, just put your code above as such:

 [code] your code here [/code]