program stop running after a while

Hi folks,

I programmed a simple digital clock. It has a 4 7-segment digits, DS3231 RTC, 2 pushbuttons. On arduino uno. Please keep in mind im a newbie, this is my first project, and my C skills are ok, no more.

The program first ask the user to enter the hour (from 00 to 23) then minutes (00 to 59). Once both of them were input by the user, it should start running, allegedly forever?

the problem is that after some time it just stop running without a hint.
When I set the loop to run every 5 secons it stop after 38:25 minutes (around 460 iterations), when I set the loop to run every 1 second it stopped at exactly 16:24, around 980 iterations.

Any clue what might be the reason

Here is the full code, thanks in advance for any help!

#include <Wire.h>
#include <RTClib.h>
#include <Time.h> //Time Library
#include <math.h>

//************************************//

RTC_DS3231 RTC;

int okPin = 6; //ok push button
int altPin = 7; //2nd push button

int okState = 0;
int lastOkState = 0;
int altState = 0;
int lastAltState = 0;

//pins for 7 segments
int latchPin = 13; // connect to LCK pin
int clockPin = 12; // connect to CLK pin
int dataPin = 11; // connect to SDI pin

//7 segments symbols
int LED_SEG_TAB[]={
0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0x01,0xee,0x3e,0x1a,0x7a,0x9e,0x8e,0x01,0x00};
//0 1 2 3 4 5 6 7 8 9 dp . a b c d e f off

int Hour;
int Minute;

void setup()
{

int counter = 1;

//setup push buttons
pinMode(altPin, INPUT); // declare pushbutton as input
pinMode(okPin, INPUT); // declare pushbutton as input

//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
//end set pins to output so you can control the shift register

Serial.begin(9600);
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(1979, 05, 30, 23, 59, 0));
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}

if (RTC.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
RTC.adjust(DateTime(1979, 05, 30, 23, 59, 0));
}

Hour = 23;
Minute = 59;
showNumber(Hour, Minute);
Hour = getHour(Hour, Minute);
Minute = getMinute(Hour, Minute);
RTC.adjust(DateTime(1979, 05, 30, Hour, Minute, 0));
//serial print stuff
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}

void loop()
{
DateTime now = RTC.now();
int Hour = now.hour();
int Minute = now.minute();
showNumber(Hour, Minute);

Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(1000);
}

int getHour(int hour, int minute){
Serial.print(" in the getHour function ");
int Hour = hour;
int Minute = minute;
altState = digitalRead(altPin);
okState = digitalRead(okPin);

while (okState == 0){
//Serial.print(" \nin the while function ");
altState = digitalRead(altPin);
okState = digitalRead(okPin);
if (altState != lastAltState) {
if(altState == HIGH) {
if (Hour == 23) { Hour = 0; }
else { Hour++; }
showNumber(Hour, Minute);
Serial.print(" \nHour = ");
Serial.print(Hour); }
else {

// if the current state is LOW then the button went from on to off:
//Serial.println("off");
}
// Delay a little bit to avoid bouncing
lastAltState = altState;
delay(50);
}

}
lastAltState = 0;
lastOkState = 0;
okState = 0;
altState = 0;
return Hour;

}

int getMinute(int hour, int minute){
//delay(50);
Serial.print(" in the getMinute function ");
int Hour = hour;
int Minute = minute;
Serial.print(" Hour is ");
Serial.print(Hour);
Serial.print(" Minute is ");
Serial.print(Minute);
//delay(10000);
altState = digitalRead(altPin);
okState = digitalRead(okPin);
altState = 0;
okState = 0;
delay(1000);

while (okState == 0){
//Serial.print(" \nin the while function ");
altState = digitalRead(altPin);
okState = digitalRead(okPin);
if (altState != lastAltState) {
if(altState == HIGH) {
if (Minute == 59) { Minute = 0; }
else { Minute++; }
showNumber(Hour, Minute);
Serial.print(" \nMinute = ");
Serial.print(Minute);

}
else {
// if the current state is LOW then the button went from on to off:
//Serial.println("off");
}
// Delay a little bit to avoid bouncing
lastAltState = altState;
delay(30);
}

}

return Minute;

}

void showNumber(int Hour, int Minute) {
// Serial.print("\nHour is ");
// Serial.print(Hour);
// Serial.print("\nMinute is ");
// Serial.print(Minute);
int a, b, c, d;

//arranging the Hour params for display
if (Hour == 0) { a = 0; b = Hour; }
else if (Hour < 10) {
a = 18;
b = Hour;
// Serial.print(" \nHour a = ");
// Serial.print(LED_SEG_TAB[a]);
}
else if (Hour >= 10) {
char * arr = convertNumberIntoArray(Hour);
b = arr[0]; a = arr[1];
}

//arranging the Minute params for display
if (Minute == 0) { c = 0; d = Minute; }
else if (Minute < 10) { c = 0; d = Minute; }
else if (Minute >= 10) {
char * arr1 = convertNumberIntoArray(Minute);
d = arr1[0]; c = arr1[1];
}
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[a]);
shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB**); **
** shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB**
```c
**); 
  shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[d]); 
  digitalWrite(latchPin, HIGH); 
  delay(200);
}

char * convertNumberIntoArray(unsigned int number) {
    int length = (int)floor(log10((float)number)) + 1;
    char * arr = new char[length];
    int i = 0;
    do {
        arr[i] = number % 10;
//        Serial.print(" \nthis is arr[] ");
//        Serial.print(arr[i]);
        number /= 10;
        i++;
    } while (number != 0);
    return arr;
}**
```

Oops..

char * arr = new char[length];
...
arr = number % 10;

Oh, and the allocated memory is never released..

Thanks for your replies and feedback, I appreciate it. Will definitely use the code tags next time.

Do i need to release the allocated memory in each loop? Is it done from the loop() function?

Thanks again, melquiades

You should not allocate memory during each loop. Use a global buffer with enough space for the numbers. The max. length of an unsigned int is 5 characters:

char numberBuffer[6]; //Null terminated

...

unsigned int theNumber = 1234;
ltoa(theNumber, numberBuffer, 10); //use itoa for int's < 32,767
Serial.println(numberBuffer);

Thanks a lot for your replies I got it fixed!
I never wanted to use dynamic allocation anyway, I guess it just slipped in a copy paste from somewhere.

cheers, melquiades