Need help adding DS3231 RTC to my binary clock

This is my new 24-hour, arduino uno binary clock. It features nice french arcitecture and circuitry with dual transistor constant current setup for 1 watt, 350ma LEDs. The clock is made of mdf. If anyone is interested, I can post electrical schematics.

I would like to add the DS3231 RTC module so that it can keep better time.

I have included my latest revision of the code. But I am not sure how to remove the “millis” thing and integrate the DS3231 into the code. Please help. Many thanks in advance.

Cheers.




Here is the LED layout for the project:

/*
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 <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  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] = {
 {17,1,17,0},
 {17,13,2,3},
 {10,9,7,4},
 {11,12,8,5}
};
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);
}

Nice clock!

pert:
Nice clock!

Thanks

Looks great, but does it keep good time? I would work on that first before I worry about special effects.

Your simplest route would be an rtc module (I would recommend a ds3231 based module). Alternatives are using an internet time server or an atomic clock radio receiver module, interesting options but would require more changes.

At the very least, I would fix your code. It will run slight slow because of the way you wrote the code.

PaulRB:
Looks great, but does it keep good time? I would work on that first before I worry about special effects.

Your simplest route would be an rtc module (I would recommend a ds3231 based module). Alternatives are using an internet time server or an atomic clock radio receiver module, interesting options but would require more changes.

At the very least, I would fix your code. It will run slight slow because of the way you wrote the code.

Thanks. I just got myself a DS3231 module. But I am not really sure which pins to connect to. The manual says SDA and SCL should connect to A4 and A5. But right now the A5 is used to toggle minutes, as you can see in the code. Sorry for a noobie question. But can I use perhaps A2 or A3 to toggle minutes to free the A5 for DS3231? Thanks.

The binary decoding is painfully inefficient. The variables hour, minute, second variables already contain binary values. The outputs can be set directly from those bit-by-bit by using bitwise operators or this:
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bit/

arduinoware:
Thanks. I just got myself a DS3231 module. But I am not really sure which pins to connect to. The manual says SDA and SCL should connect to A4 and A5. But right now the A5 is used to toggle minutes, as you can see in the code. Sorry for a noobie question. But can I use perhaps A2 or A3 to toggle minutes to free the A5 for DS3231? Thanks.

Yes you can change the wiring. The DS3231 is arguably the simplest solution for reliable timekeeping on the Arduino.

aarg:
Yes you can change the wiring. The DS3231 is arguably the simplest solution for reliable timekeeping on the Arduino.

Thanks for quick replies. I appreciate that. But are you sure? Someone told me only A0, A5 can be used for push buttons to toggle hour/minute?

arduinoware:
Thanks for quick replies. I appreciate that. But are you sure? Someone told me only A0, A5 can be used for push buttons to toggle hour/minute??

Cheers.

Did you investigate the claim?

aarg:
Did you investigate the claim?

My arduino manual says it has dedicated SDA and SCL pins on the right hand side of arduino, above pin #13. Are these dedicated SDA and SCL pins shorted with A4 and A5? Sorry, if it sounds weird. I am not too knowledgeable about electronics, just trying to finish this project because it will be a christmas present for someone.

arduinoware:
My arduino manual says it has dedicated SDA and SCL pins on the right hand side of arduino, above pin #13. Are these dedicated SDA and SCL pins shorted with A4 and A5? Sorry, if it sounds weird. I am not too knowledgeable about electronics, just trying to finish this project because it will be a christmas present for someone.

Yes, they are common with A4-A5 on the Uno.

aarg:
Yes, they are common with A4-A5 on the Uno.

Thanks. So, since A4-A5 will be occupied by DS3231, my only other option for push button is to use A2-A3-A4. Do I need to declare anything in the code? I just copied this code from someone. Do I just change "analogRead(5) to analogRead(2) in the following piece of code?

}

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

Yes, and in any other places it is used, if there are any.

It's much better to "#define" names for the pins you want to use at the top of your sketch, then use that name in the rest of the code. That way, if any pins are used repeatedly in the sketch, you only have to change the pin number in one place.

For example, at top of sketch:

#define HOUR_BUTTON A2

Then later you can put

valh = analogRead(HOUR_BUTTON);

arduinoware:
Someone told me only A0, A5 can be used for push buttons to toggle hour/minute?

They probably meant you can only use those pins unless you change the code.

To be honest, once you have the ds3231 working, you will probably not need the buttons to set the time. Ds3231 is very accurate and with the small coin cell on the module it will keep very good time for years. So you can set the time once by uploading it from the pc, and you may not need to do it again for many years, so probably not worth having the buttons. You can even program in the time changes for daylight savings time to happen automatically.

arduinoware:
Thanks. So, since A4-A5 will be occupied by DS3231, my only other option for push button is to use A2-A3-A4. Do I need to declare anything in the code? I just copied this code from someone.

Never, never “just copy”. It’s a recipe for disaster.

How can you use A4 for a button if it’s already in use for I2C?

You seem to think that you can only do an analog read from the analog pins. Such is not the case. They can be set as digital inputs/output in the same way that the digital pins can.

Do the RTC library example sketches work?

I’ll happily review your code when 1. you take my advice about defining your pin numbers and 2. you format (ctrl-T) and re-post it. Because right now is very difficult to follow.

PaulRB:
... and 2. you format (ctrl-T) and re-post it. Because right now is very difficult to follow.

It will take more than that, there are multiple statements and brackets on one line that it won't modify. However there is little point in making it look pretty while it uses that brain dead method of binary output selection ... I mentioned it earlier on.

aarg:
It will take more than that, there are multiple statements and brackets on one line that it won't modify. However there is little point in making it look pretty while it uses that brain dead method of binary output selection ... I mentioned it earlier on.

Guys, are you serious? If I knew all the things that you are already asking of me, perhaps I wouldn't be asking you for help. Like I said, I don't know how to write/edit code. Just using someone else's code to make this thing work. I have already achieved that as you can see from the pictures. I just need to introduce DS3231 into the system as far as the code goes. And you either help or not help. So far, I am only getting philosophy from you. No disrespect.