Still stuck with CLASS, "Member functions" and that sort of thing.

I am having a lot of trouble getting my head around the "accepted way" of doing things with variable names and all that sort of thing too.

I have a post about my RTC on the I2C bus.

As best I can, I am starting from scratch and building a simple "clock" which sets and reads the time over the serial port.

As I have a working clock, I tried to take parts of that and use them pieces to get a working sketch.

DIDN'T HAPPEN.

Putting that all aside, I sat down and read more of my "C++ for dummies" book. Yeah, maybe not THE book to read, but it should help.

Before going into what the book says, let's look at some of the things which are tripping me up.

Here are a few problems I have:
(Exerts from a WORKING sketch)

#include "DS1307_1.h" 
#include "alarm_clock.h"

int rtc[7];
DS1307 RTC=DS1307();             // Create RTC object

Ok, so this is two "includes" of the DS1307_1.h and the alarm_clock.h

Why the _1 for the DS1307? There is no DS1307_1.cpp file.
But there is a DS1307.cpp file.

the next part sets up the variable rtc to have 7 "parts".
No problems.

Then DS1307 RTC=DS1307();

CONFUSING!

C++ for dummies chapter 12.
Page 166:
To create an actual savings account object, I type something like the following:
SavingsAccount mySavingsAccount;
We say that mySavingsAccount is an instance of the class SavingsAccount.

Then it goes on about the naming convention and ends up by saying the compiler really doesn't care about case (etc) of the names.

Now, ok, putting THAT aside, the name is shown as RTC - note the capitols.

Else where in the code there is this:

    int rtc[7];
    RTC.get(rtc,true);

