Maker Pro
Maker Pro

Clock for I2C LCD, I2C DS1307

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I would like to know if anyone has a sketch for this setup. I have looked all over and all the places I see are either confusing, or the .ino files have something wrong that the arduino IDE does not like.

I have downloaded a sketch that is supposed to set the RTC. I am assuming it is set.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
The ones that "have something wrong" are probably just missing libraries. Could that be it?
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
No I don't think that's it. It would say something wasn't declared. I would have to find the sketch again to be sure what exactly the error was.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I guess you need to get each working independently and then combine the solutions.

How far have you got? Have you ever used I2C before now?
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I loaded a sketch to display hello world and it worked. I think it's something to do with the i2c RTC. However, I loaded a sketch earlier and serial monitor showed the time. But each time I opened the serial monitor, it would reset to the same time and start counting again.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Can you post those sketches?
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I will post them. I need to find them first. I do know they had a different extension than .ino.
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
Here's one I found. Where it says Serial.begin(9600); it says expected constructor, destructor, or type conversion before'.' token.

Code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#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);

}

Serial.begin(9600);
  // code courtesy of Mr. BroHogan (a.k.a. "Life Clock");
  Wire.beginTransmission(0x68);
  Wire.send(0x07);
  Wire.send(0x90);                       // 0x90=1Hz, 0x91=4kHz, 0x92=8kHz, 0x93=32kHz
  Wire.endTransmission();
 
/*
DS1307 Square-wave machine
Used to demonstrate the four different square-wave outputs from Maxim DS1307
See page nine of data sheet for more information
John Boxall - tronixstuff.wordpress.com
*/
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // each I2C object has a unique bus address, the DS1307 is 0x68
void setup()
{
Wire.begin();
}
void sqw1() // set to 1Hz
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary)
Wire.endTransmission();
}
void sqw2() // set to 4.096 kHz
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x11); // sends 0x11 (hex) 00010001 (binary)
Wire.endTransmission();
}
void sqw3() // set to 8.192 kHz
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x12); // sends 0x12 (hex) 00010010 (binary)
Wire.endTransmission();
}
void sqw4() // set to 32.768 kHz (the crystal frequency)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x13); // sends 0x13 (hex) 00010011 (binary)
Wire.endTransmission();
}
void sqwOff()
// turns the SQW off
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x00); // turns the SQW pin off
Wire.endTransmission();
}
void loop()
{
sqw1();
delay(5000);
sqw2();
delay(5000);
sqw3();
delay(5000);
sqw4();
delay(5000);
sqwOff();
delay(5000);
}
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Have you tried one of the sample routines which use the serial port?

This problem can happen if one off the included header files is missing a semicolon, or something else that causes the compiler to think that its still inside a block.

In this case it might be that RTCLib.h has an error in it.
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
Here's another sketch I have trouble with. It says 'RTC' was not declared in this scope.

Code:
/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC  <<<<<<this is line with error
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");  
}

void loop()
{
   digitalClockDisplay();
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Try compiling this.

Just compile it. No need to set up the hardware
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
My brain is tired now...I'm gonna let it rest for the night.

Thanks for the help.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Compiling that verifies that the serial library is included and compiles.

When you are more awake, try adding just the headers for the other code.

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t

Add these one at a time at the top of the code and see if the problem appears after you've added one of them.

It may be that the header has an error, or that it redefines Serial.
 

donkey

Feb 26, 2011
1,301
Joined
Feb 26, 2011
Messages
1,301
Here's one I found. Where it says Serial.begin(9600); it says expected constructor, destructor, or type conversion before'.' token.

Code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#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);

}

Serial.begin(9600);
  // code courtesy of Mr. BroHogan (a.k.a. "Life Clock");
  Wire.beginTransmission(0x68);
  Wire.send(0x07);
  Wire.send(0x90);                       // 0x90=1Hz, 0x91=4kHz, 0x92=8kHz, 0x93=32kHz
  Wire.endTransmission();

/*
DS1307 Square-wave machine
Used to demonstrate the four different square-wave outputs from Maxim DS1307
See page nine of data sheet for more information
John Boxall - tronixstuff.wordpress.com
*/
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // each I2C object has a unique bus address, the DS1307 is 0x68
void setup()
{
Wire.begin();
}
void sqw1() // set to 1Hz
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary)
Wire.endTransmission();
}
void sqw2() // set to 4.096 kHz
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x11); // sends 0x11 (hex) 00010001 (binary)
Wire.endTransmission();
}
void sqw3() // set to 8.192 kHz
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x12); // sends 0x12 (hex) 00010010 (binary)
Wire.endTransmission();
}
void sqw4() // set to 32.768 kHz (the crystal frequency)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x13); // sends 0x13 (hex) 00010011 (binary)
Wire.endTransmission();
}
void sqwOff()
// turns the SQW off
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x00); // turns the SQW pin off
Wire.endTransmission();
}
void loop()
{
sqw1();
delay(5000);
sqw2();
delay(5000);
sqw3();
delay(5000);
sqw4();
delay(5000);
sqwOff();
delay(5000);
}

ok after bending my head again and googling your fault I found a nice little page that explained something that made sense to an extent, then I had to go looking. ok first rule of arduino is don't talk about fight club... wait
all kidding aside if you type the error into google, google usually has an answer in this case
http://stackoverflow.com/questions/...or-destructor-or-type-conversion-before-token became useful. so something in that code is not in a function.
now the sketch error on mine says
sketch_oct14a:64: error: expected constructor, destructor, or type conversion before '.' token
sketch_oct14a:66: error: expected constructor, destructor, or type conversion before '.' token
sketch_oct14a:67: error: expected constructor, destructor, or type conversion before '.' token
sketch_oct14a:68: error: expected constructor, destructor, or type conversion before '.' token
sketch_oct14a:69: error: expected constructor, destructor, or type conversion before '.' token

sketch_oct14a is the sketch name the next digits are the line of code that is wrong.
in this case
Wire.beginTransmission(0x68);
Wire.send(0x07);
Wire.send(0x90); // 0x90=1Hz, 0x91=4kHz, 0x92=8kHz, 0x93=32kHz
Wire.endTransmission();

ok now as to how to fix it....... yeah I can see whats wrong, I can't see how to fix it.. someone who knows how to code might figure it out
also a few additions is that as of arduino 1.0 wire.write has to change to wire.send
you have 2 "void loop" functions... that can't be good
2 void setup functions, as above
and without the library of RTCLIB.h file you have I won't be able to get the rest
 

donkey

Feb 26, 2011
1,301
Joined
Feb 26, 2011
Messages
1,301
oh an also can you link me to the libraries you have.
jimmy jo likes programming for his hiw rtc his way and jimbo jones likes programming for his.
arduino uses a loose translation of C and as such a simple device can have 20 libraries all named the same thing but slightly different making it hard to find the route of your issues
 

donkey

Feb 26, 2011
1,301
Joined
Feb 26, 2011
Messages
1,301
lol steve, its actually still a little of. arduino themselves state its based on C with C++ elements, so is somewhere in the middle
 
Top