arduino uno si blocca

Salve a tutti,
sto facendo un sketch, che nelle intenzioni, dovrebbe permettere la nascita di pulcini.
Quindi ci sono 2 relays, 1 rtc, 1 sensore di temperatura, 1 pulsante.
Stringendo questo sketch fa:

  • legge la temperatura e accende o spegne la resistenza in base ad essa
  • ogni ora gira le uova

Purtroppo dopo qualche ora si blocca e non da segni di vita finchè non si resetta.
Questo è il codice:

#include <Wire.h>
#include "RTClib.h"


const int LM35_PIN = 1;// Numero del pin analogico sul quale è collegato l'LM35 
const float vref = 1.1;// Vref dell'ADC (quell'interno è di 1,1V)
const int  relayEggsRotatioPin = 2;
const int  relayTemperaturePin = 3;
const int  BUTTON_ROTATION_EGG_PIN = 7;

//attiva il debug
boolean blnDebug=true;


//date time
RTC_DS1307 RTC;
DateTime now;
int countDelayPrintDate;
int countDelayPrintTemperature;
boolean blnScheduleCommand=false;

//Temperature
float tempStart=37.7;
float tempEnd=38;
int sample_count;

//eggs rotation
int eggsRotation=0;


//void(* resetFunc) (void) = 0;//declare reset function at address 0

void setup()
{
  Serial.begin(9600); // start serial communication

  //define pin 7 input 
  pinMode(BUTTON_ROTATION_EGG_PIN, INPUT);


  //define i pin 2 3  out
  pinMode(relayEggsRotatioPin, OUTPUT);
  pinMode(relayTemperaturePin, OUTPUT);

  //beginning i le uscite
  digitalWrite(relayEggsRotatioPin, LOW);
  digitalWrite(relayTemperaturePin, LOW);

  // Start rtc
  Wire.begin();
  RTC.begin();
  //RTC.adjust(DateTime(__DATE__, __TIME__));

  //read temperature
  analogReference(INTERNAL);


  debug("End setup");
}


void loop()
{
  printDate();
  checkTemperature();
  scheduleCommand();
  eggs();
}

void eggs(){
  eggsRotation=digitalRead(BUTTON_ROTATION_EGG_PIN);

  if (eggsRotation == HIGH) {  
    //    debug("eggs rotation on");    
    digitalWrite(relayEggsRotatioPin, HIGH);
  }
  else if (eggsRotation == LOW && !blnScheduleCommand)  {
    //  debug("eggs rotation off");    
    digitalWrite(relayEggsRotatioPin, LOW);
  }
}


/* Legge la temperatura */
float readTemp()
{
  float temp = 0.0;       // valore convertito in temperatura (°C)
  int val = 0;            // valore quantizzato dall'ADC [0..1023]
  int nread = 8;          // numero di letture (da 5 a 8)
  float somma = 0.0;      // somma delle letture
  for (int i=0; i<nread; i++)
  {
    val = analogRead(LM35_PIN);                // legge il dato della tensione sul pin 'LM35_pin' 
    temp = (100.0 *  vref * val)/1024.0;       // lo converte in °C
    somma += temp;                             // aggiunge alla somma delle temperature lette   
  }   
  return ( somma / nread );                    // ne calcola il valore medio 
}

void checkTemperature(){

  float temperature=readTemp();
  
  countDelayPrintTemperature++;
  if(countDelayPrintTemperature>600){
    Serial.print(temperature,2);
    Serial.println(" C");

  }
  
  if(temperature<tempStart){
    //on
    digitalWrite(relayTemperaturePin, HIGH);
    if(countDelayPrintTemperature>600){
    debug("resistence on");

  }
    
  }
  if(temperature>tempEnd){
    //off
    digitalWrite(relayTemperaturePin, LOW);
    if(countDelayPrintTemperature>600){
       debug("resistence off");

  }

  }
  
   if(countDelayPrintTemperature>600){
    countDelayPrintTemperature=0;
  }

}






void debug(String s){
  if(blnDebug)
    Serial.println(s);
}