rtc[7] and RTC.get(

Ok, I am setting up rtc[7]. But RTC.get(rtc.true)??

I'm guessing that RTC.get( ) is in......
Well I found it in alarm_clock.ino.

I'm taking it that RTC.get(rtc.true) the RTC is what was declared with the DS1307 RTC=DS1307(); and get(rtc.true) ....

Ok, sort of understood.

Then there are these:

byte alarm_clock::run()

That is when you are using another member function in another class - or something.

I am trying, but it isn't well explained for me.

What the book says SORT OF makes sense. But I can't compare it to what I am seeing with the Arduino and the codes because of small inconsistencies like the DS1307 RTC=DS1307(); example.

It doesn't explain what is going on there.

Please, someone.

Then it goes on about the naming convention and ends up by saying the compiler really doesn't care about case (etc) of the names.

Names aren't interchangeable, and case does matter, 'rtc' and 'RTC' are distinct identifiers and is like comparing 'abc' with 'DEF'.

With regards to the initialization:

DS1307 RTC=DS1307();

A class with a default or no constructor should be constructed like:

DS1307 RTC;

There is no real reason to use like you have it there, unless there is only a copy constructor, which is a silly design. ( if DS1307(); can successfully default initialize a temporary DS1307 object, then your declaration can most certainly do it to. )

It looks like this was written by someone not too familiar with C++ and they tried to initialize RTC like this:

DS1307 RTC(); //WRONG

But it wouldn't work so they whacked on the copy initialization.

int rtc[7];
RTC.get(rtc,true);

Like I mentioned above, these are different variables. Also, notice the comma, its not a period accessing a member of rtc, its a comma separating two inputs to a function.

byte alarm_clock::run()

When the function definitions aren't inline with the class definition, the compiler needs to know that run() belongs to alarm_clock, otherwise it would treat it like a global function.

Ok, so this is two "includes" of the DS1307_1.h and the alarm_clock.h

Yes.

Why the _1 for the DS1307? There is no DS1307_1.cpp file.

You'd have to ask the author why he/she chose that approach, over #include "aNewAndImprovedDS1307.h".

There is no DS1307_1.cpp file.
But there is a DS1307.cpp file.

Then, the implementation is probably in the header file, too, which tells me that the author is an idiot. Or just lazy and doesn't care about others. People that do separate the declaration of a class from the implementation of the class.

the next part sets up the variable rtc to have 7 "parts".

It defines rtc as an array with 7 elements. Just to be technically accurate. You should be, as it makes it easier to understand what you are talking about.

Then DS1307 RTC=DS1307();

CONFUSING!

Not to mention just plain wrong. One never invokes the constructor explicitly, as that code is doing.

DS1307 RTC;

is correct. It declares an instance of the class, resulting in an implicit call to the constructor.

Then it goes on about the naming convention and ends up by saying the compiler really doesn't care about case (etc) of the names.

That is nonsense. Case most definitely DOES matter.

Else where in the code there is this:

Clearly, the get() method expects an array that it can write to as input. That would be the rtc array. The get() method is a class method, so it needs to be called for an instance of the class. The class is DS1307, and the instance is RTC. So, that bit of code makes sense (to me).

Then there are these:

Where?

That is when you are using another member function in another class - or something.

Maybe. Without the ; on the end, that looks like a function declaration. The :: is the scope resolution operator, and alarm_clock is the scope (class) where the function (run()) is defined.

There are a few things in your post that make sense and a few others that don't make sense.
First. Here do you get your library? Can you give a complete (and working) example of your library?

I have a library that makes the same that yours and one of the examples is:

#include <DS1307.h>

// Init the DS1307
DS1307 rtc(4, 5);

void setup()
{
  // Set the clock to run-mode
  rtc.halt(false);
  
  // Setup Serial connection
  Serial.begin(9600);

  // The following lines can be commented out to use the values already stored in the DS1307
  rtc.setDOW(SUNDAY);        // Set Day-of-Week to SUNDAY
  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(3, 10, 2010);   // Set the date to October 3th, 2010
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  delay (1000);
}

Note:
1- The name of the library: DS1307.h
2- The constructor of the object: DS1307 rtc(4, 5);
3- How you get the time with this library: rtc.getTimeStr()

So, one of the conclusions that I get is: Find another library!

Ok, I am setting up rtc[7]. But RTC.get(rtc.true)??

I'm guessing that RTC.get( ) is in......
Well I found it in alarm_clock.ino.

ATTENTION: What you have write in the original line is:

    int rtc[7];

RTC.get(rtc,true);

"comma", not "point". There are different things.

PS.: Now there are more replies, but I will keep mine too.

Thanks to all.

Ok, comma not full stop.

I cut and pasted and the font/screen/eyes didn't quite notice the difference.

I shall have to "move on" from that library, but "luckily" it is a local one for that sketch.

It is:
include "......"
not <......>

In my other post about the RTC and I2C bus I am trying to get a "tiny RTC" module working.
Reading more it would seem I have bought a lemon.

I can set the time and read it, but there seems to be a problem with SAVING the time.
The last sketch won't compile and I am getting errors which I don't understand, so I am reading the C++ book to try and learn.

Other ("newer") DS1307 libraries give HEAPS of errors and I think they are written for earlier versions of the IDE.
What ever: It is beyond me.

I do need to get a PROPER and WORKING library though. I agree.

I am trying to get 3 projects working and I have had 1 week off work (what we call holidays) and this week.
I was hoping to get stuff done, but these problems have stopped me unexpectedly and it is slightly annoying.

Anyway, I shall get back to reading and trying to get my head around it all.

Just the terms/names are SO foreign to me it is hard to understand what is being referenced in their examples.

Also the book was going great guns with classes then they changed the whole example halfway through and that threw me.

Oh the fun of learning.

Thanks again.

I install more than one library to test they and see what is the best to me.
I keep the more simple an the one that get the work done quickly (without fancy things). So, maybe you are interested, the library that I use right now is this:

Good work. And good holidays.

Thanks.

I am 99% sure I tried that and got flooded with errors.

But it won't hurt to try again.

lost_and_confused:
Thanks.

I am 99% sure I tried that and got flooded with errors.

But it won't hurt to try again.

In the examples? If you want to use the DS1307 you need to use this example:

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = RTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
}

