I need some help with code for DS1307, please

Hi.

I’m trying to compile/upload this piece code (which I found on the internet) to arduino uno. This is for a binary clock. I’m trying to use a Real Time Clock module so I can get better accuracy. Physically, I have a DS1307 module connected to my setup. I’m not very good at coding. Please help. I’m trying to get the following code to work with my setup. I am not sure which RTClib.h or DS3231.h the owner of this code was using. But as it is, this code won’t compile and upload. The only RTClibrary I have is the one called “RTClib” which I downloaded from github.

So, i don’t know how to tie my ds1307 into this piece of code so that I can verify it and upload it. Can anyone please help? may be prompt me towards a better library with a piece of code that is guaranteed to work?

Here is my code:

/*
  An open-source binary clock for Arduino.
  Based on the code from by Rob Faludi (http://www.faludi.com)
  Code under (cc) by Lucas Berbesson
  http://creativecommons.org/license/cc-gpl
*/
#include <DS1307.h>

// Init the DS1307 using the hardware interface
DS1307  rtc(SDA, SCL);

// Init a Time-data structure
Time  t;


int second = 0, minute = 0, hour = 0; //start the time on 00:00:00
int munit, hunit, minuteTens, hourTens, valm = 0, valh = 0, ledstats, i;
// LEDS positions matrix
int leds[4][4] = {
  {-1, 11, -1,  4},
  {-1, 10,  7,  3},
  {13,  9,  6,  2},
  {12,  8,  5,  1}
};
void setup() {
  //set outputs
  for (int k = 0; k <= 13; k++) {
    pinMode(k, OUTPUT);
    digitalWrite(k, LOW);

  }
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  rtc.begin();
}

void loop() {
  t = rtc.getTime();
  second = t.sec;
  minute = t.min;
  hour = t.hour;


  munit = minute % 10; //sets the variable munit and hunit for the unit digits
  hunit = hour % 10;
  minuteTens = (int)(minute / 10);
  hourTens = (int)(hour / 10);
  //minutes units
  if (munit & 1) {
    digitalWrite(leds[3][3], HIGH);
  } else {
    digitalWrite(leds[3][3], LOW);
  }
  if (munit & 2) {
    digitalWrite(leds[2][3], HIGH);
  } else {
    digitalWrite(leds[2][3], LOW);
  }
  if (munit & 4) {
    digitalWrite(leds[1][3], HIGH);
  } else {
    digitalWrite(leds[1][3], LOW);
  }
  if (munit & 8) {
    digitalWrite(leds[0][3], HIGH);
  } else {
    digitalWrite(leds[0][3], LOW);
  }

  //minutes
  if (minuteTens & 1)  {
    digitalWrite(leds[3][2], HIGH);
  } else {
    digitalWrite(leds[3][2], LOW);
  }
  if (minuteTens & 2)  {
    digitalWrite(leds[2][2], HIGH);
  } else {
    digitalWrite(leds[2][2], LOW);
  }
  if (minuteTens & 4) {
    digitalWrite(leds[1][2], HIGH);
  } else {
    digitalWrite(leds[1][2], LOW);
  }

  //hour units
  if (hunit & 1) {
    digitalWrite(leds[3][1], HIGH);
  } else {
    digitalWrite(leds[3][1], LOW);
  }
  if (hunit & 2) {
    digitalWrite(leds[2][1], HIGH);
  } else {
    digitalWrite(leds[2][1], LOW);
  }
  if (hunit & 4) {
    digitalWrite(leds[1][1], HIGH);
  } else {
    digitalWrite(leds[1][1], LOW);
  }
  if (hunit & 8) {
    digitalWrite(leds[0][1], HIGH);
  } else {
    digitalWrite(leds[0][1], LOW);
  }

  //hour
  if (hourTens & 1)  {
    digitalWrite(leds[3][0], HIGH);
  } else {
    digitalWrite(leds[3][0], LOW);
  }
  if (hourTens & 2)  {
    digitalWrite(leds[2][0], HIGH);
  } else {
    digitalWrite(leds[2][0], LOW);
  }

  valm = digitalRead(A1);    // add one minute when pressed
  if (valm == HIGH) {
    minute++;
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }

  valh = digitalRead(A2);    // add one hour when pressed
  if (valh == HIGH) {
    hour++;
    if (hour > 24) {
      hour = 0;
    }
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }
  delay(50);
}

