Problem when concatenating data of type double to a string.

Greetings … I commented that I have a problem when concatenating variables of type double to a string. It turns out that I am doing readings for the analog inputs, so also for the spi bus, and these readings I am storing them in an array for data of type double, and every so often I concatenate this data in a chain, where each data is separated by a character ‘;’ then send it by bluetooth. The problem is that I can not concatenate all the readings and I do not understand why? In total there are 9 readings that I make, when concatenating and I only manage to concatenate 8 … and there are times when there are only 7. Someone who can help me with this problem please. Thanks in advance.
I leave attached the code that I am using in addition to an image of the error I get…

#include <SPI.h>
#include <SdFat.h>
#include <LowPower.h>
#include <SoftwareSerial.h>
#include <Time.h>
#include <TimeLib.h>
#include <Adafruit_MAX31855.h>
SdFat SD;
File dataFile;
SoftwareSerial bluetooth(7,8);
Adafruit_MAX31855 thermo1(5,2,6);
Adafruit_MAX31855 thermo2(5,3,6);
Adafruit_MAX31855 thermo3(5,4,6);
char conf_inicial[55],aux;//array para recibir configuracion inicial
int time_ref[6]={0,0,0,0,0,0};//tiempo de referencia
double variables[9]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};//array para guardar las variables de lectura
unsigned long actual,previo=0,periodo;//guardan valores de temporizacion y periodo de muestreo
int i,punt=0,ref,stringSize;//variables auxiliares
String paramRecta[8];//almacena cadena de parametros A y B de la recta
float param_Recta[8];//almacena valores de parametros A y B de la recta
String date_time="";
String data_var="";
String peakData="";
String dateReceiveBT="";
void setup() {
  Serial.begin(115200);
  bluetooth.begin(57600);
  thermo1.begin();
  thermo2.begin();
  thermo3.begin();
  if(!SD.begin(SS)){
    Serial.println("Memoria SD no presente o mal conectado");
    while(true);  
  }
  delay(500);
  Serial.println("Esperando parametros"); 
  while(!bluetooth.available());
  while(bluetooth.available()>0){
    conf_inicial[punt]=bluetooth.read();
    punt++;
    delay(10);
  }
  punt=0;
  Serial.println();
  for(ref=0;ref<5;ref++){//separamos la hora y fecha de referencia
    for(i=1;i>=0;i--){
      time_ref[ref]+=(conf_inicial[punt]-48)*pow(10,i);
      punt++;
    }    
  }
  for(i=3;i>=0;i--){//separamos el año
    if(i==0){
      conf_inicial[punt]++;
    }
    time_ref[ref]+=(conf_inicial[punt]-48)*pow(10,i);
    punt++;
  }
  switch(conf_inicial[punt]-48){//verificamos el intervalo de muestreo
    case 1:
      periodo=100;
      break;
    case 2:
      periodo=1000;
      break;
    case 3:
      periodo=10000;
      break;
    case 4:
      periodo=60000;
      break;      
  }
  punt++;
  for(ref=0;ref<8;ref++){//separamos la cadena de parametros de la recta
    for(i=0;i<5;i++){
      if(i==2){
        paramRecta[ref]+=conf_inicial[punt];
        punt++;   
      }else{
        paramRecta[ref]+=(conf_inicial[punt]-48);
        punt++;    
      }
    }    
  }
  for(ref=0;ref<8;ref++){
    param_Recta[ref]=paramRecta[ref].toFloat();
  }
  setTime(time_ref[0],time_ref[1],time_ref[2],time_ref[3],time_ref[4],time_ref[5]);//inicializamos hora y fecha de referencia
  ref=0;
  delay(500);
}
void loop(){
  if(Serial.available()||bluetooth.available()){
    if(Serial.available()>0){
      aux=Serial.read();
      if(aux=='1'){
        dataFile = SD.open("lectura.txt");
        if (dataFile) {
          Serial.println("lectura.txt:");
          while (dataFile.available()) {
            Serial.write(dataFile.read());
          }
          dataFile.close(); //cerramos el archivo
        } else {
          Serial.println("Error al abrir el archivo");
        }
      }
    }else{
      aux=bluetooth.read()-48;
      switch(aux){//verificamos el intervalo de muestreo
        case 0:
          SD.remove("lectura.txt");//eliminamos el archivo "lectura.txt"
          dataFile = SD.open("lectura.txt",FILE_WRITE);//se crea nuevo archivo "lectura.txt"
          dataFile.close();
          bluetooth.print('1'); //notifica a Android que se realizo la limpieza de la memoria
        case 1:
          periodo=100;
          break;
        case 2:
          periodo=1000;
          break;
        case 3:
          periodo=10000;
          break;
        case 4:
          periodo=60000;
          break;
        case 5:
          Serial.println("El muestreo se ha detenido");
          while(!bluetooth.available()){
            LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);        
          }
          aux=bluetooth.read();             
          Serial.println("El muestreo se ha retomado");
          break;
      }        
    }
  }else{
    actual = millis();
    if((actual-previo)>=periodo){
      concTime();
      date_time+=';';
      for(ref=0;ref<9;ref++){
          data_var+=variables[ref];
          data_var+=';';
      }
      bluetooth.println(data_var);
      Serial.println(data_var);
      dataFile = SD.open("lectura.txt",FILE_WRITE);
      if(dataFile){
        date_time+=data_var;
        dataFile.println(date_time);
        dataFile.close();
      }
      data_var="";
      date_time="";
      previo=millis();
      }else{
          variables[0] = (8.85*(analogRead(0)*(5/1023.0)))+0.87;//Voltaje de Bateria
          variables[1] = (param_Recta[0]*(analogRead(1)*(5/1023.0)))-param_Recta[1];//Corriente de Bateria
          if(variables[0]>20||variables[1]>100){
            date_time+='*';
            concTime();
            date_time+='*';
            bluetooth.println(date_time);
            Serial.println(date_time);
            for(ref=0;ref<9;ref++){
              if(ref==0||ref==1){
                data_var+='

;
               data_var+=variables[ref];//concatenamos variables
             }else{
               data_var+=’;’;//concatenamos caracter de separacion
               data_var+=variables[ref];//concatenamos los valores de las variables  
             }    
           }
           dataFile = SD.open(“lectura.txt”, FILE_WRITE);
           if(dataFile){
             date_time+=data_var;
             dataFile.println(date_time);
             dataFile.close();  
           }
           data_var="";
           date_time="";
           while(variables[0]>20||variables[1]>100){
             for(ref=0;ref<9;ref++){
               if(ref==0||ref==1){
                 data_var+=’


![errorConcatenate.PNG|878x495](upload://A2aG5iaNJMzCimCl4OBeSaoLYjF.png);
                  data_var+=variables[ref];//concatenamos variables
                }else{
                  data_var+=';';//concatenamos caracter de separacion
                  data_var+=variables[ref]; 
                }    
              }
              dataFile = SD.open("lectura.txt", FILE_WRITE);//abrimos el archivo lectura.txt
              if(dataFile){
                dataFile.println(data_var);
                dataFile.close();  
              }                    
              data_var="";
              variables[0] = (8.85*(analogRead(0)*(5/1023.0)))+0.87;//Voltaje de Bateria
              variables[1] = (param_Recta[0]*(analogRead(1)*(5/1023.0)))-param_Recta[1];//Corriente de Bateria     
            }  
            date_time="#";
            concTime();
            date_time+=';';
            for(ref=0;ref<9;ref++){
              data_var+=variables[ref];
              data_var+=';';//concatenamos caracter de separacion
            }
            dataFile = SD.open("lectura.txt", FILE_WRITE);//abrimos el archivo lectura.txt
            if(dataFile){//verificamos si existe el archivo en la memoria
              date_time+=data_var;
              dataFile.println(date_time);//guardamos el dato en la memoria
              dataFile.close();//cerramos cesion con la memoria 
            }
            data_var="";
            date_time="";                  
          }              
          variables[2] = (param_Recta[2]*(analogRead(2)*(5/1023.0)))-param_Recta[3];
          variables[3] = (param_Recta[4]*(analogRead(3)*(5/1023.0)))-param_Recta[5];
          variables[4] = (param_Recta[6]*(analogRead(4)*(5/1023.0)))-param_Recta[7];
          variables[5] = thermo1.readCelsius();
          variables[6] = thermo2.readCelsius();
          variables[7] = thermo3.readCelsius();       
          variables[8] = 0.00;//Temperatura de PCB       
      }
  }
}

