Seriale sparisce dopo 2 minuti

Ciao, ho un problema con una MEGA ADK e l'ETHERNET SHIELD. Devo controllare con questa scheda un ventola, un led e un servo motore via internet. Per i primi 2 minuti sembra funzionare tutto correttamente. Dopo si blocca e non riceve piu nulla e l'IDE non riconosce piu la seriale attaccata. Posto il codice perchè magari mi sono dimenticato qualcosa.

void setup(){
   
  Serial.begin( 9600 );
  Serial.println("Start");
  

  //CONFIGURAZIONE ETHERNET
  Serial.println("...Configuro la connessione ethernet...");
  Ethernet.begin(mac);  //configuro lo shield ethernet
  Serial.println("Connessione Configurata.");
  delay(1000);//aspetto un secondo per far avviare lo shield ethernet
  Serial.println("Programma Avviato, Setup Terminato!");
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
  server.begin();
  client.flush();
  // initialize the FAN pin as an output:
  pinMode(fanPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinFan, INPUT); 
  
  // initialize the LED pin as an output:
  pinMode(LedPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinLed, INPUT);

  myservo.attach(3);  // attaches the servo on pin 3 to the servo object 
  delay(2000);
}

void loop(){
  time = millis();

  setServo();
  setFan();
  setLed();
  
  client = server.available();
  if (client)
  {
    Serial.println("Request incoming..");
    WaitForRequest(client);
    ParseReceivedRequest();
    PerformRequestedCommands();
    Serial.println("Richiesta servita");
    delay(100);
    client.flush();
    client.stop();
    Serial.flush();
    delay(100);
  }  

  delay(50);
  Serial.flush();  
}

