function database_write(catena,IDcentralina,DTcatena,...
    AlfaX,AlfaY,TempDef_RSN,ARRAYdateRSN,ErrRSNLink,...
    AlfaX_HR,AlfaY_HR,TempDef_RSNHR,ARRAYdateRSNHR,ErrRSNLinkHR,...
    LoadDef,ErrLoadLink,ARRAYdateLL,val_TrL,StoricoTrigger,ErrTriggerLink,ARRAYdateTrL,...
    val_SS,StoricoShock,ErrShockSensor,ARRAYdateSS,conn,FileName)

%% Attivo/disattivo la scrittura
[scrivoRSN,scrivoRSN_HR,scrivoLL,scrivoTrL,scrivoSS] = scrivo(AlfaX,...
    AlfaX_HR,LoadDef,val_TrL,val_SS);

text = 'database_write function started';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);

%% Contatori nodi
cRSN = 1; % RSN Link
cRSNHR = 1; % RSN Link HR 
cLL = 1; % Load Link
cTrL = 1; % Trigger Link
cSS = 1; % Shock Sensor

% conto i dati
[~,colRSN] = size(AlfaX'); % controllo quanti dati ho
[~,colRSNHR] = size(AlfaX_HR'); % controllo quanti dati ho
[~,colLL] = size(LoadDef'); % controllo quanti dati ho
[~,colTrL] = size(val_TrL'); % controllo quanti dati ho
[~,colSS] = size(val_SS'); % controllo quanti dati ho
[rC,~] = size(catena);
idNodo = cell2mat(catena(:,1));

text = 'Upload of the results in the DB started';
fprintf(fileID,fmt,text);

%% Scrittura risultati su DB
for a = 1:rC 
    NodeNum = cell2mat(catena(a,3));
    
    %% RSN Link 
    if  idNodo(a,1) == 38
        if scrivoRSN == 1 % se ho correttamente tutti i dati
            ListaDate = colRSN;
            
            DATAinsert = cellMakerRSN(IDcentralina,DTcatena,NodeNum,cRSN,ListaDate,...
                ErrRSNLink,ARRAYdateRSN,AlfaX,AlfaY,TempDef_RSN);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdateRSN,conn);
            
            DBwriteRSN(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cRSN = cRSN+1;
            clear DATAinsert
            % Logfile
            text = ['RSN Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ' of unit ' IDcentralina ' and chain ' DTcatena ' written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
                 
    %% RSN Link HR 
    elseif  idNodo(a,1) == 39
            if scrivoRSN_HR == 1 % se ho correttamente tutti i dati
               ListaDate = colRSNHR;
               
               DATAinsert = cellMakerRSNHR(IDcentralina,DTcatena,NodeNum,cRSNHR,ListaDate,...
                   ErrRSNLinkHR,ARRAYdateRSNHR,AlfaX_HR,AlfaY_HR,TempDef_RSNHR);
               
                idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdateRSNHR,conn); 
                
                DBwriteRSNHR(DATAinsert,idElabData,ListaDate,NodeNum,DTcatena,IDcentralina,conn);
                
                cRSNHR = cRSNHR+1;
                clear DATAinsert
                % Logfile
                text = ['RSN Link HR node no. ' num2str(NodeNum) ' of ' ...
                    num2str(rC) ' of unit ' IDcentralina ' and chain ' DTcatena ' written in the DB!'];
                fprintf(fileID,fmt,text);
            end
            
      
    %% Load Link
    elseif  idNodo(a,1) == 15 % Load Link  
            if scrivoLL == 1
               ListaDate = colLL;
               
               DATAinsert = cellMakerLL(IDcentralina,DTcatena,NodeNum,cLL,...
                   ListaDate,ARRAYdateLL,LoadDef,ErrLoadLink);
               
               idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdateLL,conn);
               
               DBwriteLL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
               
               cLL = cLL+1;
               clear DATAinsert
               % Logfile
               text = ['Load Link node no. ' num2str(NodeNum) ' of ' ...
                   num2str(rC) ' of unit ' IDcentralina ' and chain ' DTcatena ' written in the DB!'];
               fprintf(fileID,fmt,text);
            end
               
            
    %% Trigger Link
    elseif  idNodo(a,1) == 40 % Trigger Link  
            if scrivoTrL == 1
               ListaDate = colTrL;
               
               DATAinsert = cellMakerTrL(DTcatena,IDcentralina,NodeNum,ListaDate,...
                   ARRAYdateTrL,val_TrL,StoricoTrigger,cTrL,ErrTriggerLink);
            
               idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdateTrL,conn);
               
               DBwriteTrL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
               
               cTrL = cTrL+1;
               clear DATAinsert
               % Logfile
               text = ['Trigger Link node no. ' num2str(NodeNum) ' of ' ...
                   num2str(rC) ' of unit ' IDcentralina ' and chain ' DTcatena ' written in the DB!'];
               fprintf(fileID,fmt,text);
            end  

    %% Shock Sensor
    elseif  idNodo(a,1) == 54 % Shock Sensor 
            if scrivoSS == 1
               ListaDate = colSS;
               
               DATAinsert = cellMakerSS(DTcatena,IDcentralina,NodeNum,ListaDate,...
                   ARRAYdateSS,val_SS,StoricoShock,cSS,ErrShockSensor);
            
               idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdateSS,conn);
               
               DBwriteSS(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
               
               cTrL = cTrL+1;
               clear DATAinsert
               % Logfile
               text = ['Trigger Link node no. ' num2str(NodeNum) ' of ' ...
                   num2str(rC) ' of unit ' IDcentralina ' and chain ' DTcatena ' written in the DB!'];
               fprintf(fileID,fmt,text);
            end  
            
    end
    
end

text = 'DB upload completed. database_write function ended';
fprintf(fileID,fmt,text);
fclose(fileID);

end
