Home made drum kit - How can I speed up the response time after my sensor gets triggered? I am trying to make a psudo drum kit with my Arduino Due and a couple Force Sensitive Resistors (activated with my fingers, no drum sticks). So far in my testing I'm only using one FSR. I've attached an SD Card reader and put a WAV File on there (44100 Hz, 16-bit, stereo). In my 'Loop', I read in the resistance of the FSR to know if it's been triggered, then I play the WAV file accordingly. It works but its not that fast. If this project is going to work, the response time needs to be much faster. 

Here's my code:

#include "SD.h" //Lib to read SD card
#include "SPI.h" //SPI lib for SD card
#include &lt;Audio.h&gt;

#define SD_ChipSelectPin 2

const int FSR_PIN = A0; // Pin connected to FSR/resistor divider

const float VCC = 4.875; 
const float R_DIV = 10000.0; 

void setup() 
{
  Serial.println("Starting Drum Kit!");
  Serial.begin(9600);
  while (!Serial) {}

  pinMode(FSR_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);


  Serial.print("Initializing SD Card...");
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("FAILED!");
    while(true);
  }
  Serial.println("done");

  // 44100kHz stereo =&gt; 88200 sample rate
  // 100 mSec of prebuffering.
  Audio.begin(88200, 100);

  void loop() 
{
  File snare_file;
  short snare_buffer[1024];
  int fsrADC = analogRead(FSR_PIN);
  if (fsrADC &gt; 20) // If the analog reading is non-zero
  {
    Serial.println(String(fsrADC));

    snare_file = SD.open("606snare.wav");
    while (snare_file.available()) {   
      snare_file.read(snare_buffer, 1024);
      Audio.prepare(snare_buffer, 1024, 1024);
      Audio.write(snare_buffer, 1024);
    }
    snare_file.close();
  }
}


If I hold down the sensor (constant triggering), the fastest that the file plays is about every half second. Here's the Serial output:

15:12:28.453 -&gt; Initializing SD Card...done
15:12:28.487 -&gt; 1023
15:12:28.930 -&gt; 1023
15:12:29.510 -&gt; 1023
15:12:30.050 -&gt; 1023
15:12:30.628 -&gt; 1023
15:12:31.136 -&gt; 1023
15:12:31.744 -&gt; 1023
15:12:32.328 -&gt; 1023
15:12:32.834 -&gt; 1023
15:12:33.411 -&gt; 1023
15:12:33.955 -&gt; 1023


I've tried playing around with loading the file and buffer in the setup section but I can't get it to work properly. There's not a whole lot of documentation on the Audio library either. 

If you know of a better way to speed this up, or if I should be using a different library, please let me know.
This is not really a 'solution', but it might help you in finding out where the real problem is.

Check out which of the statements in the loop function uses up the most time, probably;


int fsrADC = analogRead(FSR_PIN);
Serial.println(String(fsrADC));
snare_file = SD.open("606snare.wav");
while (snare_file.available()) {   
snare_file.read(snare_buffer, 1024);
Audio.prepare(snare_buffer, 1024, 1024);
Audio.write(snare_buffer, 1024);
snare_file.close()


Try improving the most time consuming item if possible.

Some possibilities you can try in the steps above:


Do not use AnalogRead but direct port commands
Remove this completely or at least remove the String instance.
Not much you can do here, except not using a file system but raw SD read (you might have problems getting the files on your SD) without a dedicated program on your PC
Not sure about improving this one
Read less bytes, although it might even worsen things. Or read the bytes BEFORE waiting on the trigger (if you know which sample will be played).
Execute the prepare as before the trigger as 5 if possible.
Not sure about this... High likely you cannot keep too many files open at the same time.


Afaik in my synthesizer (Korg Kronos), it reads from all samples the initial part, meaning it can startup the data from RAM, while the rest is read through SSD (in your case SD). However, you might not have enough memory in an Arduino for that.