Trigger box for sounds

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);
}

}

}

Stop printing out messages.

You don’t need to open the file every time through the loop either.

This seems like a somewhat strange request. How come?

Ok, but how can this affect the delay between the button press and the sound appearance? Those are things which, I agree, they are redundant, but occur before the button press, they are not in this part:

if ( digitalRead(2) == HIGH){
  Audio.write(buffer, S);
}

I'm doing an experiment for my Ph.D. in neuroscience, and I need to have ~0 msec delay between a trigger (the button will be substituted with a signal coming from a photodiode) and the onset of the sound, they have to be in sync. That's why..

Because the button may be pressed anytime but you only find out about it when you do the digitalRead call. So the more printing you have, the more dead time you have where the button can be pressed and you are waiting to get round to this line again.

Sounds like you need the button to trigger an interrupt and the ISR to trigger the sound.

1 Like