|
#include <WiFiNINA.h>
#include <PDM.h>
static const char channels = 1;
static const int frequency = 16000;
short sampleBuffer[512];
volatile int samplesRead;
void setup() {
Serial.begin(9600);
while (!Serial);
PDM.onReceive(onPDMdata);
if (!PDM.begin(channels, frequency)) {
Serial.println("Failed to start PDM!");
while (1);
}
}
void showMicData(short data) {
Serial.println(data);
}
void micHandle() {
if (samplesRead) {
for (int i = 0; i < samplesRead; i++) {
showMicData(sampleBuffer[i]);
}
samplesRead = 0;
}
}
void loop() {
micHandle();
}
void onPDMdata() {
int bytesAvailable = PDM.available();
PDM.read(sampleBuffer, bytesAvailable);
samplesRead = bytesAvailable / 2;
}
总结