I'm trying detect and then differentiate the time between the arrivals of two 30ms pings (500Hz apart) using the audio adapter and a single mic. The tone.threshold(level) and tone.available() functions don't seem to limit what (or when) gets detected. I need to know when the end of each sonic ping occurs. Can you look at my code, please?
Code:
/* file: Ping_Listener.ino */
#include "AudioSetup.h"
#include "GlobalVars.h"
void loop()
{
if ( sampleReady )
{
noInterrupts();
digitalWriteFast( LED_PIN, HIGH );
Serial.printf( "Ping available: h1:%4.4f, h2:%4.4f; Heard: %d\n",
h1, h2, elapsedTimerH );
h1 = 0.0f;
h2 = 0.0f;
digitalWriteFast( LED_PIN, LOW );
}
sampleReady = false;
elapsedTime = 0;
interrupts();
}
/* file: MainSetup.ino */
void setup()
{
Serial.begin( 115200 ); // start serial monitor support
pinMode( LED_PIN, OUTPUT );
digitalWriteFast( LED_PIN, LOW );
AudioMemory( 64 );
sgtl5000_1.enable();
sgtl5000_1.volume( 0.8 ); // 0.8 is the max undistorted headphone level
sgtl5000_1.inputSelect( AUDIO_INPUT_MIC );
sgtl5000_1.micGain( microphoneGain );
delay( 100 );
toneHeard1.threshold( levelThreshold ); // set up tone detectors to hear either ping
toneHeard2.threshold( levelThreshold );
toneHeard1.frequency( sine1Frequency, minSineCount1 );
toneHeard2.frequency( sine2Frequency, minSineCount2 );
Serial.println( "listening...." );
delay( 50 );
sampleTimer.priority( 144 ); // start interval timer to listen for periodic pings
sampleTimer.begin( pingListen, 21 ); // 21 ~= 48KHz
}
/* file: CallbackFunctions.ino */
void pingListen()
{
// noInterrupts();
// h1 = toneHeard1.read();
// h2 = toneHeard2.read();
// interrupts();
if ( toneHeard1.available() || toneHeard2.available() )
{
noInterrupts();
h1 = toneHeard1.read();
h2 = toneHeard2.read();
elapsedTimerH = elapsedTime;
sampleReady = true;
interrupts();
}
}
/* file: AudioSetup.h */
#include <Audio.h>
AudioControlSGTL5000 sgtl5000_1; //xy=66.1999969482422, 101.99998474121094
AudioAnalyzeToneDetect toneHeard1; //xy=436.1999969482422, 160.1999969482422
AudioAnalyzeToneDetect toneHeard2; //xy=436.1999969482422, 205.1999969482422
AudioInputI2S i2s_mic; //xy=66.1999969482422, 160.1999969482422
AudioConnection patchCord1( i2s_mic, 0, toneHeard1, 0 );
AudioConnection patchCord2( i2s_mic, 0, toneHeard2, 0 );
/* file GlobalVars.h */
#include <TeensyDelay.h>
#define LOW_FREQ_LIMIT 5000
#define HIGH_FREQ_LIMIT 20000
#define LED_PIN 20
IntervalTimer sampleTimer; // timer by which to index ping intervals
elapsedMicros elapsedTime; // timer to evaluate the period following ping-to-detected
volatile unsigned int elapsedTimerH;
volatile unsigned int pingTimer;
volatile float h1, h2;
volatile bool sampleReady = false;
const float gasSoS = 0.343683f; // speed of sound through air in mm/μsec
const float h2oSoS = 1.484f; // speed of sound through water in mm/μsec
const float levelThreshold = 0.20f; // low-threshold level that will set frequency detect = true (0.0 - 1.0)
int microphoneGain = 63; // microphone input gain
int pingTimeMs = 30; // 30.0 ms recommended
int sine1Frequency = LOW_FREQ_LIMIT; // an arbitrary (ping) audible frequency -- the default start frequency
int sine2Frequency = LOW_FREQ_LIMIT + 500; // a second, similar frequency, differentiable from the first
int minSineCount1 = (sine1Frequency / 1000.0f) * pingTimeMs; // allow 30msec to detect sine1 (240 cycles)
int minSineCount2 = (sine2Frequency / 1000.0f) * pingTimeMs; // allow 30msec to detect sine2 (225 cycles)