Converting code from ds1307 to ds3231

i just want to make sure before i switch my RTC to the DS3231 that my code will still work for my feeder. Currently using the DS1307 which i have been less than thrilled about. Seems as if its constantly loosing the time.

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <TimeAlarms.h>
#include <Time.h>

RTC_DS1307 RTC;
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
int motorPin = 2;
int lastHour = 24;
int lastMinute = 60;

bool feedPet = false;
unsigned long feedTime = 0;
unsigned long lastPrintTime = 0;

struct FeedTime{
  int hour, minute;
};

FeedTime amFeed = {10, 0};  // i.e. 10:00am
FeedTime pmFeed = {17,30};  // i.e. 5:30pm

void setup ()
{
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  lcd.begin(16, 2);
  Wire.begin();
  RTC.begin();
  if (! RTC.isrunning()) 
  {
    lcd.println("RTC NOT Running!");
    RTC.adjust(DateTime((__DATE__), (__TIME__)));
  }
}

void loop ()
{
  DateTime now = RTC.now();
  FeedTime currentTime;
  currentTime.hour = now.hour();
  currentTime.minute = now.minute();
  if((currentTime.minute != lastMinute) && (((currentTime.hour == amFeed.hour) && (currentTime.minute == amFeed.minute)) || ((currentTime.hour == pmFeed.hour) && (currentTime.minute == pmFeed.minute))))
  {
    feedTime = millis();
    feedPet = true;
  }
  lastMinute = currentTime.minute;
  if (feedPet)
  {
    turnFeeder();
  }
  if (millis() - lastPrintTime > 1000UL)
  {
    lcd.setCursor(0, 0);
    char nowDate[24] = "";
    sprintf(nowDate, "DATE: %02d/%02d/%d", now.month(), now.day(), now.year());
    lcd.print(nowDate);
    // display the time
    lcd.setCursor(0, 1);
    char nowTime[24] = "";
    sprintf(nowTime, "Time: %02d:%02d:%02d", now.hour(), now.minute(), now.second());
    lcd.print(nowTime);
    lastPrintTime = millis();
  }
}

void turnFeeder(void)
{
  static bool pinState = true;
  if (millis() - feedTime < 60000UL) // 60 second(s)
  {
    if (pinState)
    {
      digitalWrite(motorPin, HIGH);
      pinState = false;
    }
  }
  else
  {
    digitalWrite(motorPin, LOW);
    pinState = true;
    feedPet = false;
  }
}

To change from using a DS1307 RTC to an DS323x RTC, it is only necessary to use #include <DS3232RTC.h> instead of #include <DS1307RTC.h>.

when i just made the switch i get these errors....

Arduino: 1.7.7 (Windows 7), Board: "Arduino Uno"

FINAL___.ino: In function 'void setup()':

FINAL___.ino:30:7: error: 'class DS3232RTC' has no member named 'begin'

FINAL___.ino:31:13: error: 'class DS3232RTC' has no member named 'isrunning'

FINAL___.ino:34:9: error: 'class DS3232RTC' has no member named 'adjust'

FINAL___.ino: In function 'void loop()':

FINAL___.ino:40:22: error: 'class DS3232RTC' has no member named 'now'

Error compiling.

A DS3231 RTC will run just fine with the following declarations

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

If you are familiar with RTClib.h there is no need to stop using it, an you can treat the DS3231 as if it were a DS1307.

If you want a library which can easily address more of the capabilities of the DS3231, like the Alarms, I would suggest Jack Christensens library for the DS3231/DS3232.

The Christensen library has different syntax than RTClib, and is similar to the standard Margolis library for the DS1307.

which RTC is better the DS3231 or DS3232? i know they minor differences between the two, i just can't make my mind up on which one i should use, if it even really matters at all......... i just want to get rid of this DS1307 for good!

which RTC is better the DS3231 or DS3232? i know they minor differences between the two, i just can't make my mind up on which one i should use, if it even really matters at all......... i just want to get rid of this DS1307 for good!

The DS3231 has the same features as the DS3232 except: (1) Battery-backed SRAM, (2) Battery-backed 32kHz output (BB32kHz bit in Control/Status register 0x0F), and (3) Adjustable temperature sensor sample rate (CRATE1:0 bits in the Control/Status register).

There is no difference in the time keeping. The DS3231 is less expensive and readily available, but there are probably pirate versions and out of spec chips in some of the lowest cost ebay modules. I've not had problems with the modules labeled ZS-042.

while were on the subject, do you think my coding looks fine? i cant help but think im not clearing something or that i forgot some sort of code to make the RTC sync time properly, im afraid i'll get the DS3231 and have the same problems i am having now, which is that they initial time syncing is off by about a minute....

if (! RTC.isrunning()) 
  {
    lcd.println("RTC NOT Running!");
    RTC.adjust(DateTime((__DATE__), (__TIME__)));
  }

Because the RTC is running, you are never actually resetting it to the compile time, and it is likely to be getting further away from your computer time. Setting with the computer time will always be a few seconds late, because of the compile time, upload time, and the reset when opening the monitor.

Get rid of the conditional test, and just set the RTC to compile time once. Then comment out the line in our sketch.

  {
    RTC.adjust(DateTime((__DATE__), (__TIME__)));
  }

To be more precise, you will need to use one of the other forms of RTC.adjust like

RTC.adjust(DateTime(2015,8,9,10,11,12));//year,month,day,hour,minute,second

The DS3231 doesn't have the CH (clock halt) flag which is found on the DS130x. And it is this flag that is tested with the isRunning function. So don't use isRunning with the DS3231!

When i upload this code to the arduino, it seems that any time i use the reset button the time resets to when it was uploaded, it also resets when the arduino is unplugged which is not good for my project. I thought that the code was written so that it would keep the time when unplugged.....

void setup ()
{
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  lcd.begin(16, 2);
  Wire.begin();
  RTC.begin();
  {
   RTC.adjust(DateTime((__DATE__), (__TIME__)));
  }
}

As I said before, DATE and TIME return the compile time and you constantly reset the time to the compile time when it starts up.

What is the point of the brackets around RTC.adjust()? They do nothing.

So if I want to unplug the arduino and still have the time be accurate upon power up. What would has to be in the code. Because the first code allows it to be powered down and back up with the time being kept. However it is just inaccurate time to begin with. Now I have accurate times that don't allow me to move the arduino without fear of unplugging and having to reupload the code

Once set, the rtc will keep good time, so simply comment-out the line that sets the rtc and upload again

How do you successfully comment out the code? Could I just delete it?

// single line comment

/* multi-
line
comment */

Don't delete, you may need to reset the time again sometime.

So I would put a /* in front of the RTC.begin command? Or the __date time command?

Sorry I'm currently In a physics lecture

#if 0
Dead line or lines here
#endif

/* start comments
more comments
end comments */

or
// comments

// more comments

// even more comments

So this?

if /*(! RTC.isrunning()) 
  {
    lcd.println("RTC NOT Running!");
    RTC.adjust(DateTime((__DATE__), (__TIME__)));*/
  }
/*
if (! RTC.isrunning()) 
  {
    lcd.println("RTC NOT Running!");
    RTC.adjust(DateTime((__DATE__), (__TIME__)));
  }
*/