SD Datastring Convention
i wondering if there preferred convention appending data logged? see standard sd example uses:
creating string called datastring, , appending it, have seen in forums string not liked. around this, code data logging looks this:
i have 5 floating point numbers log, first convert them dtostrf, because apparently arduino can't handle floats in sprintf. after that, append datastring sprintf. wondering if effecient way it? or easier manually call print of variable, print comma, print next variable, etc?
code: [select]
void loop() {
// make string assembling data log:
string datastring = "";
// read 3 sensors , append string:
(int analogpin = 0; analogpin < 3; analogpin++) {
int sensor = analogread(analogpin);
datastring += string(sensor);
if (analogpin < 2) {
datastring += ",";
}
}
creating string called datastring, , appending it, have seen in forums string not liked. around this, code data logging looks this:
code: [select]
void sdupdate()
{
datetime = rtc.now();
unsigned int hour = now.hour();
unsigned int minute = now.minute();
unsigned int second = now.second();
char datastring[100] = "";
char char_current_pay[15] = "";
char char_overall_pay[15] = "";
char char_tension[15] = "";
char char_tmotor[10] = "";
char char_tstorage[10] = "";
dtostrf(current_pay, 10, 1, char_current_pay);
dtostrf(overall_pay, 10, 1, char_overall_pay);
dtostrf(tension, 10, 1, char_tension);
dtostrf(tmotor_temp, 4, 1, char_tmotor);
dtostrf(tstorage_temp, 4, 1, char_tstorage);
sprintf(datastring, "%s, %s, %d, %s, %s, %s", char_tension, char_current_pay, maintenance_count, char_overall_pay, char_tmotor, char_tstorage);
if(analogread(vin) >= minvin) // checks if vin high enough before starting write
{
file datafile = sd.open("tr01806.csv", o_creat | o_append | o_write);
if(datafile)
{
datafile.print(hour);
datafile.print(":");
datafile.print(minute);
datafile.print(":");
datafile.print(second);
datafile.print(",");
datafile.println(datastring);
datafile.flush();
datafile.close();
}
}
else
{
eeprom.write(error_address, 1);
eeprom.write(error_prompt_address, 0);
error_it = true;
}
}
i have 5 floating point numbers log, first convert them dtostrf, because apparently arduino can't handle floats in sprintf. after that, append datastring sprintf. wondering if effecient way it? or easier manually call print of variable, print comma, print next variable, etc?
quote
after that, append datastring sprintf.pissing away resources unnecessarily. why?
code: [select]
datafile.print(hour);
datafile.print(":");
datafile.print(minute);
datafile.print(":");
datafile.print(second);
datafile.print(",");
datafile.print(char_tension);
datafile.print(", ");
datafile.print(char_current_pay);
datafile.print(", ");
datafile.print(maintenance_count);
datafile.print(", ");
// etc...
quote
i wondering if effecient way it?of course not. overhead of sprintf() , buffer contain data copied buffer unnecessary. unnecessary not efficient way of doing anything.
Arduino Forum > Using Arduino > Programming Questions > SD Datastring Convention
arduino
Comments
Post a Comment