Turning on and off during times of the day using "if" statement

I am using a DS3231 RTS to try and make my arduino project operate during certain hours of the day (8am - 7pm). I have the libraries for the RTS, I can set the time and view the time etc. That all works good.
The issue is that whatever time of day you plug the device in (before 8am or after 8am) it executes that portion of code and never changes (even after it turns 8am or after it passes 7pm)

My understanding of "if" statements is that if it is true, execute, else do something else. But it only seems to view this if statement once (at startup) and then never again. Is there a way to make it check this if statement every scan to ensure it changes state when I want it to?

I can view the time in the serial monitor as well as the hour, it is in 24 hour format. Here is a simple version of my program that only focuses on the issue;

 #include "DS3231.h" 
 DS3231  rtc(SDA, SCL);    
 Time t;

void setup(){
rtc.begin(); 
t = rtc.getTime(); 
}

void loop(){
if (t.hour >= 8 && t.hour< 19){
      //(turn sensors on, lights blink, normal program operation)
}
else{
      //(sensors off, lights off to conserve power)
}
}

One popular approach is to keep time in minutes past midnight (there are 1440 of them).

Using the loop function, while doing other things, you can continuously read the time and check if the on/off status is correct. If not, change it.

For example, if a device is supposed to be on between 8 AM and 8 PM (minutes 480 and 1200) your "if" could be:

void loop() {
int time;
...
time = get_minutes_since_midnight();
if (time >= 480 && time < 1200) device_on();
else device_off();
...
}

You need to update t in the loop, the way you have it now the time is read once in setup then t never changes afterwards.

david_2018:
You need to update t in the loop, the way you have it now the time is read once in setup then t never changes afterwards.

I see, I tried moving t=rtc.getTime(); from the setup to void, but still not change. How can I force the time value to update?

I thought it was updating because I print the time value to the serial monitor and can see it counting, but I guess it is not updating the t structure

jmooreiai:
Here is a simple version of my program that only focuses on the issue;

You've gone too far. The code you posted is so simple that it can no longer demonstrate the problem. The idea is to post minimal code that's just enough to demonstrate the problem.

For you, the simplest help option is to just post the entire, non-working sketch.

aarg:
For you, the simplest help option is to just post the entire, non-working sketch.

It is over the 9000 character limit, so I will have to remove comments and whatever I can to save space

 #include <avr/sleep.h>     //Library for putting the CPU to sleep
 
 #define trigPin 5          //Sensor 1
 #define echoPin 6
 #define trigPin2 9         //Sensor 2
 #define echoPin2 10

 #include "DS3231.h"        //Library for RTC

 DS3231  rtc(SDA, SCL);     //Communication pins to the DS3231 RTC (A4 and A5)
 Time t;

int peopleInRoom = 0;       //How many people are currently present in the room
int detectRange = 25;       //Distance in cm that the sensors will count
bool active1 = 0;                       //Activated when sensor 1 detects presence
bool active2 = 0;                       //Activated when sensor 2 detects presence

/*7-SEGMENT DISPLAY*/
int A = 3;
int B = 7;
int C = 8;
int D = A1;
int E = 4;
int F = 11;
int G = 12;

int redLED = A0;            //Turns on after max limit reached

unsigned long previousSlow = 0;
int slowDelay = 4000;

unsigned long previousQuick = 0;
int quickDelay = 1000;

unsigned long previousLED = 0;
int LEDblinkrate = 500;

unsigned long previousCount1 = 0;
unsigned long previousCount2 = 0;
int countDelay = 3000;


void setup() {
/*----------------------------------------------------------------------------------*/ 
  //CLKPR = 0x80;         //These 2 lines slow down the processor speed by a factor of 0.25
  //CLKPR = 0x02;         //^ used to save power, but conflicts with the RTC module
Serial.begin(115200);     //Set the serial monitor baud rate
rtc.begin();              //Initialize the RTC
t = rtc.getTime();        //Extract individual time values to the t structure

  /*To set the date/time;
  -Uncomment the following; set the values and upload
  -Re-comment out the variables, then re-upload*/
  //rtc.setDOW(MONDAY);           // Set Day-of-Week to ex. SUNDAY
  //rtc.setTime(21, 23, 30);     // Set the time 12:00:00 (24hr format)
  //rtc.setDate(15, 6, 2020);   // Set the date MM, DD, YYYY

 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);

  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);

  pinMode(redLED, OUTPUT);
}


