How can I increase the sample rate?

Hi all,

I'm using an Arduino Uno and an analog accelerometer with a 16-bit ADC to record vibrations. I'm also writing the data directly to an SD card. I want to increase the sampling rate but I'm not sure what the limiting factor is. I've got the delay in my loop set at 10 (see below) so I was expecting it to sample at 100Hz. However I'm only getting around 20 readings of the x, y and z accelerations per second. Can anyone suggest what I could do to improve this?

#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>

  Adafruit_ADS1115 ads;           // Construct a 16-bit ADS1115 address

  float x, y, z;

  const int chipSelect = 10;
  float time;

void setup() {
  Serial.begin(9600);
Serial.print("Initializing SD card...");
    if (!SD.begin(chipSelect)) {
      Serial.println("Card failed, or not present");
      return;
      }
      
  Serial.println("card initialized.");
  File dataFile = SD.open("datalog.csv", FILE_WRITE);
  dataFile.println("Next batch:");
  dataFile.println(",");
  dataFile.close();
  
  ads.begin();
}

void loop() {
  
  time = micros();
  float secs = time/1000000;

  x = ads.readADC_SingleEnded(3);
  y = ads.readADC_SingleEnded(2);
  z = ads.readADC_SingleEnded(1);

  File dataFile = SD.open("datalog.csv", FILE_WRITE);
  
    dataFile.print(secs,7);
    dataFile.print(",");
    dataFile.print(x,7);
    dataFile.print(",");
    dataFile.print(y,7);
    dataFile.print(",");
    dataFile.println(z,7);
    dataFile.close();
    
  //delay(10);
}

You could try not opening and closing the file every time through loop()

Ok, thanks, so should I try opening and closing the file with subroutines? Not really sure how to implement that, any examples I could look at?

Why not open the file in setup() and only close it when you need to?

Groove:
Why not open the file in setup() and only close it when you need to?

Because I'm not sure how long I need it to run for so I'm just ejecting the SD card when I've got the data I want. Don't know what will happen if I eject with the file open, guess it's worth trying...

Why do this?
float secs = time/1000000;
Just store the micros(), it and the float are both 4 bytes, not gaining anything by dividing. Let the PC do that.

"Because I'm not sure how long I need it to run for so I'm just ejecting the SD card when I've got the data I want.'
So add reading of a button press, when you see it close the file for a graceful end of write and then pull the card.

CrossRoads:
Just store the micros()

Thanks, good shout

As for the button press, would it look something like this:

  Adafruit_ADS1115 ads;           // Construct a 16-bit ADS1115 address

  float x, y, z;

  const int chipSelect = 10;
  const int buttonPin = 6; 
  int buttonState = 0;  
  
void setup() {
  
  pinMode(buttonPin, INPUT);
  
  File dataFile = SD.open("datalog.csv", FILE_WRITE);
  dataFile.println("Next batch:");
  dataFile.println(",");
  
  ads.begin();
}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH){
      dataFile.close();
      }
      
      else { 
         x = ads.readADC_SingleEnded(3);
         y = ads.readADC_SingleEnded(2);
         z = ads.readADC_SingleEnded(1);
            
         dataFile.print(micros,7);
         dataFile.print(",");
         dataFile.print(x,7);
         dataFile.print(",");
         dataFile.print(y,7);
         dataFile.print(",");
         dataFile.println(z,7);
          
         delay(10);
      }
}

Cheers for the help, really appreciate it!