add digitalPinToPinName(P) (arduino/mbed has it) and the adapted PDM library
This commit is contained in:
1 parent
8628008fb5
commit
d3f3b34319
9 files changed
+575
-1
No files matched your search
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
This example reads audio data from the on-board PDM microphones, and prints
|
||||
out the samples to the Serial console. The Serial Plotter built into the
|
||||
Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)
|
||||
|
||||
Please try with the Circuit Playground Bluefruit
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
#include <PDM.h>
|
||||
|
||||
// The default Circuit Playground Bluefruit pins
|
||||
// data pin, clock pin, power pin (-1 if not used)
|
||||
PDMClass PDM(33, 34, -1);
|
||||
|
||||
// buffer to read samples into, each sample is 16-bits
|
||||
short sampleBuffer[256];
|
||||
|
||||
// number of samples read
|
||||
volatile int samplesRead;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
|
||||
// configure the data receive callback
|
||||
PDM.onReceive(onPDMdata);
|
||||
|
||||
// optionally set the gain, defaults to 20
|
||||
// PDM.setGain(30);
|
||||
|
||||
// initialize PDM with:
|
||||
// - one channel (mono mode)
|
||||
// - a 16 kHz sample rate
|
||||
if (!PDM.begin(1, 16000)) {
|
||||
Serial.println("Failed to start PDM!");
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// wait for samples to be read
|
||||
if (samplesRead) {
|
||||
|
||||
// print samples to the serial monitor or plotter
|
||||
for (int i = 0; i < samplesRead; i++) {
|
||||
Serial.println(sampleBuffer[i]);
|
||||
}
|
||||
|
||||
// clear the read count
|
||||
samplesRead = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void onPDMdata() {
|
||||
// query the number of bytes available
|
||||
int bytesAvailable = PDM.available();
|
||||
|
||||
// read into the sample buffer
|
||||
PDM.read(sampleBuffer, bytesAvailable);
|
||||
|
||||
// 16-bit, 2 bytes per sample
|
||||
samplesRead = bytesAvailable / 2;
|
||||
}
|
||||
Reference in new issue
Block a user