[SOLVED] Freeze after 20-90 minutes when using serial communication

/****************************
 ** POWER IMPULSE COUNTING **
 ** (interrupt function)   **
 ****************************/
void powerImpluseCounting()
{
  // Retrieve signal voltage applied to
  // impulse input (maximum value is 1024
  // which means that whole reference
  // voltage (5 volts) is applied)
  int impulseVoltage = analogRead(0);
  // If an impulse comes in (voltage applied
  // to input is higher than about 80% of
  // reference voltage; software threshold)
  if(impulseVoltage > 800)
  {
    // Set impulse occurence flag
    // ("software debouncing")
    bImpulseOccurred = true;
  }
}


/************************************************
 ** BACKUP COUNTED IMPULSES OF HOUR TO SD CARD **
 ***********************************************/
void backupPowerImplusesOfHour()
{
  // Write last hour impulse count to SD card
  // (prevent data loss in case of board reset)
  char filenameImpulseCounterHour[] = "POWER/IMPLASTH.TXT";
  // Define data buffer for formatting impulse count
  char aFormatConversionBuffer[4];
  
  // Open file if SD card is initalized
  // and write current hour impulse count to it
  if (bSDCardInitialized)
  {
    // Set green LED on
    digitalWrite(3, HIGH);
    
    File impulsesLastHour = SD.open(filenameImpulseCounterHour, FILE_WRITE);
    // Seek to start of file in order
    // to overwrite all data in it
    impulsesLastHour.seek(0);
    // Write formatted string (four characters) to file
    // making sure to fully overwrite any exiting number)
    impulsesLastHour.print(dtostrf(fPowerImpulsesDuringThisHour, 4, 0, aFormatConversionBuffer));
    impulsesLastHour.close();
    
    // Set green LED off
    digitalWrite(3, LOW);
  }
}


/***********************************
 ** LOG POWER IMPLUSES OF AN HOUR **
 **********************************/
void logPowerImplusesOfHour()
{
  // Output impluses counted within last
  // hour including date to serial monitor
  // (not required for regular operation)
  /*
  Serial.print("Impulses during last hour: ");
  Serial.print(fPowerImpulsesDuringThisHour);
  Serial.print("  --  Log timestamp: ");
  Serial.print(oLogTimestamp.year(), DEC);
  Serial.print("/");
  Serial.print(oLogTimestamp.month(), DEC);
  Serial.print("/");
  Serial.print(oLogTimestamp.day(), DEC);
  Serial.print(" ");
  Serial.print(oLogTimestamp.hour(), DEC);
  Serial.print(":");
  Serial.print(oLogTimestamp.minute(), DEC);
  Serial.print(":");
  Serial.println(oLogTimestamp.second(), DEC);
  */
  
  // Generate filename dynamically so
  // each month a new file gets created
  // (e.g. log of May 2013 = LOG13_05.CSV)
  char filenameLogMonth[] = "POWER/LOG00_00.CSV";
  filenameLogMonth[9] = (oLogTimestamp.year() - 2000)/10 + '0';
  filenameLogMonth[10] = oLogTimestamp.year()%10 + '0';
  filenameLogMonth[12] = oLogTimestamp.month()/10 + '0';
  filenameLogMonth[13] = oLogTimestamp.month()%10 + '0';
  
  // Open file if SD card is initalized
  // and write date/time as well as kWh counted
  // during last hour to it
  if (bSDCardInitialized)
  {
    // Set green LED on
    digitalWrite(3, HIGH);
    
    File logfileHours = SD.open(filenameLogMonth, FILE_WRITE);
    
    logfileHours.print(oLogTimestamp.year(), DEC);
    logfileHours.print("/");
    logfileHours.print(oLogTimestamp.month(), DEC);
    logfileHours.print("/");
    logfileHours.print(oLogTimestamp.day(), DEC);
    logfileHours.print(" ");
    logfileHours.print(oLogTimestamp.hour(), DEC);
    logfileHours.print(":");
    logfileHours.print(oLogTimestamp.minute(), DEC);
    logfileHours.print(":");
    logfileHours.print(oLogTimestamp.second(), DEC);
    logfileHours.print(", ");
    logfileHours.print(fPowerImpulsesDuringThisHour/1000, 3);
    // Finish dataset with newline
    // command and close the file
    // (writes buffer to SD card)
    logfileHours.println();
    logfileHours.close();
    
    // Add impulses of last hour
    // to day impulse counter
    fPowerImpulsesDuringThisDay = fPowerImpulsesDuringThisDay + fPowerImpulsesDuringThisHour;
    // Reset impulses of current hour counter
    // (to start counting impulses of new hour)
    fPowerImpulsesDuringThisHour = 0;
    // Backup current hour counter with value 0
    // (in order to clear cumulated value set by interrupts)
    backupPowerImplusesOfHour();
    
    // Set green LED off
    digitalWrite(3, LOW);
  }
  // Backup current value of current day counter
  // (in order to restore its value during setup after a board reset)
  backupPowerImpulsesOfDay();
}


