Hello,
I'm trying to create a binary clock using LEDs and shift registers.
The software appears to be malfunctioning because only the hours and some of the minutes work correctly.
Here is my code:
//Binary Clock v1.03
//May 21, 2019
#undef min
#include <Wire.h>
#include <DS3231.h>
DS3231 RTC(SDA, SCL);
Time Clock;
int datapin = 2;
int clockpin = 3;
int latchpin = 4;
int datapin2 = 8;
int clockpin2 = 9;
int latchpin2= 10;
void setup()
{
Serial.begin(57600);
Wire.begin();
RTC.begin();
RTC.setTime(1, 0, 0);
/* 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__));
} */
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(datapin2, OUTPUT);
pinMode(clockpin2, OUTPUT);
pinMode(latchpin2, OUTPUT);
}
void loop()
{
Time currentTime = RTC.getTime();
Time& CTime = currentTime; // defines CTime as an alias of currentTime
//DateTime now = Clock.Time();
// All used for checking the time of the clock
// This section can be removed when everything is working
Serial.print(CTime.date, DEC);
Serial.print('/');
Serial.print(CTime.mon, DEC);
Serial.print('/');
Serial.print(CTime.year, DEC);
Serial.print(' ');
Serial.print(CTime.hour, DEC);
Serial.print(':');
Serial.print(CTime.min, DEC);
Serial.print(':');
Serial.print(CTime.sec, DEC);
Serial.println();
// End of section that can be removed
int mins = CTime.min;
int secs = CTime.sec;
int hr = CTime.hour;
// convert to 12 hour time
if (hr>12)
{
hr = hr-12;
}
// variables to describe pattern of on lights
byte data1 = 0;
byte data2 = 0;
// encode the time
// hr = 1st four bits
for (int i =0;i<4;i++)
{
if (bitRead(hr,i)==1)
{
bitWrite(data1,3-i,1);
}
}
// mins on the first shift register
for (int i =2;i<6;i++)
{
if (bitRead(mins,i)==1)
{
bitWrite(data1,9-i,1);
}
}
// remaining mins LEDs
for (int i =0;i<2;i++)
{
if (bitRead(mins,i)==1)
{
bitWrite(data2,1-i,1);
}
}
// seconds
for (int i =2;i<8;i++)
{
if (bitRead(secs,i-2)==1)
{
bitWrite(data2,9-i,1);
}
}
// output the information
writeByte(data1,1);
writeByte(data2,2);
// a brief pause
delay(500);
}
void writeByte(byte data, byte set)
{
int d,c,l;
if (set==1)
{
d = 2;
c = 3;
l = 4;
}
else if (set==2)
{
d = 8;
c = 9;
l = 10;
}
shiftOut(d, c, MSBFIRST, data);
// toggle the latch pin so that the data appears as an output
digitalWrite(l, HIGH);
digitalWrite(l, LOW);
}
Thank you in advance!