Your error tells you that you have not (or incorrectly) installed the DS3231 library.

Also, a DS3231 is not the same as a DS1307. So it might or might not work; you will have to look in the library to see what it states regarding that.

RTClib supports both the DS1307 and the DS3231. Else look for a dedicated DS1307 library. In both cases, use the examples in there to base your code on.

sterretje: Your error tells you that you have not (or incorrectly) installed the DS3231 library.

Also, a DS3231 is not the same as a DS1307. So it might or might not work; you will have to look in the library to see what it states regarding that.

RTClib supports both the DS1307 and the DS3231. Else look for a dedicated DS1307 library. In both cases, use the examples in there to base your code on.

Thanks.

Sorry but I know nothing about coding. Can you help?

I just need to put these together to finish a project. Going to be a Christmas gift for my wife. I am not sure where and how to find the right RTC library. I can't get hold of the guy who wrote this code. So, I don't know which Library he was using.

Any ideas on how I can get out of this situation? I have the RTClib.h installed. This one I got from arduino community. Perhaps someone could help me slightly adjust the code so that it would work with a more well known library???? It is just that I'm clueless. I woudn't know what I am looking at if I set about changing the code. I soo need somebody's help. please.

Cheers.

Hi,

Here is information and code that I know works for the DS1307:

http://arduino-info.wikispaces.com/DS1307_RealTime_Clock_Brick

terryking228:
Hi,

Here is information and code that I know works for the DS1307:

http://arduino-info.wikispaces.com/DS1307_RealTime_Clock_Brick

Ok. I googled bits and pieces of the code and looks like I have found the required library.
.
Here it is.

I managed to compile and upload the code to arduino uno. Now, something weird started to happen. When I power up the clock, the hours and minutes start to go up very quickly in parallel. As if someone is pressing and holding the toggle buttons to set the time. But the time goes all the way to 23:59 and stops climbing. Then after about 1 minute, the time becomes 00:00 and the whole thing starts all over again.

Please see the code above, in my main post. The rate at which the time goes up, is a tiny bit slower than if someone would press and hold the toggle buttons. I would say every half second.

I used to have a similar issue in the past when I had a wiring issues with the SET HOUR/MINUTE buttons. But back then, I did not have a Real Time Clock module in the system. Now I do. And this time, the LEDs toggle in a similar fashion but a bit slower than they did in the past (when I had wiring issues with buttons).

This is how I have wired the buttons. The arduino side of the buttons have landed on A1 and A2 PINs of the Arduino uno.

Any ideas on what it might be?

Please see if you can compare the code in my main post (which is currently my code uploaded into arduino) with the older code which is below. I think the main difference is that in the older code, the condition is “if valm<800” and “if valh<800” as opposed to “if (valm == HIGH)” and “if (valh == HIGH)” in the new code. I did not have any of these issues when I was using the older code which is this: (with no Real Time Clock module)

/*
An open-source binary clock for Arduino. 
Based on the code from by Rob Faludi (http://www.faludi.com)
Code under (cc) by Daniel Spillere Andrade, www.danielandrade.net
http://creativecommons.org/license/cc-gpl
*/

int second=0, minute=0, hour=0; //start the time on 00:00:00
int munit,hunit,valm=0,valh=0,ledstats,i;

