I'm trying to make a clock using a 4-digits 7-segments display, an Arduino Leonardo and a DS3231 RTC. I got already the code for making the hour in a char array, but I'm having a hard time trying to display it correctly to the 7segs. When the arduino sends the hour to the displays, they only flashes it for an instant and then they turn of waiting for the next second to display the hour again and so on. I had the same issue using the code but adapted to use with a LCD, but I managed to fix it with just a delay. I tried the same with the 7segs but nothing happened. This is my code so far:
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
#include <Time.h> //http://playground.arduino.cc/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
#include <SevSeg.h> //https://github.com/sparkfun/SevSeg
SevSeg reloj7s;
char horaC[3], minC[3], horaDisp[5];
void setup(void)
{
Serial.begin(115200);
//setSyncProvider() causes the Time library to synchronize with the
//external RTC by calling RTC.get() every five minutes by default.
setSyncProvider(RTC.get);
Serial << F("RTC Sync");
if (timeStatus() != timeSet) Serial << F(" FAIL!");
Serial << endl;
int displayType = COMMON_ANODE;
int digit1 = A0; //Pin 12 on my 4 digit display
int digit2 = A1; //Pin 9 on my 4 digit display
int digit3 = 4; //Pin 8 on my 4 digit display
int digit4 = 5; //Pin 6 on my 4 digit display
//Declare what pins are connected to the segments
int segA = 6; //Pin 11 on my 4 digit display
int segB = 7; //Pin 7 on my 4 digit display
int segC = 8; //Pin 4 on my 4 digit display
int segD = 9; //Pin 2 on my 4 digit display
int segE = 10; //Pin 1 on my 4 digit display
int segF = 11; //Pin 10 on my 4 digit display
int segG = 12; //Pin 5 on my 4 digit display
int segDP= 13; //Pin 3 on my 4 digit display
int numberOfDigits = 4; //Do you have a 1, 2 or 4 digit display?
reloj7s.Begin(displayType, numberOfDigits, digit1, digit2, digit3, digit4, segA, segB, segC, segD, segE, segF, segG, segDP);
reloj7s.SetBrightness(100);
}
void loop(void)
{
static time_t tLast;
time_t t;
tmElements_t tm;
t = now();
if (t != tLast) {
tLast = t;
printDateTime(t);
}
}
//print date and time to Serial
void printDateTime(time_t t)
{
printDate(t);
printTime(t);
}
//I think the error may be here
//print time to 7S
void printTime(time_t t) {
if(hour(t)<10){
sprintf(horaC,"%d",hour(t));
horaDisp[0]='0';
horaDisp[1]=horaC[0];
}
else{
sprintf(horaC,"%d",hour(t));
horaDisp[0]=horaC[0];
horaDisp[1]=horaC[1];
}
if(minute(t)<10){
sprintf(minC,"%d",minute(t));
horaDisp[2]='0';
horaDisp[3]=minC[0];
}
else{
sprintf(minC,"%d",minute(t));
horaDisp[2]=minC[0];
horaDisp[3]=minC[1];
}
reloj7s.DisplayString(horaDisp,2); //This is the function that sends the hour charArray to the 7Segs.
Serial.println(horaDisp);
}
//Ignore from this line
//print date to 7S
void printDate(time_t t)
{
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
}
//Print an integer in "00" format (with leading zero),
//followed by a delimiter character to Serial.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
return;
}
I think the error might be in the printTime function while sending the reloj7s.DisplayString, but I'm not so sure. Can anybody help me how to fix this? Thanks.