String of float variables.

Hi All,

I have a program that collects data from a weather shield presents the data as floats and the logs the data to an sd card and exports to serial.

The program works but is there a better way to construct the string?
Also Is there a way to format the data with a leading zero ie the seconds go from 01, 02, 03... to 59.

As I said the code works but im am wondering if there is a cleaner way to do this.

Thanks in advanced for any help. :slight_smile:

void logWeather()
{
dtostrf(now.year(),4, 0,dtostrfbuffer);
  String dataA = (dtostrfbuffer);
  dtostrf(now.month(),2, 0,dtostrfbuffer);
  String dataB = (dtostrfbuffer);
  dtostrf(now.day(),2, 0,dtostrfbuffer);
  String dataC = (dtostrfbuffer);
  dtostrf(now.hour(),2, 0,dtostrfbuffer);
  String dataD = (dtostrfbuffer);
  dtostrf(now.minute(),2, 0,dtostrfbuffer);
  String dataE = (dtostrfbuffer);
  dtostrf(now.second(),2, 0,dtostrfbuffer);
  String dataF = (dtostrfbuffer);
  dtostrf(humidity,2, 2,dtostrfbuffer);
  String dataG = (dtostrfbuffer);
  dtostrf(tempf,2, 2,dtostrfbuffer);
  String dataH = (dtostrfbuffer);
  dtostrf(temp,2, 2,dtostrfbuffer);
  String dataI = (dtostrfbuffer);
  dtostrf(pressure,2, 2,dtostrfbuffer);
  String dataJ = (dtostrfbuffer);
  dtostrf(altitude,4, 2,dtostrfbuffer);
  String dataK = (dtostrfbuffer);
  dtostrf(altitudef,4, 2,dtostrfbuffer);
  String dataL = (dtostrfbuffer);
  dtostrf(light_lvl,2, 2,dtostrfbuffer);
  String dataM = (dtostrfbuffer);
  dtostrf(batt_lvl,2, 2,dtostrfbuffer);
  String dataN = (dtostrfbuffer);
  
    
   // make a string for assembling the data to log:
  String dataString = (dataA + ":" + dataB + ":" + dataC + ":" + dataD + ":" + dataE + ":" + dataF + ":" + dataG + ":" + dataH + ":" + dataI + ":" + dataJ + ":" + dataK + ":" + dataL + ":" + dataM + ":" + dataN);
  
  
  dataFile.println(dataString);
  Serial.println(dataString);

  dataFile.flush();
}

The program works but is there a better way to construct the string?

Are you asking about strings or the resource wasting Strings? Why do you need to have a string or String at all? Writing to the serial port is no more efficient with a string or String than writing the data directly. Ditto writing to an SD card.

Hi Thanks for the reply,

Im using the string format to send the group of information together. I did not think about sending the data items indavidually. I will take a look at this as an alternative.

My question was more about the if this is the correct way to do this as I am looking to improve my coding.

Presenting the data indavidually would give more control over the formatting issue I guess.

Thanks

My question was more about the if this is the correct way to do this as I am looking to improve my coding.

It's not, because what you are trying to do is not necessary.

Ok thanks.

I will investigate other options to simplify things.

Thanks for the help.