void concTime(){
  time_t t = now(); 
  date_time+=month(t);
  date_time+='/';
  date_time+=day(t);
  date_time+='/';//concatenamos caracter de separacion
  date_time+=year(t);
  date_time+=';';//concatenamos caracter de separacion
  date_time+=hour(t);
  date_time+=':';
  date_time+=minute(t);
  date_time+=':';
  date_time+=second(t);
}

you are communicating with bluetooth using SoftwareSerial at 57600baud SoftwareSerial can have problems over 37400baud try reducing the bluetooth to 19600baud

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

in addition to an image of the error I get

You posted a picture of text that does NOT clearly illustrate and error. You need to post text AS TEXT, and explain how the output differs from what you expect.

You are NOT doing what your thread title suggests. There are NO strings in your code.

There are Strings, which are NOT the same thing.

YOU need to look at the header file for the class you are using. It is YOUR responsibility to know which file that is, and where it is located.

cesar182:

  for(ref=0;ref<9;ref++){

data_var+=variables[ref];
     data_var+=’;’;
  }
  bluetooth.println(data_var);
  Serial.println(data_var);

I can’t see anything in that bit of code which would give your serial monitor output output
like:

1.43;4.08;-14.75;-13.70;104.23;0.00;0.00;0.00
1.43;11.02;-14.75;-13.70;104.23;0.00;;;

so your problem may be due to running out of memory.

I would suggest you follow this advice:

Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

…R

Yours,
TonyWilk

First of all thanks to everyone for answering, regarding what Robin2 advises me, how could I initialize this arrangement of characters, and is it possible to concatenate other characters to this string in the same way that I do for String or do I need a pointer? Excuse my ignorance.

If you use a string (a NULL terminated array of chars), you can use strcpy() and strcat() populate and concatenate data in the string.

Thanks PaulS, definitely the problem was the SRAM memory, followed your advice and also use a macro which optimizes the use of the SRAM when making a Serial.print (string), simply adding the following Serial.print (F (string )).