I have bought the following clock for my arduinoproject.
but when i try it on my arduino mega i only get 2065535-255-255 255:255:255 as printout.
I get the message "clock is set", so it seems that the setClock() function runns atleast.
when I try the exact same code with the same pinns on a orginal arduino board the clock works nicely!
It feels like the Analog5 and Analog4 are for some reason not working as they should on my MEGA board? could it be so that other pins should be used for the clock on the MEGA board?.
I have alot of stuff connected to my Mega board, but i dont use a single one of the A0 to A15 exept those for the clock.
any ideas?
/*
Clock functions.
scl=analog5
sda=analog4
ground and 5v
*/
#include <Wire.h>
void setup () {
Serial.begin(9600);
Wire.begin();
setClock();
}
void loop () {
Serial.print("now=");
String timeNow=reciveClock();
Serial.println(timeNow);
delay(3000);
}
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
void setClock()
{
Wire.beginTransmission(0x68);//DS1307 write the initial time
Wire.write(0);
Wire.requestFrom(0x68, 7);
//this is the exact order the itmes will be fetched in too.
Wire.write(bin2bcd(30));// seconds
Wire.write(bin2bcd(20));//minutes
Wire.write(bin2bcd(10));//hours
Wire.write(bin2bcd(30));//week, have no idea what this does...
Wire.write(bin2bcd(15));//days
Wire.write(bin2bcd(4));//months/
Wire.write(bin2bcd(9)); //year(2 digit year, ec 13 for 2013)
Wire.endTransmission();
Serial.println("clock is set!");
}
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
String reciveClock()
{
//fetches the realtime clock and saves it into a string and returns it.
/*uint8_tuint8_t seconds = 0;//Initialization time
uint8_t minutes = 0;
uint8_t hours = 0;
uint8_t week = 0;
uint8_t days = 0;
uint8_t months = 0;
uint16_t year = 0;
*/
String tempClock;
// vet inte vad denna bit gör? kan den tas bort i detta läge?
Wire.beginTransmission(0x68);//Send the address of DS1307
Wire.write(0);//Sending address
Wire.endTransmission();//The end of the IIC communication
//
Wire.requestFrom(0x68, 7);//IIC communication is started, you can continue to access another address (address auto increase) and the number of visits
uint8_t seconds = bcd2bin(Wire.read());
uint8_t minutes = bcd2bin(Wire.read());
uint8_t hours = bcd2bin(Wire.read());
uint8_t weeks = bcd2bin(Wire.read());
uint8_t days = bcd2bin(Wire.read());
uint8_t months = bcd2bin(Wire.read());
uint16_t year = bcd2bin(Wire.read());
//put the date into the tempClock variable.
tempClock=tempClock+"20"+year+"-";
if (months<10) {tempClock=tempClock+"0";}
tempClock=tempClock+months+"-";
if (days<10) {tempClock=tempClock+"0";}
tempClock=tempClock+days+" ";
//and then its time for ... time.. :)
if (hours<10) {tempClock=tempClock+"0";}
tempClock=tempClock+hours+":";
if (minutes<10) {tempClock=tempClock+"0";}
tempClock=tempClock+minutes+":";
if (seconds<10) {tempClock=tempClock+"0";}
tempClock=tempClock+seconds;
//Serial.println("from within:"+tempClock);
return tempClock;
}
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
int bcd2bin(int temp)//BCD to decimal
{
int a,b,c;
a=temp;
b=0;
if(a>=16)
{
while(a>=16)
{
a=a-16;
b=b+10;
c=a+b;
temp=c;
}
}
return temp;
}
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
int bin2bcd(int temp)//decimal to BCD
{
int a,b,c;
a=temp;
b=0;
if(a>=10)
{
while(a>=10)
{
a=a-10;
b=b+16;
c=a+b;
temp=c;
}
}
return temp;
}
TIP: you should improve the readability of your code. The first step is press CTRL-T before saving/copying the code.
That is an auto formatter that takes care of proper indenting which reflects the structure of your code.
Next step is to add empty lines to separate logical blocks and add spaces to improve readability of formulas.
I did a partial cleanup of the code to show how it can look like. (I added a few debug lines too)
/*
Clock functions.
scl=analog5
sda=analog4
ground and 5v
*/
#include <Wire.h>
void setup ()
{
Serial.begin(9600);
Wire.begin();
setClock();
}
void loop ()
{
Serial.print("now= ");
String timeNow = reciveClock();
Serial.println(timeNow);
delay(3000);
}
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
void setClock()
{
Wire.beginTransmission(0x68); //DS1307 write the initial time
Wire.write(0);
Wire.requestFrom(0x68, 7); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this is a strange line, as you are gonna write
//this is the exact order the itmes will be fetched in too.
Wire.write(bin2bcd(30));// seconds
Wire.write(bin2bcd(20));//minutes
Wire.write(bin2bcd(10));//hours
Wire.write(bin2bcd(30));//week, have no idea what this does...
Wire.write(bin2bcd(15));//days
Wire.write(bin2bcd(4));//months/
Wire.write(bin2bcd(9)); //year(2 digit year, ec 13 for 2013)
Wire.endTransmission();
Serial.println("clock is set!");
}
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
String reciveClock()
{
//fetches the realtime clock and saves it into a string and returns it.
String tempClock;
// vet inte vad denna bit gör? kan den tas bort i detta läge?
Wire.beginTransmission(0x68); //Send the address of DS1307
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(0x68, 7);//IIC communication is started, you can continue to access another address (address auto increase) and the number of visits
uint8_t seconds = bcd2bin(Wire.read());
uint8_t minutes = bcd2bin(Wire.read());
uint8_t hours = bcd2bin(Wire.read());
uint8_t weeks = bcd2bin(Wire.read());
uint8_t days = bcd2bin(Wire.read());
uint8_t months = bcd2bin(Wire.read());
uint16_t year = bcd2bin(Wire.read());
// DEBUG
Serial.println(year, DEC);
Serial.println(months, DEC);
Serial.println(days, DEC);
Serial.println(weeks, DEC);
Serial.println(hours, DEC);
Serial.println(minutes, DEC);
Serial.println(seconds, DEC);
// put the date into the tempClock variable.
tempClock = tempClock + "20" + year + "-";
if (months < 10)
{
tempClock += "0";
}
tempClock += months + "-";
if (days < 10)
{
tempClock += "0";
}
tempClock += days + " ";
//and then its time for ... time.. :)
if (hours < 10)
{
tempClock = tempClock + "0";
}
tempClock += hours + ":";
if (minutes < 10)
{
tempClock += "0";
}
tempClock += minutes + ":";
if (seconds < 10)
{
tempClock += "0";
}
tempClock += seconds;
return tempClock;
}
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
int bcd2bin(int temp) //BCD to decimal ????
{
int a,b,c;
a=temp;
b=0;
if(a>=16)
{
while(a>=16)
{
a=a-16;
b=b+10;
c=a+b;
temp=c;
}
}
return temp;
}
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
//*********************************************************************************************************************************************
int bin2bcd(int temp)//decimal to BCD
{
int a,b,c;
a=temp;
b=0;
if(a>=10)
{
while(a>=10)
{
a=a-10;
b=b+16;
c=a+b;
temp=c;
}
}
return temp;
}
Thank you for the input, i have tried your code and it gives me the exact same error, only 255 as minutes, seconds hours etc..
It seems way out to have to implement a new library to fix this, it do work on a regular arduino, but not on a arduino mega.
if i disconnect the SDA and SCL connections from analog4 and analog5, i get the exact same result! thats why I had thoughts that maybe you had to connect this in another way on a arduino mega board.