Arduino If Statement not working for RTC

Hi guys, What my idea here is to rotate servo motor when the hour becomes 13. I am using a 1307 RTC module, Arduino UNO rev3 and SG90 Microservo. Please help with the code. I am using the RTC library from PaulShroffegen. Here is the link for the library.

#include <Servo.h>
#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>

Servo myservo;

int pos=0;

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
    myservo.attach(A3);
    setSyncProvider(RTC.get);
     bool parse=false;
     bool config=false;

    if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }
  Serial.begin(9600);
  while (!Serial) ;
  delay(200);
    
    }

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

void loop() {

  time_t t = now();
  
    int Minute = minute(t);
  
    int Second = second(t);
  
    int Hour = hour(t);
  
    int Day = day(t);

    int Week = weekday(t);

    int Month = month(t);

    int Year = year(t);


 if (Hour==13) {
    pos=180;
    delay(1000);
    pos=0;
   }
    
  delay(1000);
  myservo.write (pos);
  Serial.println(Hour);

}

So here is the code.
and here comes the problem for me.

if (Hour==13) {
pos=180;
delay(1000);
pos=0;
}

The position of the Servo doesn't change at all when the value of Hour variable becomes 13.

Serial.println(Hour);

Here, I have even looked in the Serial monitor. It shows fine that the current time's Hour vale is 13. But the servo is not changing its position from 0.\

Pls help me. I would really appreciate it. Thank you

You don't move your servo at 13:00, you only change the variable pos. After 1 sec you reset your variable pos to 0 and then move the servo - to '0' again.
Try this:

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.