Try to do the same exercise that you did with the last code. I think it's much more easy.

You can have issues too installing the library.

Thanks.

It kind of "erks" me when the code does this:

#include "RTClib.h"

and the sketch is in its own directory.

So that means I have to copy the RTClib.h and .cpp files into that directory first.

Obviously I am being "silly" but it is annoying.

Copied the files and now I get this:

RTClib.cpp: In member function 'uint8_t RTC_DS1307::isrunning()':
RTClib.cpp:229: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)
RTClib.cpp: In static member function 'static void RTC_DS1307::adjust(const DateTime&)':
RTClib.cpp:239: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)
RTClib.cpp:247: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)
RTClib.cpp: In static member function 'static DateTime RTC_DS1307::now()':
RTClib.cpp:253: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)

I feel this is more a step backwards than forwards.

Help please.

I have my library normally installed (copy of the whole folder that I download from github to my libraries folder) and it compiles normally. This sketch compiles too:

#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 RTC;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = RTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
}

maybe that answers your question.

Maybe the name of the folder in the library would help.

I am sure it won't be:
RTClib-master

RTClib?

And the "....." would have to be changed to <.......> right?

That aside, the name errors as given. So I had to rename it to RTClib.

Went to example and loaded said sketkch.

Not chaning the "..." to <....> and checked.

Same errors as posted before.

tried changing the "....." to <.....> and got the same errors that way too.

My folder is named RTClib.
Yes, I change "..." by <...>.

But I still get those errors.

So I am not "moving" on the learning curve.

:frowning:

RTClib.cpp: In member function 'uint8_t RTC_DS1307::isrunning()':
RTClib.cpp:229: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)
RTClib.cpp: In static member function 'static void RTC_DS1307::adjust(const DateTime&)':
RTClib.cpp:239: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)
RTClib.cpp:247: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)
RTClib.cpp: In static member function 'static DateTime RTC_DS1307::now()':
RTClib.cpp:253: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)

Now I see that the problem is not the library it self.
The error message that you get is:

RTClib.cpp:229: error: call of overloaded 'write(int)' is ambiguous
E:\Shepherd's documents\Arduino\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
C:\Program Files\Arduino\Arduino ERW 1.0.4\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)

So, the problem is that your IDE don't like the write() function.
I see that you have a very old IDE too (Arduino ERW 1.0.4).

I don't like too the place that you have the Wire library. Can you:
1- close your IDE;
2- move the Wire folder from "E:\Shepherd's documents\Arduino\libraries\Wire" to your desktop;
3- open your IDE;
4- go to File->Examples->RTClib->ds1307
5- complile

And see if the errors still there. I don't know if in that old version of the IDE it's suppose the Wire library be there but in mine I don't have it. BTW, I have the 1.5.6-r2 (maybe the last release that I fond in the site).

(maybe I should update my IDE)

Compiles ok, but I am getting junk out on the serial port.

&4[í"ð?ÂçÂe'#¡äR áóZÅ(
Æ I ù

Kind of thing.

That is because your baudrate don't matches in your sketch and your serial monitor. In your right down corner you have a box with numbers, that number must be "57600" because is the baudrate that you have in the example:

    Serial.begin(57600);

Woo Hoo!

Thanks.

(Dropped the speed back to 9600 though)

So indulge me:
What's going on with this wire library?

And can I clean up the code to get rid of these #ifdef lines?

and dumber question:
Where do I get a newer IDE?
(Skip that)

Instead, where does the program keep the "bare minimum" sketch?
I want to make mine a bit more "fancy" to help me. But all the ones I have found and edited aren't reflected when I load the IDE with no sketch.

What you want is the 1.0.5-r2 for Windows. For what I see is the last. I think I need to upgrade too.

The answer to the other question, I don't know. Maybe it's easy but I don't see the answer even in google.

Yeah, thanks,

Found it "ITMT".

Anyway, now I can get back to working on my projects.

Yeah!!!!!!

:slight_smile: