serial connection timed out

Hi,

I use an Arduino nano connected to a raspberry Pi with a usb cable. The serial communication works very well and I get the result I want, but after a while when I don't put any command, the connection timed out. In my code I use fastLED library for differents ws2811 strip, and I put one byte information from raspberry to arduino using the arduino-serial lib.
I'm not a good programmer in C, but I think my code is correct. I don't put any delay in loop function because every information that the arduino receive from raspberry is passed to a function with a low cost of execution. I create a web interface from where I can change the color of the differents ws2811 strip, and the information is transmitted from the interface to a node server, from node to differents raspberries, from raspberries to arduino. I can gliss my finger over a smartphone screen and the color of the strips change immediately, and I don't have any problem of data corrupted, so I think the problem is a loss of connection between the two components, because when I press the arduino reset button all returning to works well. Sorry for my bad english, I hope you can help me

This is my code

#include <FastLED.h>
#include "/root/socket/communs/functions.h"

#define NUM_LEDS6 141
#define NUM_LEDS7 39

#define DATA_PIN6 6
#define DATA_PIN7 7

#define MAX_LEDS 150

#define MAX_PIN 13

typedef struct str_leds{
  CRGB leds[MAX_LEDS];
  int num_led;
  int passed;

} str_leds;

str_leds *pin[13] = {0};
int colors[3];
int counter = 0;
int num_bandeau = 0;
int bandeau_id[13] = {0};
int bandeau_index = 0;
int single_leds[13] = {0};
int single_leds_index = 0;
int multiple_leds[13] = {0};
int multiple_leds_index = 0;
int actual_single = 0;
int actual_multiple = 0;
int temp_single_index = 0;
int tot_single = 0;
int temp_multiple_index = 0;
int tot_multiple = 0;
int led_start = -1;
int led_end = -1;
int verification = 1;
int end_string = 0;

int readed = -1;
int counter_led = 0;

void setup() {
  Serial.begin(9600);

  pin[DATA_PIN6] = (str_leds *)malloc(sizeof(str_leds));
  pin[DATA_PIN6]->leds[NUM_LEDS6] = {0};
  pin[DATA_PIN6]->num_led = NUM_LEDS6;
  pin[DATA_PIN6]->passed = 0;


  pin[DATA_PIN7] = (str_leds *)malloc(sizeof(str_leds));
  pin[DATA_PIN7]->leds[NUM_LEDS7] = {0};
  pin[DATA_PIN7]->num_led = NUM_LEDS6;
  pin[DATA_PIN7]->passed = 0;




  FastLED.addLeds<WS2811, DATA_PIN6, RGB>(pin[DATA_PIN6]->leds, NUM_LEDS6);
  FastLED.addLeds<WS2811, DATA_PIN7, RGB>(pin[DATA_PIN7]->leds, NUM_LEDS7);

}

void loop(){

  if((readed = Serial.read())!=-1){
    //Serial.print(readed);
    readline(readed, 1000);
  }

}

int readline(int readch, int len)
{
  int i = 0;
     if((counter<=5)&&(readch==1)){
       counter++;
     }else if((counter>5)&&(counter<=8)){
       colors[counter-6] = readch;
       counter++;
     }else if((counter>8)&&(counter < len)){
                  if(counter==9){
                    num_bandeau = readch;
                  }else if(bandeau_index<num_bandeau){
                      bandeau_id[bandeau_index] = readch;
                      pin[readch]->passed = 0;
                      bandeau_index++;
                  }else if(single_leds_index<num_bandeau){
                      single_leds[single_leds_index] = readch;
                      if(readch!=0){
                        tot_single++;
                      }
                      single_leds_index++;
                  }else if(multiple_leds_index<num_bandeau){
                      multiple_leds[multiple_leds_index] = readch;
                      if(readch!=0){
                        tot_multiple++;
                      }
                      multiple_leds_index++;
                  }else if(actual_single<tot_single){



                      while((single_leds[actual_single]==0)&&(actual_single<num_bandeau)){
                        actual_single++;
                        temp_single_index = 0;
                      }
                      if((actual_single<num_bandeau)&&(temp_single_index<single_leds[actual_single])){

                        pin[bandeau_id[actual_single]]->leds[readch] = CRGB( colors[0], colors[1], colors[2]);

                         temp_single_index++;
                         if((actual_single+1==tot_single)&&(temp_single_index==single_leds[actual_single])){
                            actual_single++;
                            temp_single_index = 0;
                          }

                      }else{
                        actual_single++;
                        temp_single_index = 0;
                      }


                  }else if(actual_multiple<tot_multiple){
                      while((multiple_leds[actual_multiple]==0)&&(actual_multiple<num_bandeau)){
                        actual_multiple++;
                        temp_multiple_index = 0;
                      }
                      if((actual_multiple<num_bandeau)&&(temp_multiple_index<multiple_leds[actual_multiple])){

                        if(led_start==-1){
                          led_start = readch;

                        }else{
                          led_end = readch;
                          if((led_start<led_end)&&(led_start>=0)){
                            for(i=led_start; i<led_end; i++){
                              pin[bandeau_id[actual_multiple]]->leds[i] = CRGB( colors[0], colors[1], colors[2]);
                            }
                          }
                          led_start = -1;
                          led_end = -1;
                          temp_multiple_index++;

                          if((actual_multiple+1==tot_multiple)||(temp_multiple_index==multiple_leds[actual_multiple])){
                            actual_multiple++;
                            temp_multiple_index = 0;
                          }

                        }
                      }else{
                        actual_multiple++;
                        temp_multiple_index = 0;
                      }
                  }else{

                      if(readch==0){
                        end_string++;
                      }else{
                        Serial.println("ahah1");
                        Serial.flush();
                        end_string = 0;
                        memset(colors, 0, sizeof colors);
                        counter = -1;
                        num_bandeau = 0;
                        memset(bandeau_id, 0, sizeof bandeau_id);

                        bandeau_index = 0;
                        memset(single_leds, 0, sizeof single_leds);
                        single_leds_index = 0;
                        memset(multiple_leds, 0, sizeof multiple_leds);
                        multiple_leds_index = 0;
                        actual_single = 0;
                        actual_multiple = 0;
                        temp_single_index = 0;
                        tot_single = 0;
                        temp_multiple_index = 0;
                        tot_multiple = 0;
                        led_start = -1;
                        led_end = -1;
                      }


                      if(end_string==6){
                        Serial.println("ok");
                        Serial.flush();
                        end_string = 0;
                        FastLED.show();
                        memset(colors, 0, sizeof colors);
                        counter = -1;
                        num_bandeau = 0;
                        memset(bandeau_id, 0, sizeof bandeau_id);

                        bandeau_index = 0;
                        memset(single_leds, 0, sizeof single_leds);
                        single_leds_index = 0;
                        memset(multiple_leds, 0, sizeof multiple_leds);
                        multiple_leds_index = 0;
                        actual_single = 0;
                        actual_multiple = 0;
                        temp_single_index = 0;
                        tot_single = 0;
                        temp_multiple_index = 0;
                        tot_multiple = 0;
                        led_start = -1;
                        led_end = -1;
                      }

                  }
              counter++;
     }else{
       Serial.println("ahah2");
       Serial.flush();
       end_string = 0;
       memset(colors, 0, sizeof colors);
       counter = 0;
       num_bandeau = 0;
       memset(bandeau_id, 0, sizeof bandeau_id);

       bandeau_index = 0;
       memset(single_leds, 0, sizeof single_leds);
       single_leds_index = 0;
       memset(multiple_leds, 0, sizeof multiple_leds);
       multiple_leds_index = 0;
       actual_single = 0;
       actual_multiple = 0;
       temp_single_index = 0;
       tot_single = 0;
       temp_multiple_index = 0;
       tot_multiple = 0;
       led_start = -1;
       led_end = -1;
     }
}