issue with if statement

what i’m trying (and failing spectacularly) to achieve is for my relay (rele) to turn on during the 16 hour period defined between 5 am and 9 pm and off for the rest of the time. the issue is that it ignores the else if part of the code. thanks in advance for your help!!!

 RtcDateTime now = Rtc.GetDateTime();
  //encendido y apagado del led
  if (now.Hour() > 5 && now.Hour() < 21) {
      Serial.println("hola");
      digitalWrite(rele, HIGH);    
  }
 else if (now.Hour() < 21 && now.Hour() > 5) {
     Serial.println("chau");
     digitalWrite(rele, LOW);

  }

here is my code

//librerias
#include <Arduino.h>
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include "DHT.h"
#include "timer.h"
#include "TimerOne.h"
#include <Wire.h>
#include <RtcDS3231.h>

//conecciones
#define DHTTYPE DHT22
#define BLYNK_PRINT Serial
#define W5100_CS  10
#define SDCARD_CS 4
#define DHTPIN 2

int rele = 5;
int t2000, ft2000;
int sensorPin = 0;
int sensorValue = 0;
int ht = 0;
int tref = 0;
boolean Encendido = false;
char auth[] = "";

DHT dht(DHTPIN, DHTTYPE);
RtcDS3231<TwoWire> Rtc(Wire);

void setup() {
  pinMode(rele, OUTPUT);
  Serial.begin(9600);
  //--------RTC SETUP ------------
  Rtc.Begin();
  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
  RtcDateTime now = Rtc.GetDateTime();
  if (now < compiled)
  {
    Rtc.SetDateTime(compiled);
  }
  Rtc.Enable32kHzPin(false);
  Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
  //--------BLYNK SETUP-----------
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
 // Blynk.begin(auth);
}


void loop() {
  //Blynk.run();
  timer_update(&ft2000, &t2000, timeout_t2000);
  RtcDateTime now = Rtc.GetDateTime();
  //encendido y apagado del led
  if (now.Hour() > 5 && now.Hour() < 21) {
      Serial.println("hola");
      digitalWrite(rele, HIGH);    
  }
 else if (now.Hour() < 21 && now.Hour() > 5) {
     Serial.println("chau");
     digitalWrite(rele, LOW);

  }
}

void timeout_t2000() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
  if (isnan(h) || isnan(t)) {
    return;
  }
  sensorValue = analogRead(sensorPin);
  ht = 1023 - sensorValue;
  Blynk.virtualWrite(V3, ht );
  timer_reset(&ft2000, &t2000, 2000);
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
  char datestring[20];

  snprintf_P(datestring,
             countof(datestring),
             PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
             dt.Month(),
             dt.Day(),
             dt.Year(),
             dt.Hour(),
             dt.Minute(),
             dt.Second() );
  Serial.print(datestring);
}
  if (now.Hour() > 5 && now.Hour() < 21) {
....
 else if (now.Hour() < 21 && now.Hour() > 5) {

Do you see some difference between those two sets of conditions? I sure don’t…

Regards,
Ray L.

If the hour value is not between 5 and 21, why do you need to perform another test?

  if (now.Hour() > 5 && now.Hour() < 21)
  {
      Serial.println("hola");
      digitalWrite(rele, HIGH);   
  }
  else
  {
     Serial.println("chau");
     digitalWrite(rele, LOW);
  }

Or more simply:

  digitalWrite (rele, (5 <= now.Hour() && now.Hour() < 21)) ;

(Simpler, and more correct as it will turn on at 5am, not 6am)

(Simpler, and more correct as it will turn on at 5am, not 6am)

More correct, possibly, but not simpler. More compact != simpler. It does not do everything that the original code does, and requires more study (on my part, anyway). I like my comparisons to happen in an if statement.

Arguable - conditional execution is much harder to reason about than boolean valued expressions, in the general case. More clear would be to use a predicate function:

bool is_daytime_hour (int hour)
{
  return hour >= 5 && hour < 21 ;
}


void loop ()
{
  .....
  .....
  digitalWrite (rele, is_daytime_hour (now.Hour()) ;
}