This is Real Time Clock code which I have extracted from Temperature Humidity Controller code.
It is working in Arduino v.0023, but I need to update it to Arduino 1.0. Is it possible?
If possible please can you show how.
#include <DHT.h> // DHT tmperature humidity sensor library
#include <DS1307.h> // Real Time Clock library
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
}
void loop()
{
if (Serial.available() > 0) SetTime(); // If COM-port get data, change system time
int Hour = RTC.get(DS1307_HR,true); // Get current hour
int Minute = RTC.get(DS1307_MIN,false);
int Second = RTC.get(DS1307_SEC,false);
lcd.setCursor(4,0);
lcd.print(addZero(Hour));
lcd.print(":");
lcd.print(addZero(Minute));
lcd.print(":");
lcd.println(addZero(Second)+ " ");
delay(500);
}
void SetTime() // Set time thh:mm
{
if (Serial.read() == 't') // If first symbol 't', set time hh:mm
{
int h = (Serial.read() - '0') * 10 + (Serial.read() - '0');
Serial.read();
int m = (Serial.read() - '0') * 10 + (Serial.read() - '0');
RTC.stop();
RTC.set(DS1307_SEC,0); //set the seconds
RTC.set(DS1307_MIN,m); //set the minutes
RTC.set(DS1307_HR,h); //set the hours
RTC.start();
}
Serial.flush();
}
String addZero(int val)
{
if (val<10) return "0" + String(val);else return String(val);
}
There is one strange thing. Code dos not work even in v0023 if I remove #include <DHT.h> . Why?
On LCD after time display there is black square which I don’t know how to take rid of.
DS1307.h includes WConstants.h. I believe in the 1.0 IDE you need to change #include of WProgram.h and WConstants.h to Arduino.h. The standard suggestion if your code needs to run on both the old and new IDEs is to use:
I am new to Arduino coding.
I just insert MichaelMeissner's code on the top of mine. It did not help.
If it is necessary to make changes inside library I don't know how to do it.
If you know how to attach a file, you should also know how to modify it.
( Well, assumed you attached the file that is really in use )
Use notepad. It's just at the beginning of the file.
Inside DS1703 folder there are 3 files: DS1703.cpp, DS1703.h and DS1703.o.
Using notepad I open DS1703.h file and on the top of it I insert suggested modification code.
Is it correct way?
It did not help.
Error message is:
Error compiling.
In file included from Try_extract_RTC.cpp:2:
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307/DS1307.h:16:24: error: WConstants.h: No such file or directory
Error compiling.
In file included from Try_extract_RTC.cpp:2:
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307/DS1307.h:16:24: error: WConstants.h: No such file or directory
So, start reading this thread again, from the top. What you were supposed to do was CLEARLY spelled out. You didn't do what you were told to do.
Sorry my English. I don't understand what concretely I did not do what supposed to do.
You ask me to show how I modified library code and what was error message.
So I did.
I can not understand what is wrong. This is why I ask forum.
At the very beginning there was one more question:
There is one strange thing. Code dos not work if I remove #include <DHT.h> . Why?
I don't understand what concretely I did not do what supposed to do.
Michael said:
DS1307.h includes WConstants.h. I believe in the 1.0 IDE you need to change #include of WProgram.h and WConstants.h to Arduino.h.
He suggest a way to make the library work for both versions of the IDE.
If you are going to follow his suggestion, you need to add his code AND delete any WProgram.h and WConstants.h include statements that are already there.
extern "C" {
#include <../Wire/Wire.h>
}
#include "DS1307.h"
DS1307::DS1307()
{
Wire.begin();
}
DS1307 RTC=DS1307();
// PRIVATE FUNCTIONS
// Aquire data from the RTC chip in BCD format
// refresh the buffer
void DS1307::read(void)
{
// use the Wire lib to connect to tho rtc
// reset the resgiter pointer to zero
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.write(0x00);
Wire.endTransmission();
// request the 7 bytes of data (secs, min, hr, dow, date. mth, yr)
Wire.requestFrom(DS1307_CTRL_ID, 7);
for(int i=0; i<7; i++)
{
// store data in raw bcd format
rtc_bcd[i]=Wire.receive();
}
}
// update the data on the IC from the bcd formatted data in the buffer
void DS1307::save(void)
{
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.write(0x00); // reset register pointer
for(int i=0; i<7; i++)
{
Wire.write(rtc_bcd[i]);
}
Wire.endTransmission();
}
// PUBLIC FUNCTIONS
void DS1307::get(int *rtc, boolean refresh) // Aquire data from buffer and convert to int, refresh buffer if required
{
if(refresh) read();
for(int i=0;i<7;i++) // cycle through each component, create array of data
{
rtc[i]=get(i, 0);
}
}
int DS1307::get(int c, boolean refresh) // aquire individual RTC item from buffer, return as int, refresh buffer if required
{
if(refresh) read();
int v=-1;
switch(c)
{
case DS1307_SEC:
v=(10*((rtc_bcd[DS1307_SEC] & DS1307_HI_SEC)>>4))+(rtc_bcd[DS1307_SEC] & DS1307_LO_BCD);
break;
case DS1307_MIN:
v=(10*((rtc_bcd[DS1307_MIN] & DS1307_HI_MIN)>>4))+(rtc_bcd[DS1307_MIN] & DS1307_LO_BCD);
break;
case DS1307_HR:
v=(10*((rtc_bcd[DS1307_HR] & DS1307_HI_HR)>>4))+(rtc_bcd[DS1307_HR] & DS1307_LO_BCD);
break;
case DS1307_DOW:
v=rtc_bcd[DS1307_DOW] & DS1307_LO_DOW;
break;
case DS1307_DATE:
v=rtc_bcd[DS1307_DATE]/16 * 10 + rtc_bcd[DS1307_DATE] % 16;
break;
case DS1307_MTH:
v=(10*((rtc_bcd[DS1307_MTH] & DS1307_HI_MTH)>>4))+(rtc_bcd[DS1307_MTH] & DS1307_LO_BCD);
break;
case DS1307_YR:
v=2000 + rtc_bcd[DS1307_YR]/16 * 10 + rtc_bcd[DS1307_YR] % 16;
break;
} // end switch
return v;
}
void DS1307::set(int c, int v) // Update buffer, then update the chip
{
switch(c)
{
case DS1307_SEC:
if(v<60 && v>-1)
{
//preserve existing clock state (running/stopped)
int state=rtc_bcd[DS1307_SEC] & DS1307_CLOCKHALT;
rtc_bcd[DS1307_SEC]=state | ((v / 10)<<4) + (v % 10);
}
break;
case DS1307_MIN:
if(v<60 && v>-1)
{
rtc_bcd[DS1307_MIN]=((v / 10)<<4) + (v % 10);
}
break;
case DS1307_HR:
// TODO : AM/PM 12HR/24HR
if(v<24 && v>-1)
{
rtc_bcd[DS1307_HR]=((v / 10)<<4) + (v % 10);
}
break;
case DS1307_DOW:
if(v<8 && v>-1)
{
rtc_bcd[DS1307_DOW]=v;
}
break;
case DS1307_DATE:
if(v<32 && v>-1)
{
rtc_bcd[DS1307_DATE]=((v / 10)<<4) + (v % 10);
}
break;
case DS1307_MTH:
if(v<13 && v>-1)
{
rtc_bcd[DS1307_MTH]=((v / 10)<<4) + (v % 10);
}
break;
case DS1307_YR:
if(v<50 && v>-1)
{
rtc_bcd[DS1307_YR]=((v / 10)<<4) + (v % 10);
}
break;
} // end switch
save();
}
void DS1307::stop(void)
{
// set the ClockHalt bit high to stop the rtc
// this bit is part of the seconds byte
rtc_bcd[DS1307_SEC]=rtc_bcd[DS1307_SEC] | DS1307_CLOCKHALT;
save();
}
void DS1307::start(void)
{
// unset the ClockHalt bit to start the rtc
// TODO : preserve existing seconds
rtc_bcd[DS1307_SEC]=0;
save();
}
In file included from C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/Print.h:26,
from C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/Stream.h:26,
from C:\Users\user\Desktop\arduino-1.0\libraries\DHT/../Wire/Wire.h:24,
from C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp:3:
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:116: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, const char*)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:115: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, const String&)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:117: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, char)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:116: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, const char*)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:118: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, unsigned char)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:117: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, char)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:119: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, int)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:118: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, unsigned char)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:120: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, unsigned int)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:119: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, int)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:121: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, long int)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:120: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, unsigned int)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:122: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, long unsigned int)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:121: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, long int)' here
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp: In member function 'void DS1307::read()':
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp:23: error: call of overloaded 'write(int)' is ambiguous
C:\Users\user\Desktop\arduino-1.0\libraries\DHT/../Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/Print.h:49: note: size_t Print::write(const char*)
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp:31: error: 'class TwoWire' has no member named 'receive'
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp: In member function 'void DS1307::save()':
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp:39: error: call of overloaded 'write(int)' is ambiguous
C:\Users\user\Desktop\arduino-1.0\libraries\DHT/../Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/Print.h:49: note: size_t Print::write(const char*)
Error message:
In file included from C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/Print.h:26,
from C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/Stream.h:26,
from C:\Users\user\Desktop\arduino-1.0\libraries\DHT/../Wire/Wire.h:24,
from C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp:3:
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:116: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, const char*)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:115: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, const String&)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:117: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, char)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:116: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, const char*)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:118: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, unsigned char)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:117: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, char)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:119: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, int)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:118: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, unsigned char)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:120: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, unsigned int)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:119: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, int)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:121: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, long int)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:120: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, unsigned int)' here
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:122: error: declaration of C function 'StringSumHelper& operator+(const StringSumHelper&, long unsigned int)' conflicts with
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/WString.h:121: error: previous declaration 'StringSumHelper& operator+(const StringSumHelper&, long int)' here
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp: In member function 'void DS1307::read()':
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp:23: error: call of overloaded 'write(int)' is ambiguous
C:\Users\user\Desktop\arduino-1.0\libraries\DHT/../Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/Print.h:49: note: size_t Print::write(const char*)
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp:31: error: 'class TwoWire' has no member named 'receive'
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp: In member function 'void DS1307::save()':
C:\Users\user\Desktop\arduino-1.0\libraries\DS1307\DS1307.cpp:39: error: call of overloaded 'write(int)' is ambiguous
C:\Users\user\Desktop\arduino-1.0\libraries\DHT/../Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Users\user\Desktop\arduino-1.0\hardware\arduino\cores\arduino/Print.h:49: note: size_t Print::write(const char*)