Implementing an RTC Clock into a light sensor project

Hi friends,
(please forgive me if I break any of the forum norms as this is my first post :folded_hands: )
I am trying to encode a RTC(DS1307) into a project of mine that consists of a light sensor and a servo motor. The code right now is

#include <Servo.h>.      // include Servo library
Servo Switch;           // create a Servo object called Switch because it controls the basement switch
const int SensorPin = A0;  // Sensor pin
int sensor;                // Sensor reading
const int threshold = 500; // threshold to turn Servo
int servoAngle = 0;        // variable to set the angle for servo
void setup() {
  // put your setup code here, to run once:
  Switch.attach(9);           // attachs the servo on pin 9 to the myServo object
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:  
    sensor = analogRead(SensorPin);   // Stores light sensor value in a variable
    Serial.println(SensorPin);   // prints the analog representation of the light sensor
    servoAngle = map(SensorPin, 0, 1023, 0, 179);  // Scales light sensor value to the Servo
    if (sensor < threshold) { // when light is on
      Switch.write(180);          // tell servo to move 180 degrees(hit switch)
      delay(1000);                 // wait for 1 second
      Switch.write(0);            // tell servo to move back to 0 degrees
      delay(1000);                 // wait for 1 second
    }
}

However while this works and all, I need the loop part to run only when it is 9:00 pm. In other words I want the code to be something like "If it is 9:00 pm check if the light sensor detects light. If light is detected run the if code in my sketch". I am asking you guys because I do not know a lot of C++, only Python. I will add a schematic as soon as I make one. Also, I have tried many youtube tutorials and set and read the time on the DS1307 RTC. It matches the real time well enough.

Thank you.

We can't see which RTC library you're using. If you can read the time you should be able to extract the hour (either as part of the library (e.g. rtc.hour() or extract it from the displayed text representation of the time (less preferred in my opinion).

If you look at examples in the DS1307RTC you should be able to do what you want.

I am using the DS1307 RTC library by Michael Margolis

Are there any examples that show me how to code with it? Specifically with if else statements?

Did you look at the examples that come with the library? It looks like the ReadTest example shows everything that you need.

print2digits(tm.Hour);

The tm.hour is the current hour. Only basing your code on that will make that between 09:00 and 10:00 the code will continuously execute.

if (tm.hour == 9)
{
  do what needs to be done
  runs between 09:00 and 10:00
}

I'll leave the rest of the logic to you.

Someone beat me to it. Good luck.

I have but I couldn't understand the part I needed

Thank you I will try this and get back to you guys and close the discussion

I did add a little edit to my earlier reply.

Do you have any reservation to use DS3231 RTC which has alarm feature. You can prgram the RTC to genetae alarm at your desired time to interrupt the UNO R3 (?) and carry out the task.

This has not been tested but see if this helps:

lastRunDay = -1

loop forever:

    read current time from RTC

    if hour == 9 and minute == 0:
        if currentDay != lastRunDay:
            run cycle once
            lastRunDay = currentDay

    wait a short time

Here it is in C++ Arduino style:

int lastRunDay = -1;

void loop()
{
    DateTime now = rtc.now();

    if (now.hour() == 9 && now.minute() == 0)
    {
        if (now.day() != lastRunDay)
        {
            runCycle();
            lastRunDay = now.day();
        }
    }

    delay(1000);
}

This exact "time" (9h, 0m) lasts for one minute, so runCycle(); will be called many times. If you want to continually call runCycle() and lastRunDay = now.day(); ignore the rest of my post...

If you want to call runCycle() and lastRunDay = now.day(); one time... add a "run_one_time" flag, to ensure runCycle(); and lastRunDay = now.day(); are run only one time...

bool run_one_time = 0;

void setup() {
  // setup code
}

void loop() {
  DateTime now = rtc.now();
  if (now.hour() == 9 && now.minute() == 0) {
    if (now.day() != lastRunDay) {


      //////////////////////////////
      if (run_one_time == 0) { // flag is clear, meaning "ok to proceed"
        run_one_time = 1; // set flag to stop multiple calls...
        runCycle();
        lastRunDay = now.day();
      } // run_one_time
      //////////////////////////////


    } // lastRunDay
  } // now.minute
  //////////////////////////////
  else {
    run_one_time = 0; // clear flag when time != 9:00 to allow one-time calls 
  }
  //////////////////////////////

Hi all, I tried the first code by @sterretje and it worked and i tested it. It works fine and does what I want it to do. I will try your other ideas and experiment with it. @GolamMostafa i do not have a DS3231 RTC, only DS1307.

Also I will keep this topic open for now because I have another idea to add a motion sensor and buzzer.

It would be better to start a new topic if you run into a problem adding those two devices. You can always provide a link back if you think it matters, but I doubt it will make a difference.