Coding not working properly

Hi,

I am trying to get a set of LED strip lights to turn on at 8pm and off at 9pm on a specific day.
I have wired everything up and the wiring is correct as if I set Relay to HIGH the LED's come on.
It is now after 10pm and the lights are still on.
Please help.

#include <DS3231.h>

int Relay = 4;
String today = "";

DS3231  rtc(SDA, SCL);
Time t;

void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
  delay (2000);
}

void loop() {

  today = rtc.getDOWStr();
  
  if (today == "Wednesday") {
  
      if ( 20 < t.hour && t.hour < 21); {
        
        digitalWrite (Relay, HIGH); 
      }
     
  }
 
}
if ( 20 < t.hour && t.hour < 21);

Classic mistake

Remove the semicolon at the end of the line

Thanks
How about now.

#include <DS3231.h>

int Relay = 4;
String today = "";

DS3231  rtc(SDA, SCL);
Time t;

void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
  delay (2000);
}

void loop() {

  today = rtc.getDOWStr();

  if (today == "Tuesday") {

    if ( 20 < t.hour && t.hour < 23) {

      digitalWrite (Relay, HIGH); 
    }
  }

  else if (today == "Wednesday"); {

    if ( 20 < t.hour && t.hour < 23) {

      digitalWrite (Relay, HIGH); 
    }
  }
}

Do you understand why the semicolon caused a problem ?

As well as the semicolon, where do you turn the relay off?

It is set to LOW in the void setup.
I thought that means that whenever it is not set to high, it is automatically low.

Frank16616:
It is set to LOW in the void setup.
I thought that means that whenever it is not set to high, it is automatically low.

When you issue the digitalWrite command it changes to that state and stays in the that state. You don't have to hold it there. Set and forget, or set-time-change.

Can you elaborate?
I don't understand what you mean.

digitalWrite(Relay, LOW); sets the pins low and it will stay low until the program reaches a digitalWrite(Relay, HIGH); instruction. It will then stay high until the program reaches a digitalWrite(Relay, LOW) instruction.

In you program it will start LOW and when the if statement is true the pin will change to HIGH and stay HIGH, until a LOW command is reached (of which you don't have any).

In your thought process the pin would only stay high for the length of the instruction (like 4 microseconds), and you would never see it change to HIGH.

For more information, look under the Resources -> Reference for information on the digitalWrite instruction.

Ok, I understand.

Where and how should I add the digitalWrite LOW command?
Should I add it to each of the two IF statements?

I have tried adding an else to the two if statements working with the timings, but it won't work properly.

If you reword you functionality, the if statement becomes clearer.

You want the light on during the 8pm hour.

therefore:

if hour==20 then HIGH
else then LOW

I am just using the hour for trails. I want it to be on for around 9 hours from 3 pm to midnight and in the second if, the second day from 11 am to 6 pm.

#include <DS3231.h>

int Relay = 4;
String today ;                           //the ="" is useless. 

DS3231  rtc(SDA, SCL);
Time t;

void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
  delay (2000);
}

void loop() {

  today = rtc.getDOWStr();

  if (today == "Tuesday") {

    if ( 20 < t.hour && t.hour < 23) {
      digitalWrite (Relay, HIGH);
    }

    else{                         //if you "if" conditions are not fulfilled, then turn relay off
      digitalWrite (Relay, LOW);
    }
  }

  if (today == "Wednesday") {          //  "else" is useless "if" is enough

    if ( 20 < t.hour && t.hour < 23) {

      digitalWrite (Relay, HIGH);
    }
     else{

      digitalWrite (Relay, LOW);
    }
}
 
}

try this, it should work !

this code basically says :

check day
if day is ok, check time
if time is ok, turn on
otherwise, turn off

hope that helped!