DS3231 setting constraint

I am a beginner of Arduino. I simplified the program in attachment

the t.hour cannot change var to 1 even if the time is in the constraint,

may I get some help on it? thank you..

int var = 0;
#include <DS3231.h>
DS3231  rtc(SDA, SCL);
Time  t;
void setup() {
  Serial.begin(9600);
  rtc.begin();

  rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  rtc.setTime(2, 0, 0);     // Set the time to 12:00:00 (24hr format)
 rtc.setDate(3, 3, 2017);   // Set the date to January 1st, 2014

}

void loop() {
   t = rtc.getTime();
  if  (t.hour > 1) 
 {int var = 1;
 }
 
   Serial.println(var);
   delay(1000);

}
  Serial.println(var);

Which of the 2 variables named var are you printing ?

  if  (t.hour > 1)
  {
    int var = 1;  //remove the int declaration so that the code uses the global var variable
  }

actually there is a switch in below,

but the var keep not changing at the beginning of the code.

It means that if I set int var= 1,
the var will always be 1...

switch (var) {
      case 1:                                 //case 1 for time in 12am to 5am
        if(analogRead(A0) <500)              // motion senor sensitivity
              digitalWrite(30,HIGH);          //LED ON if sensing motion
         else
              digitalWrite(30,LOW);           //LED OFF if no motion detected          
        
        break;
        
        case 0:                                   // case 2 for other time period
         
          if(analogRead(LDRsensorPin) <700)          // if light blocked for below 700
          digitalWrite(30,HIGH);                  //LED ON
          else
          digitalWrite(30,LOW);                   //LED OFF
          break;    
  }
  }

thank you sir,
the problem solved :slight_smile:

I don't think that you are getting what I am saying. In your original code you have 2 different variables named var. One is a global and is set to 0 at the start of the program. The other is a local variable and it's value is set to 1 it t.hour() is greater than 1.

Which one are you printing in your original code and which one will be used by the switch/case ?

Did you try replacing

   int var = 1;

with

   var = 1;

as I suggested ?

You may also like to consider an else after the if to set its value to something else, maybe 0, but I am not sure what exactly you are doing.

(Later)
I see that the problem is solved but I will leave the reply above in place for the benefit of anyone reading the thread.