corrected:
/* Clock Main Time Frame 3 x Max 7219 & 7 Segments
August 2021
*/
#include <Wire.h>
#include <SPI.h>
#include "RTClib.h" //https://github.com/adafruit/RTClib
#include "LedControl.h"
RTC_DS3231 rtc;
LedControl lc(12, 11, 10, 3);//data,latch,clock,modules
void setup() {
Serial.begin(115200);
rtc.begin();
PORTD |= B11111100;
pinMode(12, OUTPUT);
//we have already set the number of devices when we created the LedControl
int devices = lc.getDeviceCount();
//we have to init all devices in a loop
for (int address = 0; address < devices; address++) {
lc.shutdown(address, false);
lc.setIntensity(address, 8);
lc.clearDisplay(address);
}
Serial.println("Started");
lc.setLed(0, 2, 7, true);
lc.setLed(0, 5, 7, true);
}
void loop() {
static byte oldledSecs = 0;
DateTime now = rtc.now();
if (now.second() != oldledSecs) { // only execute on change
oldledSecs = now.second(); // ready for next change
if (now.hour() < 10 )lc.setChar(1, 0, ' ' , true);
else lc.setDigit(1, 0, now.hour() / 10, true);
lc.setDigit(1, 1, now.hour() % 10, true);
lc.setDigit(1, 2, now.minute() / 10, true);
lc.setDigit(1, 3, now.minute() % 10, true);
lc.setDigit(0, 0, now.day() / 10, true);
lc.setDigit(0, 1, now.day() % 10, true);
lc.setDigit(0, 3, now.month() / 10, true);
lc.setDigit(0, 4, now.month() % 10, true);
lc.setDigit(0, 6, now.year() / 10, true);
lc.setDigit(0, 7, now.year() % 10, true);
char dataString[30];
sprintf(dataString, "%02d.%02d.%02d %02d:%02d:%02d\r\n", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
Serial.print(dataString);
}
int keypress = getKey();
if (keypress == 4) {
int NH = now.hour();
int NM = now.minute();
int keypress = 0;
while (getKey() == 4);
delay(25);
while (keypress != 4) {
keypress = getKey();
if (keypress == 1) {
NH++;
lc.setDigit(1, 0, NH / 10, true);
lc.setDigit(1, 1, NH % 10, true);
}
if (keypress == 2) {
NM++;
lc.setDigit(1, 2, NM / 10, true);
lc.setDigit(1, 3, NM % 10, true);
}
}
rtc.adjust(DateTime(now.year(), now.month(), now.day(), NH, NM, 0));
}
if (keypress == 8) {
byte ND = now.day();
byte NM = now.month();
byte NY = now.year();
byte keypress = 0;
while (getKey() == 8);
delay(25);
while (keypress != 8) {
keypress = getKey();
if (keypress == 1) {
ND++;
lc.setDigit(0, 0, ND / 10, true);
lc.setDigit(0, 1, ND % 10, true);
}
if (keypress == 2) {
NM++;
lc.setDigit(0, 3, NM / 10, true);
lc.setDigit(0, 4, NM % 10, true);
}
if (keypress == 16) {
NY++;
lc.setDigit(0, 6, NY / 10, true);
lc.setDigit(0, 7, NY % 10, true);
}
}
NY = 2000 + NY;
rtc.adjust(DateTime(NY, NM, ND, now.hour(), now.minute(), 0));
}
delay(20);
}
int getKey() {
int retkey = 0;
while (PIND & B11111100); // let buttons loose
if (digitalRead(2) == LOW) retkey += 1;
if (digitalRead(3) == LOW) retkey += 2;
if (digitalRead(4) == LOW) retkey += 2;
if (digitalRead(6) == LOW) retkey += 4;
if (digitalRead(7) == LOW) retkey += 8;
if (digitalRead(5) == LOW) retkey += 16;
if (retkey) delay(500);
return retkey;
}