DS1307 Initialization sketch returns Error Compiling for Board Mega 2560

Working my way through Arduino Projects for Amateur Radio by Jack Purdum. Preceding projects worked with the 2560 board I am using. Loaded the provided sketch for initializing the RTC DS1307. Obtained the recommended GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library. Dropped the -Master from the name as required. On testing compile I get Error compiling for board Arduino/Genuino Mega or Mega 2560.
Not sure where to go from here. Tried checking the code to see if it matched the book. The RTClib is in the Arduino libraries on my computer and Manage Library shows it to be Installed.

All suggestions are welcome.
Thank you.

DS initialization sketch.pdf (31.3 KB)

RTC initialization error code.pdf (40.9 KB)

Chicov:
Not sure where to go from here.

Use the "I2C scanner" sketch and in a few seconds you will know whether

  • the RTC can be found on the I2C bus
  • the RTC is responding on the I2C bus

If the I2C scanner" sketch cannot find your RTC, something with the wiring or the hardware is wrong and you never need to install and use silly amounts of third-party libraries to check defective wiring or defective hardware.

To succussfully use this forum, you most learn to post your code using the code tags, which are found at the icon </>. Please read the sticky post at the top of the board on how to use the forum. Your code should look like this. I have removed a stray comment line so that the setup() and loop() were not commented out as in the pdf posting.

#include <Time.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <DS1307RTC.h>
/*****
This program initializes the RTC module to the time and date on the host PC.
If the user wishes to use some other date and time, they can hard code the
values in the SetStartingClockValues() function.
Feb. 5, 2014
Dr. Purdum, W8TEE
*/
#include <Wire.h> //I2C header file
#include <LiquidCrystal.h>
#include <RTClib.h>
#define RTCI2CADDRESS 0x68 // 7 bit device address (without last bit – see
// at the datasheet)
#define SECONDS 0 // Offsets into the DS1307 internal registers
#define MINUTES 1
#define HOURS 2
#define DAY 3 // This is a day-of-the-week index register
#define DATE 6
#define MONTH 5
#define YEAR 16
#define MILLENIA 2000
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // For LCD display from Chapter 3
RTC_Millis rtc; // An RTC object
DateTime currentDateTime; // A date and time object
byte result;
byte timeBuffer[8];
byte flagBit = 0;

void setup()
{
Wire.begin(); // Initiate Wire library and join the I2C bus as a master
lcd.begin(16, 2); // set up the LCD's number of columns and rows
Serial.begin(9600); // Initiate serial communication
rtc.begin(DateTime(__DATE__, __TIME__));
currentDateTime = rtc.now();
SetStartingClockValues();
Serial.println("Clock initialized");
}
void loop()
{
// Nothing to do here...
}
/*****
This function reads the system clock of the host PC and translates those time
and date values for use by the RTC. This involves reading the date and time
values from the PC via the now() method from the RTClib library and copying
those values into the timeBuffer[] array. Because the DS1307 wants its data
in BCD format, the timeBuffer[] data are converted to BCD format. The RTClib
library is available from: https://github.com/adafruit/RTClib.
Parameter list:
void
Return value:
void
CAUTION: This function assumes the DateTime variable named currentDateTime
exists prior to the function call.
*****/
void SetStartingClockValues()
{
timeBuffer[MONTH] = currentDateTime.month(); // Month (1-12)
timeBuffer[DATE] = currentDateTime.day(); // Day (1-31)
timeBuffer[YEAR] = currentDateTime.year() - MILLENIA; // Year (0-99)
timeBuffer[DAY] = currentDateTime.dayOfWeek(); // Day of week (1-7)
timeBuffer[HOURS] = currentDateTime.hour(); // Hour (0-23)
timeBuffer[MINUTES] = currentDateTime.minute(); // Minutes (0-59)
timeBuffer[SECONDS] = currentDateTime.second(); // Seconds (0-59)
Wire.beginTransmission(RTCI2CADDRESS); // Select RTC on the I2C bus
Wire.write(0); // Set internal register pointer
Wire.write(DecToBcd(timeBuffer[SECONDS])); // Second
Wire.write(DecToBcd(timeBuffer[MINUTES])); // Minute
Wire.write(DecToBcd(timeBuffer[HOURS])); // Hour
Wire.write(DecToBcd(timeBuffer[DAY])); // Weekday
Wire.write(DecToBcd(timeBuffer[DATE])); // Day
Wire.write(DecToBcd(timeBuffer[MONTH])); // Month (with century bit = 0)
Wire.write(DecToBcd(timeBuffer[YEAR])); // Year
Wire.write(0);
result = Wire.endTransmission();
if (result)
Serial.print("RTC Write error occurred");
}
/*****
This function converts the byte-length decimal value to a Binary Coded Decimal
value.
Parameter list:
byte value A number expressed as a decimal value
Return value:
byte The decimal value expressed as a BCD number
*****/
byte DecToBcd(byte value)
{
return (value / 10 * 16 + value % 10);
}

There is an error due to the syntax confusion caused by the multiple libraries.

timeBuffer[DAY] = currentDateTime.dayOfWeek(); // Day of week (1-7)

needs to be

timeBuffer[DAY] = currentDateTime.dayOfTheWeek(); // Day of week (1-7)

to use the syntax of the RTCLib. With that change the code compiles.

There is another problem I see

#define SECONDS 0 // Offsets into the DS1307 internal registers
#define MINUTES 1
#define HOURS 2
#define DAY 3 // This is a day-of-the-week index register
#define DATE 6
#define MONTH 5
#define YEAR 16
#define MILLENIA 2000

You declare

byte timeBuffer[8];

There is no timeBuffer[16] or timeBuffer[2000] when used here

void SetStartingClockValues()
{
timeBuffer[MONTH] = currentDateTime.month(); // Month (1-12)
timeBuffer[DATE] = currentDateTime.day(); // Day (1-31)
timeBuffer[YEAR] = currentDateTime.year() - MILLENIA; // Year (0-99)
timeBuffer[DAY] = currentDateTime.dayOfTheWeek(); // Day of week (1-7)
timeBuffer[HOURS] = currentDateTime.hour(); // Hour (0-23)
timeBuffer[MINUTES] = currentDateTime.minute(); // Minutes (0-59)
timeBuffer[SECONDS] = currentDateTime.second(); // Seconds (0-59)
Wire.beginTransmission(RTCI2CADDRESS); // Select RTC on the I2C bus
Wire.write(0); // Set internal register pointer
Wire.write(DecToBcd(timeBuffer[SECONDS])); // Second
Wire.write(DecToBcd(timeBuffer[MINUTES])); // Minute
Wire.write(DecToBcd(timeBuffer[HOURS])); // Hour
Wire.write(DecToBcd(timeBuffer[DAY])); // Weekday
Wire.write(DecToBcd(timeBuffer[DATE])); // Day
Wire.write(DecToBcd(timeBuffer[MONTH])); // Month (with century bit = 0)
Wire.write(DecToBcd(timeBuffer[YEAR])); // Year
Wire.write(0);
result = Wire.endTransmission();
if (result)
Serial.print("RTC Write error occurred");
}