Pages: [1] 2   Go Down
Author Topic: Real Time Clock code update to Arduino 1.0  (Read 1772 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

   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.

Code:
#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.

DHT.h library is taken from  https://github.com/adafruit/DHT-sensor-library
DS1307.h library is attached.


* DS1307.h (1.32 KB - downloaded 14 times.)
* DS1307.o (17.17 KB - downloaded 9 times.)
* DS1307.cpp (3.63 KB - downloaded 11 times.)
« Last Edit: August 09, 2012, 09:06:11 am by Alex1503 » Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 50
Posts: 1766
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"

#else
#include "WProgram.h"
#include "WConstants.h"
#endif
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The standard suggestion dos not work.
And I don't know how to make any changes inside library.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The standard suggestion dos not work.
And I don't know how to make any changes inside library.

If you don't know how to change the libraries, then how do you know it doesn't work?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2741
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I insert suggested modification code.
Is it correct way?
It did not help.
Then, it was not the correct change. Post the modified code and the EXACT error message.

You'll probably need to note that Michael suggest that you REPLACE some code, not just INSERT some code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Modified DS1703.h file
Code:
/*
  DS1307.h - library for DS1307 rtc
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"

#else
#include "WProgram.h"
#include "WConstants.h"
#endif
// ensure this library description is only included once
#ifndef DS1307_h
#define DS1307_h

// include types & constants of Wiring core API
#include <WConstants.h>

// include types & constants of Wire ic2 lib
#include <../Wire/Wire.h>

#define DS1307_SEC 0
#define DS1307_MIN 1
#define DS1307_HR 2
#define DS1307_DOW 3
#define DS1307_DATE 4
#define DS1307_MTH 5
#define DS1307_YR 6

#define DS1307_BASE_YR 2000

#define DS1307_CTRL_ID B1101000  //DS1307

 // Define register bit masks
#define DS1307_CLOCKHALT B10000000

#define DS1307_LO_BCD  B00001111
#define DS1307_HI_BCD  B11110000

#define DS1307_HI_SEC  B01110000
#define DS1307_HI_MIN  B01110000
#define DS1307_HI_HR   B00110000
#define DS1307_LO_DOW  B00000111
#define DS1307_HI_DATE B00110000
#define DS1307_HI_MTH  B00110000
#define DS1307_HI_YR   B11110000

// library interface description
class DS1307
{
  // user-accessible "public" interface
  public:
    DS1307();
    void get(int *, boolean);
    int get(int, boolean);
void set(int, int);
    void start(void);
    void stop(void);

  // library-accessible "private" interface
  private:
    byte rtc_bcd[7]; // used prior to read/set ds1307 registers;
void read(void);
void save(void);
};

extern DS1307 RTC;

#endif
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2214
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You will probably also need to change any wire.send/wire.receive to wire.write/wire.read. A simpler option would be to use a library already written/converted for Arduino 1.0+ I would recommend this one https://github.com/davidhbrown/RealTimeClockDS1307 as I have used it in a few projects without problems.
Logged

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: August 09, 2012, 06:42:42 am by Alex1503 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't understand what concretely I did not do what supposed to do.

Michael said:
Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is how I change DS1307.h file:
Code:
/*
  DS1307.h - library for DS1307 rtc
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"

#else
#include "WProgram.h"
#include "WConstants.h"
#endif

// ensure this library description is only included once
#ifndef DS1307_h
#define DS1307_h



// include types & constants of Wire ic2 lib
#include <../Wire/Wire.h>

#define DS1307_SEC 0
#define DS1307_MIN 1
#define DS1307_HR 2
#define DS1307_DOW 3
#define DS1307_DATE 4
#define DS1307_MTH 5
#define DS1307_YR 6

#define DS1307_BASE_YR 2000

#define DS1307_CTRL_ID B1101000  //DS1307

 // Define register bit masks
#define DS1307_CLOCKHALT B10000000

#define DS1307_LO_BCD  B00001111
#define DS1307_HI_BCD  B11110000

#define DS1307_HI_SEC  B01110000
#define DS1307_HI_MIN  B01110000
#define DS1307_HI_HR   B00110000
#define DS1307_LO_DOW  B00000111
#define DS1307_HI_DATE B00110000
#define DS1307_HI_MTH  B00110000
#define DS1307_HI_YR   B11110000

// library interface description
class DS1307
{
  // user-accessible "public" interface
  public:
    DS1307();
    void get(int *, boolean);
    int get(int, boolean);
void set(int, int);
    void start(void);
    void stop(void);

  // library-accessible "private" interface
  private:
    byte rtc_bcd[7]; // used prior to read/set ds1307 registers;
void read(void);
void save(void);
};

extern DS1307 RTC;

#endif

This is how I change DS1307.cpp file:
Code:
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();
}


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is error message:
I

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*)
« Last Edit: August 09, 2012, 08:16:01 am by Alex1503 » Logged

Pages: [1] 2   Go Up
Jump to: