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 <Audio.h>
#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 => 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 > 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 -> Initializing SD Card...done
15:12:28.487 -> 1023
15:12:28.930 -> 1023
15:12:29.510 -> 1023
15:12:30.050 -> 1023
15:12:30.628 -> 1023
15:12:31.136 -> 1023
15:12:31.744 -> 1023
15:12:32.328 -> 1023
15:12:32.834 -> 1023
15:12:33.411 -> 1023
15:12:33.955 -> 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.
setup()
, but it didn't work properly. How did it not work? I think moving the loading there is really time consuming. Though keeping the sound loaded permanently might get a problem, when you need to load several files for several drum pads.