0

PCF8523 Real Time Clock Not Updating and "if statement" prob
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PCF8523 Real Time Clock Not Updating and "if statement" prob

by KylePo on Tue Dec 18, 2018 9:30 pm

Hey Everybody,

So I recently bought a PCF Real time clock module for my Arduino nano. The intended purpose is to cause an action to happen two times a day. I have two problems. My code is pasted below. One is that when I look in the serial monitor, the time will constantly reset itself every time I close and reopen it. I just want to make sure that when the device is autonomous it will actually count the time. The second thing I need help with is the if-statement in the void loop. It does not work, and I never see the then-statement in my code. Is there a problem with how I am using the now function? Thanks!



Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include "RTClib.h"

RTC_PCF8523 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int timer = 0;
int GREEN_LED = 12;
int RED_LED = 11;

int BUTTON = 4;

void setup() {
  while (!Serial) {
    delay(1);  // for Leonardo/Micro/Zero
  }
  Serial.begin(57600);
  if (! rtc.begin()){
    Serial.println("Couldn't find RTC");
    while(1);
  }
  if (! rtc.initialized()) {
    Serial.println("RTC is NOT currently running");
  }
  rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
 
void loop() {
  DateTime now = rtc.now();
  /*Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(") ");*/
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  if ((now.minute(), DEC) == 1) {
    Serial.println("Yay!"); //not working
  }
 
  delay(3000);
 
}

KylePo
 
Posts: 2
Joined: Tue Dec 18, 2018 9:25 pm

Re: PCF8523 Real Time Clock Not Updating and "if statement"

by adafruit_support_bill on Tue Dec 18, 2018 9:50 pm

This line in your setup is resetting the time whenever setup runs:
Code: Select all | TOGGLE FULL SIZE
  rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

You only want to do that if the clock is not running already.

Not sure what you are trying to test in this line:
Code: Select all | TOGGLE FULL SIZE
if ((now.minute(), DEC) == 1)

It is not clear what the expression "(now.minute(), DEC) " will evaluate to. But it probably isn't '1'.

adafruit_support_bill
 
Posts: 74378
Joined: Sat Feb 07, 2009 10:11 am

Re: PCF8523 Real Time Clock Not Updating and "if statement"

by KylePo on Tue Dec 18, 2018 10:21 pm

Thank you! How do I prevent it from constantly resetting while still setting it to the time I want? And where can I find information on how to turn the time into a variable?

KylePo
 
Posts: 2
Joined: Tue Dec 18, 2018 9:25 pm

Re: PCF8523 Real Time Clock Not Updating and "if statement"

by adafruit_support_bill on Wed Dec 19, 2018 6:57 am

How do I prevent it from constantly resetting while still setting it to the time I want?

Only call rtc.adjust if the clock is not already initialized. See the example code from the library.
Code: Select all | TOGGLE FULL SIZE
  if (! rtc.initialized()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }


And where can I find information on how to turn the time into a variable?

Not sure I understand your question. You already have a 'now' variable defined in your loop():
Code: Select all | TOGGLE FULL SIZE
  DateTime now = rtc.now();

adafruit_support_bill
 
Posts: 74378
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.