void printDate(){
  now = RTC.now();
  if(blnDebug){
    countDelayPrintDate++;
    if(countDelayPrintDate>600){
      //    DateTime now = RTC.now();
      //We print the day
      Serial.print(now.day(), DEC);
      Serial.print('/');
      //We print the month
      Serial.print(now.month(), DEC);
      Serial.print('/');
      //We print the year
      Serial.print(now.year(), DEC);
      Serial.print(' ');
      //We print the hour
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      //We print the minutes
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      //We print the seconds
      Serial.print(now.second(), DEC);
      debug("");
      countDelayPrintDate=0;
    }
  }

}

void scheduleCommand(){

  //Faccio il reset ogni ora

  /*  if(now.minute()==1){
   debug("Every 1 minute.");
   debug("reset");
   resetFunc();
   debug("fine reset");
   }
   
   */


  if(now.minute()==30 && now.second()==0 && !blnScheduleCommand){
    blnScheduleCommand=true;
    digitalWrite(relayEggsRotatioPin, HIGH);
    debug("Every 1 hour turn eggs.");
  }

  if(now.minute()==30 && now.second()==7 && blnScheduleCommand){
    blnScheduleCommand=false;
    digitalWrite(relayEggsRotatioPin, LOW);
    debug("Stop turn eggs.");
  }



  /*if(now.hour()>=r1HourStart && now.hour()<r1HourEnd){
   if(!blnScheduleCommand){
   blnScheduleCommand=true;
   processCommand(CONST_R1_ON);
   debug("Start "+ String(r1HourStart) + " hour.");
   }
   }
   else{  
   if(blnScheduleCommand){
   blnScheduleCommand=false;
   processCommand(CONST_R1_OFF);
   debug("Stop "+String(r1HourEnd)+" hour.");
   }
   }
   
   */

}

Ho pensato che si poteva saturare la memoria in qualche modo ma non vedo come.
Comunque sto rinunciando all'idea di usare arduino e sto comprando un termostato elettronico.

Purtroppo ho adsl che non funziona da 3 settimane a causa di un fulmine quindi non sarò spesso online.

Grazie

Prima di tutto ti suggerisco di levarti dal codice qualsiasi uso delle String (la classe String).
La tua debug() usa le String ma la memoria di Arduino Uno è solo di 2Kb di SRAM e quella classe fa allocazione dinamica e a volte (in certe situazioni, non sempre) disalloca "male" la SRAM e alla fine la esaurisce.

Ho fatto alcune prove,
la funzione che usava la stringa l'ho trasformata in char[], ma non credo che il problema era quello. Credo che il problema stava nella lettura continua della sonda lm35, secondo voi è plausibile?
Adesso leggo 30 volte la sonda ogni 2 secondi e sembra che non si blocca più sono 3 giorni che è in funzione.

#include <Wire.h>
#include "RTClib.h"

const int SENSITIVITY = 4096;
const int LM35_PIN = 1;// Numero del pin analogico sul quale è collegato l'LM35 
const float vref = 1.1;// Vref dell'ADC (quell'interno è di 1,1V)
const int  relayEggsRotatioPin = 2;
const int  relayTemperaturePin = 3;
const int  BUTTON_ROTATION_EGG_PIN = 7;
const long TEMPARATURE_CHECK_INTERVAL = 2000;
//attiva il debug
boolean blnDebug=true;


//date time
RTC_DS1307 RTC;
DateTime now;
int countDelayPrintTemperature;
boolean blnScheduleCommand=false;
long now2; 

//Temperature
float tempStart=37.4;
float tempEnd=37.6;
//int sample_count;

//eggs rotation
int eggsRotation=0;


//void(* resetFunc) (void) = 0;//declare reset function at address 0

void setup()
{
  Serial.begin(9600); // start serial communication

  //define pin 7 input 
  pinMode(BUTTON_ROTATION_EGG_PIN, INPUT);


  //define i pin 2 3  out
  pinMode(relayEggsRotatioPin, OUTPUT);
  pinMode(relayTemperaturePin, OUTPUT);

  //beginning i le uscite
  digitalWrite(relayEggsRotatioPin, LOW);
  digitalWrite(relayTemperaturePin, LOW);

  // Start rtc
  Wire.begin();
  RTC.begin();
  //RTC.adjust(DateTime(__DATE__, __TIME__));

  //read temperature
  analogReference(INTERNAL);
  debug("End setup");
}