void setup() { //set outputs and inputs
pinMode(1, OUTPUT);pinMode(2, OUTPUT);pinMode(3, OUTPUT);pinMode(4, OUTPUT);pinMode(5, OUTPUT);
pinMode(6, OUTPUT);pinMode(7, OUTPUT);pinMode(8, OUTPUT);pinMode(9, OUTPUT);pinMode(10, OUTPUT);
pinMode(11, OUTPUT);pinMode(12, OUTPUT);pinMode(13, OUTPUT);

pinMode(0, INPUT);
}

void loop() {

static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
// (static variables are initialized once and keep their values between function calls)
// move forward one second every 1000 milliseconds

if (millis() - lastTick >= 1000) {
	lastTick = millis();
	second++;

}

// move forward one minute every 60 seconds
	if (second >= 60) {
	minute++;
	second = 0; // reset seconds to zero
}

// move forward one hour every 60 minutes
if (minute >=60) {
	hour++;
	minute = 0; // reset minutes to zero
}

if (hour >=24) {
	hour=0;
	minute = 0; // reset minutes to zero
}

	munit = minute%10; //sets the variable munit and hunit for the unit digits
	hunit = hour%10;


	ledstats = digitalRead(0);  // read input value, for setting leds off, but keeping count
	if (ledstats == LOW) {
    
	for(i=1;i<=13;i++){
	digitalWrite(i, LOW);}
  
	} else  {

	//minutes units
	if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {  digitalWrite(1, HIGH);} else {  digitalWrite(1,LOW);}
	if(munit == 2 || munit == 3 || munit == 6 || munit == 7) {digitalWrite(2, HIGH);} else {digitalWrite(2,LOW);}
	if(munit == 4 || munit == 5 || munit == 6 || munit == 7) {digitalWrite(3, HIGH);} else {digitalWrite(3,LOW);}
	if(munit == 8 || munit == 9) {digitalWrite(4, HIGH);} else {digitalWrite(4,LOW);}

	//minutes 
	if((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60))  {digitalWrite(5, HIGH);} else {digitalWrite(5,LOW);}
	if(minute >= 20 && minute < 40)  {digitalWrite(6, HIGH);} else {digitalWrite(6,LOW);}
	if(minute >= 40 && minute < 60) {digitalWrite(7, HIGH);} else {digitalWrite(7,LOW);}

	//hour units
	if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {digitalWrite(8, HIGH);} else {digitalWrite(8,LOW);}
	if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {digitalWrite(9, HIGH);} else {digitalWrite(9,LOW);}
	if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {digitalWrite(10, HIGH);} else {digitalWrite(10,LOW);}
	if(hunit == 8 || hunit == 9) {digitalWrite(11, HIGH);} else {digitalWrite(11,LOW);}

	//hour
	if(hour >= 10 && hour < 20)  {digitalWrite(12, HIGH);} else {digitalWrite(12,LOW);}
	if(hour >= 20 && hour < 24)  {digitalWrite(13, HIGH);} else {digitalWrite(13,LOW);}

	}

	valm = analogRead(0);    // add one minute when pressed
	 if(valm<800) {
	 minute++;
	 second=0;
	 delay(250);
	}
  
	valh = analogRead(5);    // add one hour when pressed
	 if(valh<800) {
	 hour++;
	 second=0;
	 delay(250);
	}


}

justjohn:
You still doing something like this:
DS3231 rtc(SDA, SCL);
in your new code (didn’t see it in what you posted)
if so, and if using the arduino uno, then it seems that the library would set SCL to output, yet your reading it with:
valh = analogRead(5);

Not sure, but maybe that has “something” to do with a problem?

Not exactly. I am not sure I understand what you mean. In the old version of the code, There is no SDA or SCL. You need to pay more attention.

Additionally, like I said I am not very knowledgeable about electronics. So, please don’t be sarcastic.

I tried some troubleshooting on my own and removed the cables going to A2, A1 (arduino) and the problem seems to be gone. So, it is definitately related to buttons.

