12hr conversion problem? ds3231 max7219 Nano custom LED clock

Any advice? Thanks in advance.

Problem: When clock hours reach double digits the clock crashes.
Works fine from 1:00 to 9:59.
I think problem is around line 218, where 24hr is converted to 12hr.
Thank you very much for taking a look.

// Maxmatrix library:
// https://brainy-bits.com/tutorials/scroll-text-using-the-max7219-led-dot-matrix/
// https://code.google.com/p/arudino-maxmatrix-library/
// niq_ro from
// http://arduinotehniq.blogspot.com
// http://nicuflorica.blogspot.ro/
// original sketch - ver.4.0, Craiova, Romania, 5.8.2015

#include <MaxMatrix.h>
#include <avr/pgmspace.h>
#include <DS3231.h>    //JC
// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

int data = 12;    // DIN pin of MAX7219 module
int load = 10;    // CS pin of MAX7219 module
int clock = 11;  // CLK pin of MAX7219 module
 
int maxInUse = 4;  //how many MAX7219 are connected
MaxMatrix m(data, load, clock, maxInUse); // define Library
 
byte buffer[11];  //***JC
 
// char string1[] = " niq_ro test sketch from https://brainy-bits.com/tutorials/scroll-text-using-the-max7219-led-dot-matrix/        ";  // Scrolling Text
 //char string4[] = "    temperature:        "; // Fixxed text
 //char string5[] = "    relative humidity:        "; // Fixxed text
 char string6[] = "    clock:        "; // Fixxed text
// char string7[] = "    date:        "; // Fixxed text

#include <Wire.h>
#include "RTClib.h" // from https://github.com/adafruit/RTClib


RTC_DS1307 RTC; // Tells the RTC library that we're using a DS1307 RTC

byte spid = 35; 


void setup(){
  m.init(); // module MAX7219
  m.setIntensity(1); // LED Intensity 0-15
  byte c;
  m.shiftLeft(false, true);
  Wire.begin();
  RTC.begin();      //works with either lib
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
}
int ftmp;
void loop(){


float te = 19.9; //--1307 - old read float -- sensor not present
if (!ftmp) te = te * 1.8 + 32;
//te = rtc.getTemp();  //--3231   -jc   -this should be the value here.
int t2 = 10*te; 


// http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/
char c[3], d[2]; 
String str1, str2;
int t2z = t2/10;
int t2u = t2 - t2z*10;
str1=String(t2z);
str1.toCharArray(c,3);
str2=String(t2u);
str2.toCharArray(d,2);

printString1(" ", 1);   //put OFF colon for temp

//   TEMPERATURE DIGITS IN C AND D
printString1(c, 10);
printString1(d, 18);
if (ftmp){
  ftmp = 0;
  printString1("c", 26);
}else{
  ftmp = 25;
  printString1("*", 26);
  
}
delay(5000);

DateTime now = RTC.now();
int ora = now.hour();
int minut = now.minute();

//  ***e and f are the hour and minute
// http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/
char e[3], f[3]; 
String str3, str4;
str3=String(ora);
str3.toCharArray(e,3);
str4=String(minut);
str4.toCharArray(f,3);


//  *** Print hour  *********
if (ora > 12) ora = ora - 12;  //convert from 24hr time
if (ora >= 10){
  printString1("1", 2);  
  printString1(e-10, 10);
  //printString1(e, 2); // same as line above
}
else
{
  printString1(" ", 2);  
  printString1(e, 10);
}

printString1(":", 2);   //put colon for time
//  *** print minute  **********
if (minut >= 10) printString1(f, 18);
else
{
  printString1("0", 18);
  printString1(f, 26);
}
delay(5000);
}
 
// Put extracted character on Display
void printCharWithShift(char c, int shift_speed){
  if (c < 32) return;
  c -= 32;
  memcpy_P(buffer, CH + 8*c, 8);//***JC
  m.writeSprite(maxInUse*8, 0, buffer);
  m.setColumn(maxInUse*8 + buffer[0], 0);
  
  for (int i=0; i<buffer[0]+1; i++) 
//    for (int i=0; i<buffer[0]+1; i++) 
  {
    delay(shift_speed);
    m.shiftLeft(false, false);
  }
}
 
