i have a project clock i have been working on, and built a library (bad one) for my seven segments using 4 shift register. everything works the way i want, but when displaying the year, it reads 2023 instead of 2024 and i cannot figure out why.
#include "SegDisplay.h"
#include "RTClib.h"
RTC_DS3231 rtc;
SegDisplay SegDisplay(D6,D8,D7); //D6,D7,D8 data clock latch
const int dot1 = D0; //colon dot D0
//------------------------ Main Setup -----------------------------------------//
void setup(){
Serial.begin(115200);
pinMode (dot1, OUTPUT);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
//--------------------------- Loop - display time and date from NTP if connected ---------------------------------------------//
void loop(){
DateTime now = rtc.now();
SegDisplay.updateDisplay(now.twelveHour(),now.twelveHour(),now.minute(),now.minute());
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
delay(1000);
SegDisplay.DisplayDate(now.day(),now.day(),now.month(),now.month());
Serial.print(now.day());
Serial.print("-");
Serial.print(now.month());
Serial.print("-");
Serial.println(now.year());
delay(1000);
SegDisplay.DisplayYear(now.year());
delay(1000);
digitalWrite(dot1, HIGH);
delay(400);
digitalWrite(dot1, LOW);
delay(400);
}
library h.file
#include "Arduino.h"
#ifndef SegDisplay_h
#define SegDisplay_h
class SegDisplay
{
public:
unsigned ShiftRegisters;
SegDisplay(uint8_t DataPin, uint8_t ClockPin, uint8_t LatchPin);
void sendSerialData(byte registerCount, byte *pValueArray);
void updateDisplay(uint8_t data1, uint8_t data2, uint8_t data3, uint8_t data4);
void clearDisplay();
void blinkDisplay();
void DisplayTemp(uint8_t data1, uint8_t data2);
void DisplayHumid(uint8_t data1, uint8_t data2);
void DisplayDate(uint8_t data1, uint8_t data2, uint8_t data3, uint8_t data4);
void DisplayYear(uint8_t data1);
private:
uint8_t _DataPin;
uint8_t _ClockPin;
uint8_t _LatchPin;
uint8_t _data1;
uint8_t _data2;
uint8_t _data3;
uint8_t _data4;
};
#endif
library cpp.file
#include "Arduino.h"
#include "SegDisplay.h"
#define ShiftRegisters 4
SegDisplay::SegDisplay(uint8_t DataPin, uint8_t ClockPin, uint8_t LatchPin){
pinMode(DataPin, OUTPUT);
pinMode(ClockPin, OUTPUT);
pinMode(LatchPin, OUTPUT);
_DataPin = DataPin;
_ClockPin = ClockPin;
_LatchPin = LatchPin;
}
void SegDisplay::updateDisplay(uint8_t data1, uint8_t data2, uint8_t data3, uint8_t data4){
byte RegArray [ShiftRegisters];
byte Digits [10];
Digits [0] = 16 + 32 + 64 + 2 + 4 + 1;
Digits [1] = 32 + 64;
Digits [2] = 16 + 32 + 8 + 4 + 2;
Digits [3] = 16 + 32 + 8 + 64 + 2;
Digits [4] = 1 + 8 + 32 + 64;
Digits [5] = 16 + 1 + 8 + 64 + 2;
Digits [6] = 16 + 1 + 4 + 2 + 64 + 8;
Digits [7] = 16 + 32 + 64;
Digits [8] = 2 + 64 + 32 + 16 + 1 + 8 + 4;
Digits [9] = 2 + 64 + 32 + 16 + 1 + 8;
Digits [99] = 0;
_data1 = data1;
_data2 = data2;
_data3 = data3;
_data4 = data4;
RegArray[0] = Digits[_data1 / 10];
RegArray[1] = Digits[_data2 % 10];
RegArray[2] = Digits[_data3 / 10];
RegArray[3] = Digits[_data4 % 10];
SegDisplay::sendSerialData(ShiftRegisters, RegArray);
}
void SegDisplay::clearDisplay(){
byte RegArray [ShiftRegisters];
RegArray[0] = 0;
RegArray[1] = 0;
RegArray[2] = 0;
RegArray[3] = 0;
SegDisplay::sendSerialData(ShiftRegisters, RegArray);
}
void SegDisplay::blinkDisplay(){
byte RegArray [ShiftRegisters];
byte Digits [2];
Digits [8] = 8 + 4 + 2 + 64 + 32 + 1 + 16;
Digits [99] = 0;
for(int i=0;i<5;i++){
RegArray[0] = Digits[8];
RegArray[1] = Digits[8];
RegArray[2] = Digits[8];
RegArray[3] = Digits[8];
SegDisplay::sendSerialData(ShiftRegisters, RegArray);
delay(200);
RegArray[0] = Digits[99];
RegArray[1] = Digits[99];
RegArray[2] = Digits[99];
RegArray[3] = Digits[99];
SegDisplay::sendSerialData(ShiftRegisters, RegArray);
delay(200);
}
}
void SegDisplay::DisplayTemp(uint8_t data1, uint8_t data2){
byte RegArray [ShiftRegisters];
byte Digits [14];
Digits [0] = 16 + 32 + 64 + 2 + 4 + 1;
Digits [1] = 32 + 64;
Digits [2] = 16 + 32 + 8 + 4 + 2;
Digits [3] = 16 + 32 + 8 + 64 + 2;
Digits [4] = 1 + 8 + 32 + 64;
Digits [5] = 16 + 1 + 8 + 64 + 2;
Digits [6] = 16 + 1 + 4 + 2 + 64 + 8;
Digits [7] = 16 + 32 + 64;
Digits [8] = 2 + 64 + 32 + 16 + 1 + 8 + 4;
Digits [9] = 2 + 64 + 32 + 16 + 1 + 8;
Digits [99] = 0;
Digits [95] = 16 + 32 + 8 + 1; // Degree dot
Digits [96] = 16 + 1 + 4 + 2; // Capital C
Digits [97] = 4 + 8; // r, 80
Digits [98] = 1 + 4 + 8 + 64; // h, 116
_data1 = data1;
_data2 = data2;
RegArray[0] = Digits[data1 / 10];
RegArray[1] = Digits[data2 % 10];
RegArray[2] = Digits[95];
RegArray[3] = Digits[96];
SegDisplay::sendSerialData(ShiftRegisters, RegArray);
}
void SegDisplay::DisplayHumid(uint8_t data1, uint8_t data2){
byte RegArray [ShiftRegisters];
byte Digits [14];
Digits [0] = 16 + 32 + 64 + 2 + 4 + 1;
Digits [1] = 32 + 64;
Digits [2] = 16 + 32 + 8 + 4 + 2;
Digits [3] = 16 + 32 + 8 + 64 + 2;
Digits [4] = 1 + 8 + 32 + 64;
Digits [5] = 16 + 1 + 8 + 64 + 2;
Digits [6] = 16 + 1 + 4 + 2 + 64 + 8;
Digits [7] = 16 + 32 + 64;
Digits [8] = 2 + 64 + 32 + 16 + 1 + 8 + 4;
Digits [9] = 2 + 64 + 32 + 16 + 1 + 8;
Digits [99] = 0;
Digits [95] = 16 + 32 + 8 + 1; // Degree dot
Digits [96] = 16 + 1 + 4 + 2; // Capital C
Digits [97] = 4 + 8; // r, 80
Digits [98] = 1 + 4 + 8 + 64; // h, 116
_data1 = data1;
_data2 = data2;
RegArray[0] = Digits[data1 / 10];
RegArray[1] = Digits[data2 % 10];
RegArray[2] = Digits[97];
RegArray[3] = Digits[98];
SegDisplay::sendSerialData(ShiftRegisters, RegArray);
}
void SegDisplay::DisplayDate(uint8_t data1, uint8_t data2, uint8_t data3, uint8_t data4){
byte RegArray [ShiftRegisters];
byte Digits [10];
Digits [0] = 16 + 32 + 64 + 2 + 4 + 1;
Digits [1] = 32 + 64;
Digits [2] = 16 + 32 + 8 + 4 + 2;
Digits [3] = 16 + 32 + 8 + 64 + 2;
Digits [4] = 1 + 8 + 32 + 64;
Digits [5] = 16 + 1 + 8 + 64 + 2;
Digits [6] = 16 + 1 + 4 + 2 + 64 + 8;
Digits [7] = 16 + 32 + 64;
Digits [8] = 2 + 64 + 32 + 16 + 1 + 8 + 4;
Digits [9] = 2 + 64 + 32 + 16 + 1 + 8;
Digits [99] = 0;
_data1 = data1;
_data2 = data2;
_data3 = data3;
_data4 = data4;
RegArray[0] = Digits[data1 / 10];
RegArray[1] = Digits[data2 % 10];
RegArray[2] = Digits[data3 / 10];
RegArray[3] = Digits[data4 % 10];
SegDisplay::sendSerialData(ShiftRegisters, RegArray);
}
void SegDisplay::DisplayYear(uint8_t data1){
byte RegArray [ShiftRegisters];
byte Digits [10];
Digits [0] = 16 + 32 + 64 + 2 + 4 + 1;
Digits [1] = 32 + 64;
Digits [2] = 16 + 32 + 8 + 4 + 2;
Digits [3] = 16 + 32 + 8 + 64 + 2;
Digits [4] = 1 + 8 + 32 + 64;
Digits [5] = 16 + 1 + 8 + 64 + 2;
Digits [6] = 16 + 1 + 4 + 2 + 64 + 8;
Digits [7] = 16 + 32 + 64;
Digits [8] = 2 + 64 + 32 + 16 + 1 + 8 + 4;
Digits [9] = 2 + 64 + 32 + 16 + 1 + 8;
Digits [99] = 0;
_data1 = data1;
RegArray[0] = Digits[(data1 % 1000)/100];
RegArray[1] = Digits[data1 / 1000];
RegArray[2] = Digits[data1 / 100];
RegArray[3] = Digits[(data1 % 100)/ 10];
SegDisplay::sendSerialData(ShiftRegisters, RegArray);
}
void SegDisplay::sendSerialData (byte registerCount, byte *pValueArray) {
// Signal to the 595s to listen for data
digitalWrite (_LatchPin, LOW);
for (byte reg = registerCount; reg > 0; reg--)
{
byte value = pValueArray [reg - 1];
for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
{
digitalWrite (_ClockPin, LOW);
digitalWrite (_DataPin, value & bitMask ? HIGH : LOW);
digitalWrite (_ClockPin, HIGH);
}
}
// Signal to the 595s that I'm done sending
digitalWrite (_LatchPin, HIGH);
} // sendSerialData
any help would be much appreciated. and before you ask, yes i have tried using other libraries, but i do not understand what parts do what, my code I can understand.