RTC problems

I'm using a DS3231 RTC for my anniversary project (Anniversary Gift - Programming Questions - Arduino Forum) and for some reason my RTC will not retain what time it is. The RTC reads everything just fine when it is plugged in to the computer, and it runs each test program smoothly but it won't retain time when either the power is turned off or when I unplug it from the computer.

#include <Wire.h>
#include "RTClib.h"

#include <Servo.h>

#include <Adafruit_GFX.h>
#include <gfxfont.h>

#include "Adafruit_LEDBackpack.h"

#define DS3231_I2CIADDRESS 0x68

Servo giftgiver;
RTC_DS3231 rtc;
Adafruit_7segment matrix = Adafruit_7segment();


DateTime firstDate(2017, 1, 21, 0, 0, 0);

void setup() {
  #ifndef __AVR_ATtiny85__
      Serial.begin(9600);
      Serial.println("Display test");
  #endif
      matrix.begin(0x70);

  #ifndef ESP8266
    while (!Serial);
  #endif

  delay(3000);

  matrix.setBrightness(8);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  giftgiver.attach(9);   //Attach it in the loop if it's the 21 so you can save power
  giftgiver.write(0);
  
  if (rtc.begin()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    Serial.println(F("RTC TIME SET"));
    digitalWrite(LED_BUILTIN, HIGH);
  }
  if (rtc.lostPower()) {
    Serial.println("RTC LOST POWER");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
}

void loop() {
  DateTime current = rtc.now();
  Serial.print("Days: ");
  int daysSince = floor((current.unixtime() / 86400L) - (firstDate.unixtime() / 86400L));
  Serial.print(daysSince);
  matrix.print(daysSince, DEC);
  matrix.writeDisplay();
  if(current.day() == 9 && current.minute() == 27) {
    giftgiver.write(150);
    delay(1000);
  }
  delay(3600000);
  giftgiver.detach();
}

Do you have a battery on the rtc module? If not, add one. If you have one, replace it.

Which module (if you use a module)?

If by module you mean what RTC, it's the DS3231 (as mentioned above).

I do have a brand-new battery in it, but I don't think I have a replacement... is there a way to check if it works?

If it's worth noting, the compiler gives me a:

WARNING: Category 'Real-time clock' in library DS3231 is not valid. Setting to 'Uncategorized'

The DS3231 is the rtc chip. The module is e.g. http://m.gearbest.com/boards-shields/pp_248286.html.

If the module is a ZS-042, it is designed for use with a LIR2032, not a CR2032; it requires a modification for the latter.

Then no, I do not use a module.

Andromeda_:
Then no, I do not use a module.

OK feel free to share what you actually do have. You know, schematic, code. . . The usual.

but it won't retain time when either the power is turned off or when I unplug it from the computer.

These lines in your code are resetting the RTC time to the compile time every time you cycle the power. Set the time in the rtc , comment out the rtc.adjust() line, download the modified sketch.

 if (rtc.begin()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //comment out this line once rtc is set.
    Serial.println(F("RTC TIME SET"));
    digitalWrite(LED_BUILTIN, HIGH);
  }

cattledog:
These lines in your code are resetting the RTC time to the compile time every time you cycle the power. Set the time in the rtc , comment out the ret.adjust() line, download the modified sketch.

OOPS, I saw the lostPower but not that one. must stop using a cell phone for this site.

Good catch.

Andromeda_:
I'm using a DS3231 RTC for my anniversary project (Anniversary Gift - Programming Questions - Arduino Forum) and for some reason my RTC will not retain what time it is. The RTC reads everything just fine when it is plugged in to the computer, and it runs each test program smoothly but it won't retain time when either the power is turned off or when I unplug it from the computer.

Is there a backup battery inserted in the RTC module?

jurs:
Is there a backup battery inserted in the RTC module?

...and is the + side facing out?

Yes there is a backup, and yes the + is facing outwards.

What happens when you use the test sketches that come with the RTC library?

Everything works until I unplug it and try it on its own.

Andromeda_:
Everything works until I unplug it and try it on its own.

Okay, what is the voltage on the battery when it is standing alone?

Everything works until I unplug it

you have not been very clear about describing what the not correct time is after you re apply the power.

Have you fixed the code which resets the time to the original compile time when you unplug it?

If you have done that, when you plug it back in, does it come back with the time you unplugged it?

If so, then it is not running under battery power.

Andromeda_:
Everything works until I unplug it and try it on its own.

This line is copied from your setup function:

   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

This command tells the RTC something like:
DO NOT KEEP THE TIME, BUT
SET THE IME BACK ON EACH power-on or reset!

That way time and date will always be set back to the DATE and TIME when you compiled the sketch.

power-loss is not even required, a reset is enough to run the setup() function again and set time back.