do you think I should change it to analog read instead of digital read?

or perhaps, use valm<800 equation instead of valm==HIGH??

Cheers.

arduinoware:
I tried some troubleshooting on my own and removed the cables going to A2, A1 (arduino) and the problem seems to be gone. So, it is definitately related to buttons.

do you think I should change it to analog read instead of digital read?

or perhaps, use valm<800 equation instead of valm==HIGH??

Cheers.

Using a switch as you are, you should use digital read along with “HIGH” and “LOW”, or at least that seems to make the most sense.

You have wired your button inputs on A0 and A1 with external pullups. The inputs will read HIGH when the button is not pressed, and LOW when pressed. Your imcrement logic is set for the reverse situation. Change the code

valm = digitalRead(A1);    // add one minute when pressed
 // if (valm == HIGH) {
     if (valm == LOW) {
    minute++;
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }

  valh = digitalRead(A2);    // add one hour when pressed
  //if (valh == HIGH) {
    if (valh == LOW) {   
    hour++;
    if (hour > 24) {
      hour = 0;
    }
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }
  delay(50);
}

cattledog: You have wired your button inputs on A0 and A1 with external pullups. The inputs will read HIGH when the button is not pressed, and LOW when pressed. Your imcrement logic is set for the reverse situation. Change the code

valm = digitalRead(A1);    // add one minute when pressed
 // if (valm == HIGH) {
     if (valm == LOW) {
    minute++;
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }

  valh = digitalRead(A2);    // add one hour when pressed   //if (valh == HIGH) {     if (valh == LOW) { 
    hour++;     if (hour > 24) {       hour = 0;     }     second = 0;     rtc.setTime(hour, minute, second);     delay(250);   }   delay(50); }

Thanks man. I appreciate. I will see if that helps. But you are mentioning A0 and A1. Did you mean A1 and A2? That is what I've got setup and the same for the code.

Cheers.

But you are mentioning A0 and A1. Did you mean A1 and A2?

Yes.

You may see this link for a UNO-DS1307-7segment Clock System using Wire.h and no RTC-library.

I'm guessing what "cattledog" suggested worked - I didn't catch that, but he's right.

I was trying to correct part of what I wrote if so, and if using the arduino uno, then it seems that the library would "set SCL to output" but you replied before I did. Because it was meant to read: would be "using SCL (A5) for output"

But that was after looking at his library, but realized you weren't even using A5 anyhow, so it was all just a moot point caused by me going between his library and your two sample codes. :confused:

cattledog: You have wired your button inputs on A0 and A1 with external pullups. The inputs will read HIGH when the button is not pressed, and LOW when pressed. Your imcrement logic is set for the reverse situation. Change the code

valm = digitalRead(A1);    // add one minute when pressed
 // if (valm == HIGH) {
     if (valm == LOW) {
    minute++;
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }

  valh = digitalRead(A2);    // add one hour when pressed   //if (valh == HIGH) {     if (valh == LOW) { 
    hour++;     if (hour > 24) {       hour = 0;     }     second = 0;     rtc.setTime(hour, minute, second);     delay(250);   }   delay(50); }

Yes. This did the trick. Problem solved.

But noticed something unusual for me. With other codes I did not see this happen. The thing is that, when I change the time (Hours/minutes) with the two buttons that I'm using, A1 and A2, all works well till I get to 23 on the hours and 59 on the minutes. Then I can toggle neither hours, nor minutes. So, lets say, if it is 23:59 right now and I decide to change it to 00:20, I have to wait till it becomes 00:00 and only then can I begin toggling again.

With other codes, I was able to toggle all the way through without a stop like this.

Can this be fixed by adjusting the code?

Cheers.

Did you also mean to do:

if (minute > 59) {
      minute = 0;
    }

justjohn: Did you also mean to do:

if (minute > 59) {
      minute = 0;
    }

Thanks. But would it be better to use if (hour >= 24) { hour = 0; }

