do an action between two hours

Dear Friends,
In this opportunity I writing to you asking help in order to find a solution to this short sketch. The main purpose is to powering a sensor through the enabling of a pin according to the hour (starting and finish hour) given by a RTC.
Although the IDE can compile the sketch and the serial print can work, the snippet "if" statement does not work.
I appreciate so much your help.
Kind Regards,

#include <Sodaq_DS3231.h>

#include <Wire.h>


// set pin numbers:
const int ledPin = 13;
const int powerPin =  8;      // the number of the power pin

const uint8_t horaInicio = 17; // set thestating hour
const uint8_t horaFin = 19;  // set the finish hour


void setup()
{
  pinMode(powerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
}

void loop()
{
  DateTime now = rtc.now(); //get the current date-time

  Serial.print(now.hour(), DEC);


  Serial.println();

  delay (2000);

  if ( horaInicio <= now.hour() >= horaFin )
  {
    //power on
    digitalWrite (powerPin, HIGH);
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }

  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
  delay (1000);
}

powerPin_8.ino (771 Bytes)

 if ( horaInicio <= now.hour() >= horaFin )

That will not work.

I think maybe what you meant is this:

 if ((horaInicio <= now.hour()) && (now.hour() <= horaFin))

but I still think maybe that is not what you wanted.
Look again at this:

const uint8_t horaInicio = 17; // set thestating hour

const uint8_t horaFin = 19;  // set the finish hour

Do you want the light to be on from 17:00 to 19:00?
Or do you want it to be on from 17:00 through 19:59 (and turn off at 20:00)?
Think about it: if the time is 19:59, then now.hour() will still be 19.

Maybe what you want is this:

 if ((horaInicio <= now.hour()) && (now.hour() < horaFin))

Thank you! now it is working, i want that the sensor works from 17:00 to 19:59 (sensor turns off at 20)