Dear friends,
I'm realizing a very simple system with an Arduino DUE. My aim is to implement a trigger-box, where a brief sound is triggered (i.e. played from the DAC) when a button is pressed. The sound is a .wav file of about 87KB. I want the triggering of the sound to be fairly immediate, < 1msec after the button press, this is crucial.
At the moment, I have realized a very simple circuit where an SD card shield is connected to the DPI pins of my Arduino DUE, and a button-press is recorded to a digital pin. I'm sending a copy of both the button-press and the auditory signal to an oscilloscope to compare their onsets. I get a delay of ~5msec from the button press and the starting of the auditory signal. Do you know which strategies might reduce this delay at < 1msec? Thanks a lot!
Here is the code I'm using:
#include <SD.h>
#include <SPI.h>
#include <Audio.h>
File myFile;
long t = 0;
long debounce_delay = 2000;
void setup() {
// debug output at 9600 baud
pinMode(2, INPUT);
Serial.begin(9600);
// setup SD-card
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println(" failed!");
return;
}
Serial.println(" done.");
// hi-speed SPI transfers
SPI.setClockDivider(4);
// 44100Khz stereo
// 100 mSec of prebuffering.
Audio.begin(44100, 1);
}
void loop() {
myFile = SD.open("noise.wav");
if (myFile) {
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening noise.wav");
}
int volume = 1024;
const int S=512; // Number of samples to read in block
short buffer[S];
Serial.print("Playing");
// until the file is not finished
while (myFile.available()) {
// read from the file into buffer
myFile.read(buffer, sizeof(buffer));
Audio.prepare(buffer, S, volume);
if ( digitalRead(2) == HIGH){
Audio.write(buffer, S);
}
}
}