Changed to
#include <Wire.h>
and things got worse
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:19: error: ISO C++ forbids declaration of 'start' with no type
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:19: error: prototype for 'int RTCReader::start()' does not match any in class 'RTCReader'
C:\Users\Username\Documents\Arduino\libraries\RTCReader/RTCReader.h:11: error: candidate is: void RTCReader::start()
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:24: error: ISO C++ forbids declaration of 'end' with no type
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:24: error: prototype for 'int RTCReader::end()' does not match any in class 'RTCReader'
C:\Users\Username\Documents\Arduino\libraries\RTCReader/RTCReader.h:12: error: candidate is: void RTCReader::end()
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:33: error: ISO C++ forbids declaration of 'setDateTime' with no type
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:33: error: prototype for 'int RTCReader::setDateTime(short int, short int, short int, short int, short int, short int, short int)' does not match any in class 'RTCReader'
C:\Users\Username\Documents\Arduino\libraries\RTCReader/RTCReader.h:13: error: candidate is: void RTCReader::setDateTime(short int, short int, short int, short int, short int, short int, short int)
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:49: error: ISO C++ forbids declaration of 'getDateTime' with no type
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:49: error: prototype for 'int RTCReader::getDateTime(short int*, short int*, short int*, short int*, short int*, short int*, short int*)' does not match any in class 'RTCReader'
C:\Users\Username\Documents\Arduino\libraries\RTCReader/RTCReader.h:14: error: candidate is: void RTCReader::getDateTime(short int*, short int*, short int*, short int*, short int*, short int*, short int*)
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:68: error: ISO C++ forbids declaration of 'decToBcd' with no type
C:\Users\Username\Documents\Arduino\libraries\RTCReader\RTCReader.cpp:68: error: prototype for 'int RTCReader::decToBcd(byte)' does not match any in class 'RTCReader'
C:\Users\Username\Documents\Arduino\libraries\RTCReader/RTCReader.h:17: error: candidate is: byte RTCReader::decToBcd(byte)
so I think it would be useful to provide my code, just please help me get out of this
Header file
#ifndef RTCReader_h
#define RTCReader_h
#include "Arduino.h"
#include <Wire.h>
class RTCReader
{
public:
RTCReader(int rtc_i2c_address);
void start();
void end();
void setDateTime(short weekDay, short monthDay, short month, short year, short hours, short mins, short secs);
void getDateTime(short* weekDay, short* monthDay, short* month, short* year, short* hours, short* mins, short* secs);
private:
byte decToBcd(byte val);
byte bcdToDec(byte val);
int RTC_I2C_ADDRESS;
int regPtrReset;
};
#endif
cpp file
#include "Arduino.h"
#include "RTCReader.h"
// Arduino version compatibility Pre-Compiler Directives
#if defined(ARDUINO) && ARDUINO >= 100 // Arduino v1.0 and newer
#define I2C_WRITE Wire.write
#define I2C_READ Wire.read
#else // Arduino Prior to v1.0
#define I2C_WRITE Wire.send
#define I2C_READ Wire.receive
#endif
RTCReader::RTCReader(int rtc_i2c_address)
{
RTC_I2C_ADDRESS = rtc_i2c_address;
regPtrReset = 0x00;
}
RTCReader::start()
{
Wire.begin();
}
RTCReader::end()
{
Wire.end();
}
// 1) Sets the date and time on the RTC
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.
RTCReader::setDateTime(short weekDay, short monthDay, short month, short year, short hours, short mins, short secs)
{
Wire.beginTransmission(RTC_I2C_ADDRESS);
I2C_WRITE(regPointReset);
I2C_WRITE(decToBcd(secs) & 0x7f); // 0 to bit 7 starts the clock
I2C_WRITE(decToBcd(mins));
I2C_WRITE(decToBcd(hours)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateRTC)
I2C_WRITE(decToBcd(weekDay));
I2C_WRITE(decToBcd(monthDay));
I2C_WRITE(decToBcd(month));
I2C_WRITE(decToBcd(year));
Wire.endTransmission();
}
// Gets the date and time from the RTC and prints result
RTCReader::getDateTime(short* weekDay, short* monthDay, short* month, short* year, short* hours, short* mins, short* secs)
{
Wire.beginTransmission(RTC_I2C_ADDRESS);
I2C_WRITE(regPointReset);
Wire.endTransmission();
Wire.requestFrom(RTC_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*secs = bcdToDec(I2C_READ() & 0x7f);
*mins = bcdToDec(I2C_READ());
*hours = bcdToDec(I2C_READ() & 0x3f); // Need to change this if 12 hour am/pm
*weekDay = bcdToDec(I2C_READ());
*monthDay = bcdToDec(I2C_READ());
*month = bcdToDec(I2C_READ());
*year = bcdToDec(I2C_READ());
}
// Convert normal decimal numbers to binary coded decimal
RTCReader::decToBcd(byte val)
{
return ((val / 10 * 16) + (val % 10));
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ((val / 16 * 10) + (val % 16));
}
the sketch
#include <RTCReader.h>
#define RTC_I2C_ADDRESS 0x68 // This is the I2C address
RTCReader rtcReader(RTC_I2C_ADDRESS);
short weekDay, monthDay, month, year, hours, mins, secs;
char *Day[] = {"","Lun","Mar","Mer","Gio","Ven","Sab","Dom"};
char *Mon[] = {"","Gen","Feb","Mar","Apr","Mag","Giu","Lug","Ago","Set","Ott","Nov","Dic"};
void setup()
{
rtcReader.start();
rtcReader.getDateTime(&weekDay, &monthDay, &month, &year, &hours, &mins, &secs);
if (year == 0)
{
rtcReader.setDateTime(3, 14, 8, 2013, 13, 15, 0);
}
}
void loop()
{
rtcReader.getDateTime(&weekDay, &monthDay, &month, &year, &hours, &mins, &secs);
if (hours < 10)
Serial.print("0");
Serial.print(hours, DEC);
Serial.print(":");
if (mins < 10)
Serial.print("0");
Serial.print(mins, DEC);
Serial.print(":");
if (secs < 10)
Serial.print("0");
Serial.print(secs, DEC);
Serial.print(" ");
Serial.print(Day[weekDay]);
Serial.print(", ");
Serial.print(monthDay, DEC);
Serial.print(" ");
Serial.print(Mon[month]);
Serial.print(" 20");
if (year < 10)
Serial.print("0");
Serial.println(year, DEC);
}