Rileggere variabili dalla ultima riga da una SD (Arduino MEga) [RISOLTO]

allego il mio sketch (confusionario)
se riesci a carpirne il problema te ne sarei grato.

int result;
int buttonPin = 22;
int buttonPin1 = 24;

int giri = 0;

#include <Wire.h>
#include "RTClib.h"
#include <SD.h>
#include <SPI.h>
#define DEBUG_RIGHE_FILE 1 //0=non legge ... 1=legge
int button = 8;
int start;
int stop_;
int tempo;
RTC_DS1307 RTC;
int flag = 0;
int anno;
int mese;
int giorno;
int ora;
int minuti;
int secondi;
int data;
long contatore_alto =113165;
long contatore_basso = 112972;
int flag_contatore;
 int kw_giorno;
 int giri_kw;
File contatore;
String ReadLine;
int riavvio = 0;
char charBuf[80];
byte err_sd=1;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;



void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
    Wire.begin();
    RTC.begin();
    pinMode (button,INPUT);
  pinMode(buttonPin,OUTPUT);
   pinMode(buttonPin1,OUTPUT);
     pinMode(53, OUTPUT);
  leggi_SD(); //vai alla funzione leggi l'ultima riga da sd   
   
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
   // RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  sd_init(); 
  leggi_SD();
}

void sd_init() 
{   
   if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
}

void leggi_SD(){
  String ReadLine;
  char charBuf[50];
contatore = SD.open("valori2.txt",FILE_READ);
    
if(contatore) {
   int16_t c;
       while ((c = contatore.read())>0){
         ReadLine += (char)c;
           if((char)c=='\n') {                 

                 #if DEBUG_RIGHE_FILE               
                 Serial.print(ReadLine);//debug 
                 #endif

               if(c > 0) ReadLine="";   
            }  
       }  
   } 
  Serial.print("ultima linea ");//debug
  Serial.println(ReadLine);//debug  //questa è l'ultima linea
  ReadLine.toCharArray(charBuf, 50); //metto i singoli caratteri in un array di char
  estrai_valori(charBuf);//vai alla funzione estrai dati
}


void estrai_valori(char *charBuffer){ 
 unsigned long temp[]= {0,0,0,0,0,0,0,0,0};
  char *p = charBuffer;  
  byte i = 0;

  while (*p != '\0') 
  {
    if (*p == ',') { ++i;  ++p;  continue;}
    if (isdigit(*p)) 
    {
         temp[i] *= 10; 
         temp[i] += (*p - '0');            
    }    
     ++p;
  }  

Serial.println(temp[0]);Serial.println(temp[1]);Serial.println(temp[2]);Serial.println(temp[3]);Serial.println(temp[4]);
Serial.println(temp[5]);Serial.println(temp[6]);Serial.println(temp[7]);Serial.println(temp[8]);

contatore.close();
}




void loop() {
 
  DateTime now = RTC.now();
    
    anno = (now.year());
    mese = (now.month());
    giorno =(now.day());
    ora = (now.hour());
    minuti = (now.minute());
    secondi = (now.second());
  
     if (ora > 7 && ora < 21) { flag_contatore = 0;} else {flag_contatore = 1;}
     if (ora == 0 && minuti == 0 && secondi < 20) {giri_kw = 0; kw_giorno = 0;}
  
  
    if (flag == 0) {
   start = (now.unixtime());
   //Serial.println(start);
   }
         flag =1 ;
   
   

 
  // Se arriva impulso tramite Xbee allora si comincia
  if (Serial.available()) {
    int inByte = Serial.read();
  // Serial1.println( Serial.write(inByte)); 
  
   
   if (inByte == 'I') {      
      giri = giri + 1;
      kw_giorno = kw_giorno + 8.333333;
   
     if (giri == 120) {
       
       if (flag_contatore == 0) {contatore_alto = contatore_alto + 1;}
       if (flag_contatore == 1) {contatore_basso = contatore_basso + 1;}
       giri = 0;}
       
     
     
      tempo = now.unixtime() - start ;
      flag = 0; 
    digitalWrite (buttonPin,HIGH);
    delay(150);
    float z = tempo/3600.000000000;
    float  consumo_mom = 8.333333 / z;
  
  
  // Stampa su monitor
    Serial.print (" secondi passati --> ");
    Serial.print (tempo);
    Serial.print (" consumo attuale --> ");
    Serial.print (consumo_mom);
    Serial.print (" Giri disco al momento --> ");
    Serial.println (giri);
    Serial.println ("****************************************************************");
    Serial.print (" Contatore tariffa alta --> ");
    Serial.print (contatore_alto);
    Serial.print (" Contatore tariffa bassa --> ");
    Serial.print (contatore_basso);
     Serial.print (" Consumo Giornaliero in w/h --> ");
    Serial.println (kw_giorno);
    Serial.print (" DATA: ");
    Serial.print (giorno);
    Serial.print (".");
    Serial.print (mese);
    Serial.print (".");
    Serial.print (anno);
    Serial.print ("   ORA:");
    Serial.print (ora);
    Serial.print (":");
    Serial.print (minuti);
    Serial.print (":");
    Serial.print (secondi);
    Serial.println ();
    Serial.println ();
    Serial.println ();
    
    
//Scrittura su SD  
contatore = SD.open("valori2.txt", FILE_WRITE);
      // if the file opened okay, write to it:
  if(contatore) {
    Serial.print("scrivo valori...");
    delay(250);
      contatore.print(giorno);
     contatore.print(",");
     contatore.print(mese);
      contatore.print(",");
       contatore.print(anno);
         contatore.print(",");
       
         contatore.print(ora);
          contatore.print(",");
           contatore.print(minuti);
         //   contatore.print(",");
         //    contatore.print(secondi);
            
              contatore.print(",");
              
              //  contatore.print(consumo_mom,0);
                
              //   contatore.print(",");
                   contatore.print(kw_giorno);
                  
                    contatore.print(",");
                     contatore.print(contatore_alto);
                    
                       contatore.print(",");
                        contatore.print(contatore_basso);
                        
                          contatore.print(",");
                           contatore.print(giri);
                            
                            contatore.print("\n");
                           
                         
                          
	// close the file:
  contatore.close();
    Serial.println("done.");

  } else {
    // if the file didn't open, print an error:
    Serial.println("apertura file fallita...");
  }
    
    
}

   

  digitalWrite (buttonPin,LOW);
 

  









  // read from port 1, send to port 0:
 // if (Serial1.available()) {
   // int inByte = Serial1.read();
  }
}