Hi,
I'm trying to use Teensy 3.5 with the Audio Shield to develop a project that consists in giving an audio stimulus and simultaneously use ADC to data acquisition of the response to the stimulus. I'm having some trouble because at the beginning of the project I thought that it was possible to use '.play("audiofile")' and the audiofile would play while I could use the microcontroller to do other things, but if I don't wait at least the length of the sound used there's no audible output.
At this moment I'm playing a 25ms sound, giving it a delay of 25 so I can hear the sound and have the data acquisition saved in the SD card properly, if I don't use the delay, the conversion doesn't work well or I don't hear any sound. I'm using adc0_isr() to have a specific frequency sampling rate, saving the values of the acquisition to the SD card.
My question is: Is it possible to play the sound and start the ADC with the interrupt without having to wait for the all sound to play or in this case is mandatory that I wait while the sound is playing? I'm leaving the code I'm using below and would appreciate immensely if someone could help. Thank you very much in advance,
Aurora
(I'm using LDR as a turn on button)
I'm trying to use Teensy 3.5 with the Audio Shield to develop a project that consists in giving an audio stimulus and simultaneously use ADC to data acquisition of the response to the stimulus. I'm having some trouble because at the beginning of the project I thought that it was possible to use '.play("audiofile")' and the audiofile would play while I could use the microcontroller to do other things, but if I don't wait at least the length of the sound used there's no audible output.
At this moment I'm playing a 25ms sound, giving it a delay of 25 so I can hear the sound and have the data acquisition saved in the SD card properly, if I don't use the delay, the conversion doesn't work well or I don't hear any sound. I'm using adc0_isr() to have a specific frequency sampling rate, saving the values of the acquisition to the SD card.
My question is: Is it possible to play the sound and start the ADC with the interrupt without having to wait for the all sound to play or in this case is mandatory that I wait while the sound is playing? I'm leaving the code I'm using below and would appreciate immensely if someone could help. Thank you very much in advance,
Aurora
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <ADC.h>
#include <ADC_util.h>
AudioPlaySdWav playSdWav1;
AudioOutputI2S audioOutput;
AudioConnection patchCord1(playSdWav1, 0, audioOutput, 0);
AudioConnection patchCord2(playSdWav1, 1, audioOutput, 1);
AudioControlSGTL5000 sgtl5000_1;
// Use these with the Teensy 3.5 & 3.6 SD card
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define BUFFER_SIZE 500
int ledPin = 13;
const int Pin4 = A20; //LDR
const int readPin = A14; //input signal
int controlo = 0;
ADC *adc = new ADC();
File blah;
uint32_t freq = 30000; //Freq Timer = 30kHz
int buffer_count = 0;
String dataString = "";
uint32_t delta_time_adc_0 = 0;
elapsedMillis timed_read_elapsed;
void setup() {
Serial.begin(9600);
delay(1000);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
pinMode(Pin4, INPUT);
pinMode(readPin, INPUT);
AudioMemory(60);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
adc->adc0->setAveraging(1); // set number of averages
adc->adc0->setResolution(16); // set bits of resolution
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_HIGH_SPEED);
adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED);
}
void loop() {
int Status = analogRead(Pin4);
if (Status <= 700 and controlo == 0) //condition to start
{
controlo = 1;
playSdWav1.play("ex4.wav");
delay(25);
blah = SD.open("Teste.csv",FILE_WRITE);
Serial.print("Start Timer with frequency ");
Serial.print(freq);
Serial.println(" Hz.");
adc->adc0->stopTimer();
adc->adc0->startSingleRead(readPin); // call this to setup everything before the Timer starts, differential is also possible
adc->adc0->enableInterrupts(adc0_isr);
adc->adc0->startTimer(freq); //frequency in Hz
}
}
void adc0_isr() {
uint16_t adc_val = adc->adc0->readSingle();
if (buffer_count < BUFFER_SIZE) {
buffer_count++;
dataString += String(adc_val);
dataString += ",";
if (buffer_count == BUFFER_SIZE){
blah.println(dataString);
Serial.print("Saved to Sd card");
blah.close();
delta_time_adc_0 = timed_read_elapsed; //delta_time_adc_0 keeps the value of the time consumed
}
}
}