Need to read voltage 100 times/second & write data to SD card. Memory issue?

I wasn't sure what section to post this in, but hopefully someone here could point me in the right direction.

The end goal of this project is to read voltage 0-5v off a PSI sensor around 100 times every second and record the data and millisecond as a text file on an SD card (shield is adafruit). The PSI sensor is connected to an air shock and we're measuring the curve of impact when dropped on the ground. Without any buttons or switches the current train of thought is to store the data to a string & write the string to a new file every 5 seconds or so. That way, when we take out the SD card to collect the data, only the last text file will be corrupted and the files containing our actual data will be have already been properly closed out. I know, it's a messy hack & we should just use a switch to close out the file...working on it.

My issue (regardless of using a switch/button) is I can only seem to get around 10 measurements a second before it starts writing non-sense to the SD card. It varies from completely empty files, to only half of the text being written, to crazy readings. I'm assuming this is a memory/bandwidth issue since when I slow it down to 10 readings/second it works fine? Any recommendations to get this up to the proper speed (100 readings/second)? Assembly? Memory tricks? Buffering? I'm currently thinking of hooking the arduino (via serial) up to my Raspberry Pi since it can store much more data before having to dump it & write to the SD card on the Pi.

#include <SPI.h>
#include <SD.h>


const int chipSelect = 10;  // SD Card is hooked up to pin 10 (Ethernet Sheild uses 4)
unsigned long time;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");
  
  //SD.begin(chipSelect);

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized."); 

  //SD.remove("/");

  //Serial.println("Creating example.txt...");

  
  //File dataFile = SD.open("datalog.txt", FILE_WRITE);
  
  Serial.println("3");
  delay(1000);
  Serial.println("2");
  delay(1000);
  Serial.println("1");
  delay(1000);
  
} 


// the loop routine runs over and over again forever:
void loop() {
  String toSDcard = "";
  String fileName = String(millis());
  fileName += ".TXT";
  File dataFile = SD.open(fileName, FILE_WRITE);
  
  for (int i = 0; i < 30; i++) {
    time = millis();
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (5.0 / 1023.0);
    // print out the value you read:

    //toSDcard += String(time, DEC);
    toSDcard += time;
    toSDcard += " ";
        
    toSDcard += String(voltage, 3); // appends float (4 decimal spaces) to string
    toSDcard += '\n';
    //Serial.println(toSDcard);
    delay(100);
    
  }
  Serial.println(toSDcard);
  Serial.println(fileName);
  
  dataFile.println(toSDcard);
  dataFile.close();
  toSDcard = "";
  //Serial.println("NEW FILE!!!!");
}

Yeah, SD card access is slow.

Why, oh why, must you use the String class to concatenate output that is written sequentially anyway? The String class is a known source of mysterious crashes due to memory problems on Arduino.

aarg:
Yeah, SD card access is slow.

Why, oh why, must you use the String class to concatenate output that is written sequentially anyway? The String class is a known source of mysterious crashes due to memory problems on Arduino.

I don't have to use String!! Please show me the error of my ways. What would you recommend I use to concatenate the output?

McTerd:
I don't have to use String!! Please show me the error of my ways. What would you recommend I use to concatenate the output?

You don't have to concatenate it. Just write it out one item at a time. Whatever you write always appends the end of the file.

aarg:
You don't have to concatenate it. Just write it out one item at a time. Whatever you write always appends the end of the file.

The idea was to use String as a buffer to use as few writes to the SD card as possible. With the timestamp in there as well, is the SD card interface even able to take 200 (timestamp + reading) a second?

Gimme 5 minutes, I'll try this out.

Writes to an SD card go to a memory buffer first before the actual transfer to the card, so it is futile to store it up.

Just remember to flush from time to time...