// Extract characters from Scrolling text
void printStringWithShift(char* s, int shift_speed){
  
  while (*s != 0){
    printCharWithShift(*s, shift_speed);
    s++;
  }
}

void printString(char* s)
{
  int col = 0;
  while (*s != 0)
  {
    if (*s < 32) continue;
    char c = *s - 32;
    memcpy_P(buffer, CH + 8*c, 8);  //***JC
    m.writeSprite(col, 0, buffer);
    m.setColumn(col + buffer[0], 0);
    col += buffer[0] + 1;
    s++;
  }
}

void printString1(char* s, int col)
{
  int col1 = col;
  while (*s != 0)
  {
    if (*s < 32) continue;
    char c = *s - 32;
    memcpy_P(buffer, CH + 8*c, 8);  //***JC
    m.writeSprite(col, 0, buffer);
    m.setColumn(col + buffer[0], 0);
    col += buffer[0] + 1;
    s++;
  }
}

AlmostCompleteNANO.ino (11.8 KB)

The names of the variables in your code make no sense. There is NO excuse for using String to do the conversion from int or byte to string. itoa() or sprintf() have FAR less overhead AND make your code a heck of a lot easier to understand.

hourString or hour_string would make a lot more sense than e as a variable name.

Thank you for taking a look. Any thoughts on why the clock doesn't display 10-12:00 correctly?
I edited the post to display the entire sketch, except the font. Thanks again.

I edited the post to display the entire sketch, except the font.

It's still pissing away resources using the String class uselessly. It still has piss-poor names for the variables. Try again.

I dont know how to program. I am simply asking for a possible solution to a problem with a 3d printing project I am working on. It is a replica of a time/temperature sign in NYC. I have spent many months building it and this sketch is the last detail. Thanks for your time.

IMG_1419.JPG

Ijoyce88:
I dont know how to program.

That's why you are having problems.

It is a replica of a time/temperature sign in NYC. I have spent many months building it and this sketch is the last detail.

Programming is not just a "detail".
Having the programming right is as important to making your project work as having the wiring right.

Now, let's take a look at your code.

DateTime now = RTC.now();
int ora = now.hour();
int minut = now.minute();

So, the hour and minute are stored as integers in the variables ora and minut, respectively.
Integers are cool. You can do arithmetic on integers.

//  ***e and f are the hour and minute
// http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/
char e[3], f[3];
String str3, str4;
str3=String(ora);
str3.toCharArray(e,3);
str4=String(minut);
str4.toCharArray(f,3);

OK, what's happening here is that you are converting those integers into character arrays. You are doing it in a very clumsy way, but it appears to be working.
Note that the comment ("e and f are the hour and minute") might be a bit misleading. e and f are not integers. They are character arrays. You cannot do arithmetic on character arrays the way you would on integers! (Well, you can try, but the results won't be what you expect!)

//  *** Print hour  *********
if (ora > 12) ora = ora - 12;  //convert from 24hr time

Too late now. You have already done the conversion to character array. Changing the value of the integer now will not retroactively change what's in the character array.

if (ora >= 10){
  printString1("1", 2); 
  printString1(e-10, 10);
  //printString1(e, 2); // same as line above
}

Remember, e is a character array. You can't just subtract from it the way you are trying to do.
As I said, arithmetic on character arrays won't work as you expect it to work.

If you want to do arithmetic, first do it on a number (such as an integer), then convert it to a character array.

Thank you very much for your help. I forwarded this to my friend who is a programmer or engineer (i dont know because because its all way over my head. Im an electrician and he is a software...something). He does not program C or C++ or arduino but he said that this bit of info is enough for him to correct the problem in the sketch so that it will work for my project. Thank you!