instead of

if (hour > 24) { hour = 0; }

?

Cheers.

cattledog: You have wired your button inputs on A0 and A1 with external pullups. The inputs will read HIGH when the button is not pressed, and LOW when pressed.

Hi.

Can I change the button code to the following or will it interfere with getting time from the RTC?

valm = digitalRead(A1);    // add one minute when pressed
    if (valm == LOW) {
    minute++;
    if (minute >= 60) {
      hour++;
      minute = 0;
    }
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }

  valh = digitalRead(A2);    // add one hour when pressed
    if (valh == LOW) {   
    hour++;
    if (hour >= 24) {
      hour = 0;
      minute = 0;
    }
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }
  delay(50);
}

arduinoware:
Thanks. But would it be better to use
if (hour >= 24) {
hour = 0;
}

instead of

if (hour > 24) {
hour = 0;
}

?

Cheers.

It should be either a test for " > 23" or “== 24”, but not " >= 24" or " > 24". Although " >= 24" would work because the “=” part of the test. But the " >" part of it doesn’t make sense since 24 would be an invalid hour to have already sent to the clock in a prior iteration.

arduinoware:
Hi.

Can I change the button code to the following or will it interfere with getting time from the RTC?

valm = digitalRead(A1);    // add one minute when pressed

if (valm == LOW) {
   minute++;
   if (minute >= 60) {
     hour++;
     minute = 0;
   }
   second = 0;
   rtc.setTime(hour, minute, second);
   delay(250);
 }

valh = digitalRead(A2);    // add one hour when pressed
   if (valh == LOW) {  
   hour++;
   if (hour >= 24) {
     hour = 0;
     minute = 0;
   }
   second = 0;
   rtc.setTime(hour, minute, second);
   delay(250);
 }
 delay(50);
}

Seen this after I replied to the question about “just” the hour. As with the hour, the test for minutes should never be “>=” (it would work, but doesn’t make sense). It should be either " > 59" or " ==60" Also, after you increment the hour in the test for the minutes, you should also test the “hour” there for being greater than 23 or equal to 24.

justjohn: Seen this after I replied to the question about "just" the hour. As with the hour, the test for minutes should never be ">=" (it would work, but doesn't make sense). It should be either " > 59" or " ==60" Also, after you increment the hour in the test for the minutes, you should also test the "hour" there for being greater than 23 or equal to 24.

Thanks John. Although, I'm a little bit confused. Could you please help apply what you mean to the piece of code? I am not sure how to do it properly.

Right now, everything is working. real time clock is also working. But I can't toggle hours/minutes past 23:59. It can only go up to 23:59. if I need to set the time past that point, I'm having to wait till it is 00:00.

I don't know how to edit the code to fix that issue but also avoid interfering with time keeping of the RTC.

Cheers.

arduinoware: Thanks John. Although, I'm a little bit confused. Could you please help apply what you mean to the piece of code? I am not sure how to do it properly.

Right now, everything is working. real time clock is also working. But I can't toggle hours/minutes past 23:59. It can only go up to 23:59. if I need to set the time past that point, I'm having to wait till it is 00:00.

I don't know how to edit the code to fix that issue but also avoid interfering with time keeping of the RTC.

Cheers.

Do something like this instead:

valm = digitalRead(A1);    // add one minute when pressed
    if (valm == LOW) {
    minute++;
//    if (minute >= 60) {  //will work, but a little unusual for this scenario
    if (minute == 60) {
      hour++;
      if (hour == 24) hour = 0;  //new
      minute = 0;
    }
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }

  valh = digitalRead(A2);    // add one hour when pressed
    if (valh == LOW) {  
    hour++;
//    if (hour >= 24) {  //will work, but a little unusual for this scenario
    if (hour == 24) {
      hour = 0;
      minute = 0;  
    }
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }
  delay(50);  //any particular reason for this delay?
}