You will see if you run the code and have "heart.wav" on your SD card that it counts 9 beats when in reality there are only 6 in the file.
Currently the my delay is set to 200ms which should be plenty of time between beats.
Here is the audio file
https://www.dropbox.com/s/z7nu6lg9tv...heart.wav?dl=0
Any thoughts?
Cheers.
Phil
Code: Select all
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1; //xy=168,263
AudioAnalyzePeak peak1; //xy=491,482
AudioAnalyzePeak peak2; //xy=502,344
AudioOutputI2S i2s1; //xy=610,203
AudioConnection patchCord1(playSdWav1, 0, peak2, 0);
AudioConnection patchCord2(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord3(playSdWav1, 1, peak1, 0);
AudioConnection patchCord4(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=189,662
// GUItool: end automatically generated code
int delaytime = 200; //delay time in milliseconds
boolean lubdub = true;
int led = 2;
int thresholdlevel = 15; // range 0 to 30
int cnt = 0;
void setup() {
Serial.begin(115200);
AudioMemory(10);
pinMode(led, OUTPUT);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(7);
SPI.setSCK(14);
if (!(SD.begin(10))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
delay(1000);
}
// for best effect make your terminal/monitor a minimum of 62 chars wide and as high as you can.
elapsedMillis msecs;
void loop() {
if (playSdWav1.isPlaying() == false) {
Serial.println("Start playing");
playSdWav1.play("heart.WAV");
delay(10); // wait for library to parse WAV info
}
if (msecs > 40) {
if (peak2.available()) {
msecs = 0;
float rightPeak = peak2.read() * 30;
if ( rightPeak > thresholdlevel) {
switch (lubdub) {
case 1:
digitalWrite(led, HIGH);
Serial.print("lub ");
cnt++;
Serial.println(cnt);
myDelay(delaytime);
digitalWrite(led, LOW);
lubdub = !lubdub;
break;
case 0:
Serial.print("dub ");
// Serial.println(rightPeak);
cnt++;
Serial.println(cnt);
myDelay(delaytime);
lubdub = !lubdub;
break;
}
}
}
}
}
void myDelay(unsigned long duration) { //my own delay to uses millis
unsigned long starter = millis();
while (millis() - starter <= duration)
{
// do nothing but wait
}
}