/***********************************************
 ** BACKUP COUNTED IMPULSES OF DAY TO SD CARD **
 **********************************************/
void backupPowerImpulsesOfDay()
{
  // Write last day impulse count to SD card
  // (prevent data loss in case of board reset)
  char filenameImpulseCounterDay[] = "POWER/IMPLASTD.TXT";
  // Define data buffer for formatting impulse count
  char aFormatConversionBuffer[4];
  
  // Open file if SD card is initalized
  // and write current day impulse count to it
  if (bSDCardInitialized)
  {
    // Set green LED on
    digitalWrite(3, HIGH);
    
    File impulsesLastDay = SD.open(filenameImpulseCounterDay, FILE_WRITE);
    // Seek to start of file in order
    // to overwrite all data in it
    impulsesLastDay.seek(0);
    // Write formatted string (four characters) to file
    // making sure to fully overwrite any exiting number)
    impulsesLastDay.print(dtostrf(fPowerImpulsesDuringThisDay, 4, 0, aFormatConversionBuffer));
    impulsesLastDay.close();
    
    // Set green LED off
    digitalWrite(3, LOW);
  }
}


/*********************************
 ** LOG POWER IMPLUSES OF A DAY **
 ********************************/
void logPowerImplusesOfDay()
{
  // Output impluses counted within last
  // day including date to serial monitor
  // (not required for regular operation)
  /*
  Serial.print("Impulses during last day: ");
  Serial.print(fPowerImpulsesDuringThisDay);
  Serial.print("  --  Log timestamp: ");
  Serial.print(oLogTimestamp.year(), DEC);
  Serial.print("/");
  Serial.print(oLogTimestamp.month(), DEC);
  Serial.print("/");
  Serial.println(oLogTimestamp.day(), DEC);
  */
  
  // Generate filename dynamically
  // so each year a new file gets
  // created
  // (e.g. log of 2013 = ARCHIVE3.CSV)
  char filenamePowerLogYear[] = "POWER/ARCHIVE0.CSV";
  filenamePowerLogYear[13] = oLogTimestamp.year()%10 + '0';
  
  // Open file if SD card is initalized
  // and write date as well as kWh counted
  // during last day to it
  if (bSDCardInitialized)
  {
    // Set green LED on
    digitalWrite(3, HIGH);
    
    File logfileDays = SD.open(filenamePowerLogYear, FILE_WRITE);
    
    logfileDays.print(oLogTimestamp.year(), DEC);
    logfileDays.print("/");
    logfileDays.print(oLogTimestamp.month(), DEC);
    logfileDays.print("/");
    logfileDays.print(oLogTimestamp.day(), DEC);
    logfileDays.print(", ");
    logfileDays.print(fPowerImpulsesDuringThisDay/1000, 3);
    // Finish dataset with newline
    // command and close the file
    // (writes buffer to SD card)
    logfileDays.println();
    logfileDays.close();
    
    // Reset impulses of current day counter
    // (to start counting impulses of new day)
    fPowerImpulsesDuringThisDay = 0;
    // Backup current day counter with value 0
    // (in order to clear cumulated value set by hourly LOGGING routine)
    backupPowerImpulsesOfDay();
    
    // Set green LED off
    digitalWrite(3, LOW);
  }
}