void loop()
{

  if (millis() - now2 > TEMPARATURE_CHECK_INTERVAL) {
  printDate();
  checkTemperature();
    now2 = millis();
  }

  scheduleCommand();
  eggs();
//  delay(2000);

}

void eggs(){
  eggsRotation=digitalRead(BUTTON_ROTATION_EGG_PIN);

  if (eggsRotation == HIGH) {  
    //    debug("eggs rotation on");    
    digitalWrite(relayEggsRotatioPin, HIGH);
  }
  else if (eggsRotation == LOW && !blnScheduleCommand)  {
    //  debug("eggs rotation off");    
    digitalWrite(relayEggsRotatioPin, LOW);
  }
}


/* Legge la temperatura */
float readTemp()
{
  
  float temp = 0.0;       // valore convertito in temperatura (°C)
  int val = 0;            // valore quantizzato dall'ADC [0..1023]
  int nread = 30;          // numero di letture (da 5 a 8)
  float somma = 0.0;      // somma delle letture

  for (int i=0; i<nread; i++){
    val = analogRead(LM35_PIN);                // legge il dato della tensione sul pin 'LM35_pin' 
    temp = (100.0 *  vref * val)/1024.0;       // lo converte in °C
    somma += temp;                             // aggiunge alla somma delle temperature lette   
  }   
  return ( somma / nread );                    // ne calcola il valore medio 
}

void checkTemperature(){

  // Leggo la temperatura
    float temperature=readTemp();
  if(blnDebug){
    Serial.print(temperature,2);
    Serial.println(" C");
  }

    if(temperature<tempStart){
      //on
      digitalWrite(relayTemperaturePin, HIGH);
      //debug("resistence on");
    }

    if(temperature>tempEnd){
      //off
      digitalWrite(relayTemperaturePin, LOW);
      //debug("resistence off");
    }

}


void debug(char s[]){
 if(blnDebug)
 Serial.println(s);
 }

void printDate(){
  now = RTC.now();
  if(blnDebug){
      //We print the day
      Serial.print(now.day(), DEC);
      Serial.print('/');
      //We print the month
      Serial.print(now.month(), DEC);
      Serial.print('/');
      //We print the year
      Serial.print(now.year(), DEC);
      Serial.print(' ');
      //We print the hour
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      //We print the minutes
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      //We print the seconds
      Serial.print(now.second(), DEC);
      Serial.println();
  }

}

void scheduleCommand(){

  //Faccio il reset ogni ora

  /*  if(now.minute()==1){
   debug("Every 1 minute.");
   debug("reset");
   resetFunc();
   debug("fine reset");
   }   
   */


  if(now.minute()==30 && now.second()>=0 && !blnScheduleCommand){
    blnScheduleCommand=true;
    digitalWrite(relayEggsRotatioPin, HIGH);
    //    debug("Every 1 hour turn eggs.");
  }

  if(now.minute()==30 && now.second()>=7 && blnScheduleCommand){
    blnScheduleCommand=false;
    digitalWrite(relayEggsRotatioPin, LOW);
    //  debug("Stop turn eggs.");
  }



  /*if(now.hour()>=r1HourStart && now.hour()<r1HourEnd){
   if(!blnScheduleCommand){
   blnScheduleCommand=true;
   processCommand(CONST_R1_ON);
   debug("Start "+ String(r1HourStart) + " hour.");
   }
   }
   else{  
   if(blnScheduleCommand){
   blnScheduleCommand=false;
   processCommand(CONST_R1_OFF);
   debug("Stop "+String(r1HourEnd)+" hour.");
   }
   }
   
   */

}

Che ne pensate?

Grazie

Potrebbe essere. Non sono un espertone di letture analogiche, però tieni conto che il convertitore analogico-digitale di Arduino richiede dei tempi per effettuare la conversione.
La tua funzione read_temp() legge velocemente una serie di volte, prova a cercare sul forum o su internet se non è il caso di mettere una piccola delay() tra una lettura e l'altra (nel for quindi).

now2 DEVE essere un unsigned long e non un long. E' normale si blocchi.
Metti anche TEMPARATURE_CHECK_INTERVAL come const unsigned long.

LM35_PIN segnalo come A1 e non come 1. E' più leggibile ed evita di confondersi con pin 1 digitale.