Strange Terminators at the end of "String"


hello
i building datalogger , intend store data in sd card. data comes in 1 datavalue @ time sequence of 6 values per dataset. wanted write data sd card , serial monitor 1 line @ time (i feel faster).

i wrote small code simulate data generation , data recording sd card. code seems work except data terminated strange data 121212..., 222222... etc


what doing wrong? , how can prevent unwanted data consuming sd card memory space.
 
the code is:

code: [select]

#include <spi.h>
#include <sd.h>

const int chipselect = 4;

void setup()
{
  // open serial communications , wait port open:
  serial.begin(9600);

  string datastring = "";
  string datastringx = "";

  while (!serial)
  {
    ; // wait serial port connect. needed native usb port only
  }

  // see if card present , can initialized:
  if (!sd.begin(chipselect))
  {
    serial.println("card failed, or not present");
    // don't more:
    return;
  }
  serial.println("card initialized.");


  // open file. note 1 file can open @ time,
  // have close 1 before opening another.
  file datafile = sd.open("datalog.txt", file_write);
 
  serial.println("datalog.txt");
  datafile.println("datalog.txt");
  serial.println("");
  datafile.println("");

  // if file available, write it:

  if (datafile)
  {
    (int = 0; < 30; i++)
    {
      (int j = 0; j < 6; j++)
      {
        datastring += string(serial.print(20 * i));
        datastring += string(serial.print(" \t"));
        datastringx += string(datafile.print(20 * i));
        datastringx += string(datafile.print(" \t"));
      }
      datastring += string(serial.print('\0'));
      datastringx += string(datafile.print('\0'));
     
      serial.print(datastring);
      datafile.print(datastringx);
     
      datastring=""; //clear datastring
      datastringx=""; //clear datastring
     
      serial.println();
      datafile.println();
             
    }
    datafile.close();
  }
  else
  {
    serial.println("error opening datalog.txt");
  }


}

void loop()
{


}





the data displayed on serial monitor , sd card presented below. thank you.

datalog.txt

0    0    0    0    0    0     1212121212121
20    20    20    20    20    20     2222222222221
40    40    40    40    40    40     2222222222221
60    60    60    60    60    60     2222222222221
80    80    80    80    80    80     2222222222221
100    100    100    100    100    100     3232323232321
120    120    120    120    120    120     3232323232321
140    140    140    140    140    140     3232323232321
160    160    160    160    160    160     3232323232321
180    180    180    180    180    180     3232323232321
200    200    200    200    200    200     3232323232321
220    220    220    220    220    220     3232323232321
240    240    240    240    240    240     3232323232321
260    260    260    260    260    260     3232323232321
280    280    280    280    280    280     3232323232321
300    300    300    300    300    300     3232323232321
320    320    320    320    320    320     3232323232321
340    340    340    340    340    340     3232323232321
360    360    360    360    360    360     3232323232321
380    380    380    380    380    380     3232323232321
400    400    400    400    400    400     3232323232321
420    420    420    420    420    420     3232323232321
440    440    440    440    440    440     3232323232321
460    460    460    460    460    460     3232323232321
480    480    480    480    480    480     3232323232321
500    500    500    500    500    500     3232323232321
520    520    520    520    520    520     3232323232321
540    540    540    540    540    540     3232323232321
560    560    560    560    560    560     3232323232321
580    580    580    580    580    580     3232323232321

those numbers generated in code (it told do).

code: [select]
    (int = 0; < 30; i++)
    {
      (int j = 0; j < 6; j++)
      {
        datastring += string(serial.print(20 * i));
        datastring += string(serial.print(" \t"));
        datastringx += string(datafile.print(20 * i));
        datastringx += string(datafile.print(" \t"));
      }
      datastring += string(serial.print('\0'));
      datastringx += string(datafile.print('\0'));


why??

x.print() returns number of characters printed.

then append numbers strings. ( string(1) + string(1) == "11" )

if want add counts use integer. ( int(1) + int(1) == 2 )


Arduino Forum > Using Arduino > Programming Questions > Strange Terminators at the end of "String"


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'