//CONTROLLO IL POTENZIOMETRO E SETTO IL SERVOMOTORE
void setServo(){
  valServo = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  valServo = map(valServo, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(valServo);                  // sets the servo position according to the scaled value 
  delay(15);
}

//CON IL BOTTONE VADO AD AZIONE LE VENTOLE
void setFan(){
  valFan = digitalRead(buttonPinFan);
  //check if input is HIGH

  if ((valFan == HIGH) && (old_val_Fan == LOW)) {
    buttonFan = 1 - buttonFan;
    delay(50);
  }

  old_val_Fan = valFan;

  if (buttonFan == 1) {     
    // turn LED on:    
    digitalWrite(fanPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(fanPin, LOW); 
  }
}

//CON IL BOTTONE VADO AD AZIONARE IL LED
void setLed(){
  valLed = digitalRead(buttonPinLed);
  //check if input is HIGH
 
  if ((valLed == HIGH) && (old_val_Led == LOW)) {
    buttonLed = 1 - buttonLed;    
    delay(50);
  }

  old_val_Led = valLed;

  if (buttonLed == 1) {     
    // turn LED on:    
    digitalWrite(LedPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LedPin, LOW); 
  }
}

//-- Commands and parameters (sent by browser) --
char cmd[15];    // Nothing magical about 15, increase these if you need longer commands/parameters
char param1[15];
char param2[15];


void WaitForRequest(EthernetClient client) // Sets buffer[] and bufferSize
{
  bufferSize = 0;

  while (client.connected()) {
    if (client.available()) {
	char c = client.read();
	if (c == '\n')
	  break;
	else
	  if (bufferSize < bufferMax)
	    buffer[bufferSize++] = c;
	  else
	    break;
    }
  }

  PrintNumber("bufferSize", bufferSize);
}

void ParseReceivedRequest()
{
  Serial.println("in ParseReceivedRequest");
  Serial.println(buffer);

  //Received buffer contains "GET /cmd/param1/param2 HTTP/1.1".  Break it up.
  char* slash1;
  char* slash2;
  char* slash3;
  char* space2;

  slash1 = strstr(buffer, "/") + 1; // Look for first slash
  slash2 = strstr(slash1, "/") + 1; // second slash
  slash3 = strstr(slash2, "/") + 1; // third slash
  space2 = strstr(slash2, " ") + 1; // space after second slash (in case there is no third slash)
  if (slash3 > space2) slash3=slash2;

  PrintString("slash1",slash1);
  PrintString("slash2",slash2);
  PrintString("slash3",slash3);
  PrintString("space2",space2);

  // strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
  cmd[0] = 0;
  param1[0] = 0;
  param2[0] = 0;
  strncat(cmd, slash1, slash2-slash1-1);
  strncat(param1, slash2, slash3-slash2-1);
  strncat(param2, slash3, space2-slash3-1);

  PrintString("cmd",cmd);
  PrintString("param1",param1);
  PrintString("param2",param2);
}

void PerformRequestedCommands()
{
  if ( strcmp(cmd,"digitalWrite") == 0 ) RemoteDigitalWrite();
  if ( strcmp(cmd,"digitalRead") == 0 ) RemoteDigitalRead();
  if ( strcmp(cmd,"analogRead") == 0 ) RemoteAnalogRead();
  if ( strcmp(cmd,"analogWrite") == 0 ) RemoteAnalogWrite();
}

void RemoteDigitalWrite()
{
  int ledPin = param1[0] - '0'; // Param1 should be one digit port
  int ledState = param2[0] - '0'; // Param2 should be either 1 or 0
  digitalWrite(ledPin, ledState);
  
  if(ledPin==fanPin) buttonFan = 1 - buttonFan;
  if(ledPin==LedPin) buttonLed = 1 - buttonLed;
  
  //-- Send response back to browser --
  server.print("D");
  server.print(ledPin, DEC);
  server.print(" is ");
  server.print( (ledState==1) ? "ON" : "off" );

  //-- Send debug message to serial port --
  Serial.println("RemoteDigitalWrite");
  PrintNumber("ledPin", ledPin);
  PrintNumber("ledState", ledState);
}
void RemoteDigitalRead()
{
  int ledPin = param1[0] - '0'; // Param1 should be one digit port
  int ledState; // Param2 should be either 1 or 0
  ledState = digitalRead(ledPin);

  //-- Send response back to browser --
  server.print("D");
  server.print(ledPin, DEC);
  server.print(" is ");
  server.print( (ledState==1) ? "ON" : "off" );

  //-- Send debug message to serial port --
  Serial.println("RemoteDigitalRead");
  PrintNumber("ledPin", ledPin);
  PrintNumber("ledState", ledState);
}

void RemoteAnalogRead()
{
  // If desired, use more server.print() to send http header instead of just sending the analog value.
  int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
  int analogValue = analogRead(analogPin);

  //-- Send response back to browser --
  server.print("A");
  server.print(analogPin, DEC);
  server.print(" is ");
  server.print(analogValue,DEC);

  //-- Send debug message to serial port --
  Serial.println("RemoteAnalogRead");
  PrintNumber("analogPin", analogPin);
  PrintNumber("analogValue", analogValue);
}

void RemoteAnalogWrite()
{
  // If desired, use more server.print() to send http header instead of just sending the analog value.
  int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
  int analogValue = param2[0] - '0';
  
  if(analogPin!=3) analogWrite(analogPin, analogValue);
  else{     
    myservo.write((analogValue*20)-1);
    delay(15);
  }
  
  //-- Send response back to browser --
  server.print("A");
  server.print(analogPin, DEC);
  server.print(" is ");
  server.print(analogValue,DEC);

  //-- Send debug message to serial port --
  Serial.println("RemoteAnalogWrite");
  PrintNumber("analogPin", analogPin);
  PrintNumber("analogValue", analogValue);
}

void PrintString(char* label, char* str)
{
  Serial.print(label);
  Serial.print("=");
  Serial.println(str);
}

void PrintNumber(char* label, int number)
{
  Serial.print(label);
  Serial.print("=");
  Serial.println(number, DEC);
}

mancio:
magari mi sono dimenticato qualcosa

Si, ti sei dimenticato di postare l'inizializzazione delle variabili ed il codice così com'è non compila.

si scusate

//DEFINIZIONE LIBRERIE
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 

//DEFINIZIONE MACRO PER IL RESET DELLA SCHEDA
#include <avr/io.h>
#include <avr/wdt.h>
#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {}

//CONFIGURAZIONE PER L'ETHERNET
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x1D, 0x93 };
//byte ip[] = { 192,168,1,10 };

#define bufferMax 128
int bufferSize;
char buffer[bufferMax];
char bufferRisp[256];

EthernetClient client;//dichiaro una variabile globale per il client ethernet
EthernetServer server(80); // Port 80 is http.

unsigned long time = 0;

char valBuffer[8];
char timestamp[8];

char jsonMsg[256];

int sizeRisp = 0;
int richiedorisposta = 0;
int i;

//ATTUATORI
Servo myservo;  // create servo object to control a servo 

int potpin = A10;  // analog pin used to connect the potentiometer
int valServo;    // variable to read the value from the analog pin 

const int buttonPinFan = 7;     // the number of the pushbutton pin
const int fanPin =  8;      // the number of the LED pin

int valFan=0; //val will be used to store state of pin 
int old_val_Fan=0;

int buttonFan = 0;

const int buttonPinLed = 5;     // the number of the pushbutton pin
const int LedPin =  6;      // the number of the LED pin

int valLed=0; //val will be used to store state of pin 
int old_val_Led=0;

int buttonLed = 0;



void setup(){
  //CONFIGURAZIONE USB HOST
  Serial.begin( 9600 );
  Serial.println("Start");
  
  //CONFIGURAZIONE ETHERNET
  Serial.println("...Configuro la connessione ethernet...");
  Ethernet.begin(mac);  //configuro lo shield ethernet
  Serial.println("Connessione Configurata.");
  delay(1000);//aspetto un secondo per far avviare lo shield ethernet
  Serial.println("Programma Avviato, Setup Terminato!");
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
  server.begin();
  client.flush();
  // initialize the LED pin as an output:
  pinMode(fanPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinFan, INPUT); 
  
  // initialize the LED pin as an output:
  pinMode(LedPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinLed, INPUT);

  myservo.attach(3);  // attaches the servo on pin 33 to the servo object 
  delay(2000);
}

void loop(){
  time = millis();

  setServo();
  setFan();
  setLed();
  
  client = server.available();
  if (client)
  {
    Serial.println("Request incoming..");
    WaitForRequest(client);
    ParseReceivedRequest();
    PerformRequestedCommands();
    Serial.println("Richiesta servita");
    delay(100);
    client.flush();
    client.stop();
    Serial.flush();
    delay(100);
  }

  delay(100);
  Serial.flush();
  delay(100);  
}

//CONTROLLO IL POTENZIOMETRO E SETTO IL SERVOMOTORE
void setServo(){
  valServo = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  valServo = map(valServo, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(valServo);                  // sets the servo position according to the scaled value 
  delay(15);
}

//CON IL BOTTONE VADO AD AZIONE LE VENTOLE
void setFan(){
  valFan = digitalRead(buttonPinFan);
  //check if input is HIGH

  if ((valFan == HIGH) && (old_val_Fan == LOW)) {
    buttonFan = 1 - buttonFan;
    delay(50);
  }

  old_val_Fan = valFan;

  if (buttonFan == 1) {     
    // turn LED on:    
    digitalWrite(fanPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(fanPin, LOW); 
  }
}

//CON IL BOTTONE VADO AD AZIONARE IL LED
void setLed(){
  valLed = digitalRead(buttonPinLed);
  //check if input is HIGH
 
  if ((valLed == HIGH) && (old_val_Led == LOW)) {
    buttonLed = 1 - buttonLed;    
    delay(50);
  }

  old_val_Led = valLed;

  if (buttonLed == 1) {     
    // turn LED on:    
    digitalWrite(LedPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LedPin, LOW); 
  }
}

//-- Commands and parameters (sent by browser) --
char cmd[15];    // Nothing magical about 15, increase these if you need longer commands/parameters
char param1[15];
char param2[15];


void WaitForRequest(EthernetClient client) // Sets buffer[] and bufferSize
{
  bufferSize = 0;

  while (client.connected()) {
    if (client.available()) {
	char c = client.read();
	if (c == '\n')
	  break;
	else
	  if (bufferSize < bufferMax)
	    buffer[bufferSize++] = c;
	  else
	    break;
    }
  }

  PrintNumber("bufferSize", bufferSize);
}

void ParseReceivedRequest()
{
  Serial.println("in ParseReceivedRequest");
  Serial.println(buffer);

  //Received buffer contains "GET /cmd/param1/param2 HTTP/1.1".  Break it up.
  char* slash1;
  char* slash2;
  char* slash3;
  char* space2;

  slash1 = strstr(buffer, "/") + 1; // Look for first slash
  slash2 = strstr(slash1, "/") + 1; // second slash
  slash3 = strstr(slash2, "/") + 1; // third slash
  space2 = strstr(slash2, " ") + 1; // space after second slash (in case there is no third slash)
  if (slash3 > space2) slash3=slash2;

  PrintString("slash1",slash1);
  PrintString("slash2",slash2);
  PrintString("slash3",slash3);
  PrintString("space2",space2);

  // strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
  cmd[0] = 0;
  param1[0] = 0;
  param2[0] = 0;
  strncat(cmd, slash1, slash2-slash1-1);
  strncat(param1, slash2, slash3-slash2-1);
  strncat(param2, slash3, space2-slash3-1);

  PrintString("cmd",cmd);
  PrintString("param1",param1);
  PrintString("param2",param2);
}

void PerformRequestedCommands()
{
  if ( strcmp(cmd,"digitalWrite") == 0 ) RemoteDigitalWrite();
  if ( strcmp(cmd,"digitalRead") == 0 ) RemoteDigitalRead();
  if ( strcmp(cmd,"analogRead") == 0 ) RemoteAnalogRead();
  if ( strcmp(cmd,"analogWrite") == 0 ) RemoteAnalogWrite();
}

void RemoteDigitalWrite()
{
  int ledPin = param1[0] - '0'; // Param1 should be one digit port
  int ledState = param2[0] - '0'; // Param2 should be either 1 or 0
  digitalWrite(ledPin, ledState);
  
  if(ledPin==fanPin) buttonFan = 1 - buttonFan;
  if(ledPin==LedPin) buttonLed = 1 - buttonLed;
  
  //-- Send response back to browser --
  server.print("D");
  server.print(ledPin, DEC);
  server.print(" is ");
  server.print( (ledState==1) ? "ON" : "off" );
  delay(200);
  //-- Send debug message to serial port --
  Serial.println("RemoteDigitalWrite");
  PrintNumber("ledPin", ledPin);
  PrintNumber("ledState", ledState);
}
void RemoteDigitalRead()
{
  int ledPin = param1[0] - '0'; // Param1 should be one digit port
  int ledState; // Param2 should be either 1 or 0
  ledState = digitalRead(ledPin);

  //-- Send response back to browser --
  server.print("D");
  server.print(ledPin, DEC);
  server.print(" is ");
  server.print( (ledState==1) ? "ON" : "off" );
  delay(200);
  //-- Send debug message to serial port --
  Serial.println("RemoteDigitalRead");
  PrintNumber("ledPin", ledPin);
  PrintNumber("ledState", ledState);
}

void RemoteAnalogRead()
{
  // If desired, use more server.print() to send http header instead of just sending the analog value.
  int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
  int analogValue = analogRead(analogPin);

  //-- Send response back to browser --
  server.print("A");
  server.print(analogPin, DEC);
  server.print(" is ");
  server.print(analogValue,DEC);
  delay(200);
  //-- Send debug message to serial port --
  Serial.println("RemoteAnalogRead");
  PrintNumber("analogPin", analogPin);
  PrintNumber("analogValue", analogValue);
}

void RemoteAnalogWrite()
{
  // If desired, use more server.print() to send http header instead of just sending the analog value.
  int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
  int analogValue = param2[0] - '0';
  
  if(analogPin!=3) analogWrite(analogPin, analogValue);
  else{     
    myservo.write((analogValue*20)-1);
    delay(15);
  }
  
  //-- Send response back to browser --
  server.print("A");
  server.print(analogPin, DEC);
  server.print(" is ");
  server.print(analogValue,DEC);
  delay(200);
  //-- Send debug message to serial port --
  Serial.println("RemoteAnalogWrite");
  PrintNumber("analogPin", analogPin);
  PrintNumber("analogValue", analogValue);
}

void PrintString(char* label, char* str)
{
  Serial.print(label);
  Serial.print("=");
  Serial.println(str);
}

void PrintNumber(char* label, int number)
{
  Serial.print(label);
  Serial.print("=");
  Serial.println(number, DEC);
}

Serial.flush() nella versioni dell'IDE 1.x non svuota più il buffer della seriale

Dovresti usare una funzione come

void EmpyBuffer(){
byte a;
while (Serial.available()) {a=Serial.read();}
}

Quindi lo stesso vale per la client.flush?

mancio:
Quindi lo stesso vale per la client.flush?

Non lo so, sono due librerie diverse.
Bisognerebbe controllare il codice della libreria.