void loop() {
/*----------------------------------------------------------------------------------*/ 
  long duration, distance;
  long duration2, distance2;
  bool toggle = 0;                        //Used to blink the 7-segment display
  unsigned long currentMillis = millis(); //Start timing to avoid using delays


//if (t.hour >= 7 && t.hour < 17 && t.dow != 6 && t.dow != 7){         //Set the hours of operation
if (t.sec >= 0 && t.sec < 30){

/*MAXIMUM PEOPLE REACHED RED LED*/
  if (peopleInRoom >= 8) {                //Change this value to set room limit
    if ((unsigned long)(currentMillis - previousLED) >= LEDblinkrate) {  
      previousLED = currentMillis;
      digitalWrite(redLED, !digitalRead(redLED));
    }
  }
  else {
    digitalWrite(redLED, LOW);
  }
  
/*RUN SENSORS, DETERMINE DISTANCE*/
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration) / 29.1;

  delay(600);                             //Delay between sensor triggers to reduce interference
 
  digitalWrite(trigPin2, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2) / 29.1;

/*IF DISTANCE LESS THAN DETECTRANGE, COUNT*/
 if (distance <= detectRange && peopleInRoom > 0 && distance2 > detectRange && active1 == 0){
            peopleInRoom--;
            active1 = 1;
            //delay(200);
        }
        
 if (distance2 <= detectRange && peopleInRoom < 9 && distance > detectRange && active2 == 0){
            peopleInRoom++;
            active2 = 1;
            //delay(200);
        }

if (!active1){
  previousCount1 = currentMillis;
}
if (!active2){
  previousCount2 = currentMillis;
}

if (active1 == 1){
  if ((unsigned long)(currentMillis - previousCount1) >= countDelay) {  
    previousCount1 = currentMillis;
    active1 = 0;
  }
}

if (active2 == 1){
  if ((unsigned long)(currentMillis - previousCount2) >= countDelay) {  
    previousCount2 = currentMillis;
    active2 = 0;
  }
}

/*BLINK 7-SEGMENT DISPLAY*/
if (distance < detectRange || distance2 < detectRange){               //Turns bit on when presence detected
  toggle = 1;    
}
if ((unsigned long)(currentMillis - previousQuick) >= quickDelay) {   //Toggles bit when no presence
    previousQuick = currentMillis;
    if (distance > detectRange && distance2 > detectRange){
      toggle = !toggle;
    }
}

if (active1 == 0 && active2 == 0){                                    //Decreases blink rate when no presence
    if ((unsigned long)(currentMillis - previousSlow) >= slowDelay) {
    previousSlow = currentMillis;
    quickDelay = slowDelay;
    }
}

/*SET EACH NUMBER OF 7-SEGMENT DISPLAY*/
if (toggle == 1){
 if (peopleInRoom == 0){
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, LOW);    
  }
   else if (peopleInRoom == 1){
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
  }
   else if (peopleInRoom == 2){
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, LOW);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
  }
   else if (peopleInRoom == 3){
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
  }
   else if (peopleInRoom == 4){
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }
   else if (peopleInRoom == 5){
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }
   else if (peopleInRoom == 6){
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }
   else if (peopleInRoom == 7){
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
  }
  else if (peopleInRoom == 8){
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }
  else if (peopleInRoom == 9){
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }
}
  else {
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
  digitalWrite(E, LOW);
  digitalWrite(F, LOW);
  digitalWrite(G, LOW);
  }  
}

else{
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
  digitalWrite(E, LOW);
  digitalWrite(F, LOW);
  digitalWrite(G, LOW);
  digitalWrite(redLED, LOW);
  Serial.print("Goodnight!");
  Serial.print("       ");
  Serial.print(rtc.getDOWStr());      // Send Day-of-Week
  Serial.print(" ");
  Serial.print(rtc.getDateStr());     // Send date
  Serial.print(" -- ");
  Serial.println(rtc.getTimeStr());   // Send time
  delay(60000);
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  digitalWrite(C, HIGH);
  digitalWrite(D, LOW);
  digitalWrite(E, HIGH);
  digitalWrite(F, HIGH);
  digitalWrite(G, LOW);
  delay(500);
}
}

jmooreiai:
It is over the 9000 character limit, so I will have to remove comments and whatever I can to save space

Ah, too late but it's fine if you just post it as an attachment. Much safer, because there is no risk that you accidentally removed something vital.

I do see that you have not yet moved the RTC update of variable 't' into the loop() function...

so I will have to remove... whatever I can to save space

Did that include the call to rtc.getTime() within loop()?

aarg:
Ah, too late but it's fine if you just post it as an attachment. Much safer, because there is no risk that you accidentally removed something vital.

I do see that you have not yet moved the RTC update of variable 't' into the loop() function...

I did try moving "t = rtc.getTime();" to the loop function, but didnt find it made any difference so I put it back in setup as that it where it was originally. I can move it back to void loop, but the program still only checks the time once at startup and then never again..

I did try moving "t = rtc.getTime();" to the loop function

That is what everyone else would do.

Post the revised code, and explain why you think it did not work. Post output examples supporting your point.