philspitler
Member
Hi, I'm using the audio peek function to do beat detection and i am getting a false read.
Basically I want to blink an LED every other beat so I detect the beats and then use case: to either turn on the LED or ignore the beat.
The sounds I am using as my test has a beat of around 180ms then a gap of 240ms before the next beat (see image attached).
So, in theory I should be able to detect a peek then wait for 200ms before waiting for the next peek.
When I do have a 200ms delay and process this sound, the sketch detects 4 beats instead of 2.
With a delay of 400ms the sketches detects 3 beats.
If I up the delay to 500ms then it only detects the 2 beats.

Basically I want to blink an LED every other beat so I detect the beats and then use case: to either turn on the LED or ignore the beat.
The sounds I am using as my test has a beat of around 180ms then a gap of 240ms before the next beat (see image attached).
So, in theory I should be able to detect a peek then wait for 200ms before waiting for the next peek.
When I do have a 200ms delay and process this sound, the sketch detects 4 beats instead of 2.
With a delay of 400ms the sketches detects 3 beats.
If I up the delay to 500ms then it only detects the 2 beats.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
int led = 2;
int thresholdlevel = 15; // range 0 to 30
const int myInput = AUDIO_INPUT_LINEIN;
int delaytime = 500; //delay time in milliseconds
boolean lubdub = true;
AudioInputI2S audioInput; // audio shield: mic or line-in
AudioAnalyzePeak peak_R;
AudioConnection c2(audioInput, 1, peak_R, 0);
AudioControlSGTL5000 audioShield;
void setup() {
pinMode(led, OUTPUT);
AudioMemory(6);
audioShield.enable();
audioShield.inputSelect(myInput);
audioShield.volume(1);
Serial.begin(115200);
Serial.println("starting...");
}
elapsedMillis fps;
uint8_t cnt = 0;
void loop() {
if (fps > 24) {
if (peak_R.available()) {
fps = 0;
uint8_t rightPeak = peak_R.read() * 30.0;
if ( rightPeak > thresholdlevel) {
switch (lubdub) {
case 1:
digitalWrite(led, HIGH);
Serial.print("lub ");
cnt++;
myDelay(delaytime);
digitalWrite(led, LOW);
lubdub = !lubdub;
break;
case 0:
Serial.print("dub ");
Serial.println(cnt);
myDelay(delaytime);
lubdub = !lubdub;
break;
}
}
else {
digitalWrite(led, LOW);
}
}
}
}
void myDelay(unsigned long duration) { //my own delay to uses millis
unsigned long starter = millis();
while (millis() - starter <= duration)
{
// do nothing but wait
}
}

