voidsetup(){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);}}
// check if the sound value is higher than 500
if(sampleBuffer[i]>=500){digitalWrite(LEDR,LOW);digitalWrite(LEDG,HIGH);digitalWrite(LEDB,HIGH);}// check if the sound value is higher than 250 and lower than 500
if(sampleBuffer[i]>=250&&sampleBuffer[i]<500){digitalWrite(LEDB,LOW);digitalWrite(LEDR,HIGH);digitalWrite(LEDG,HIGH);}//check if the sound value is higher than 0 and lower than 250
if(sampleBuffer[i]>=0&&sampleBuffer[i]<250){digitalWrite(LEDG,LOW);digitalWrite(LEDR,HIGH);digitalWrite(LEDB,HIGH);}
/*
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)
Circuit:
- Arduino Nano 33 BLE Sense board
This example code is in the public domain.
*/#include<PDM.h>// buffer to read samples into, each sample is 16-bits
shortsampleBuffer[256];// number of samples read
volatileintsamplesRead;voidsetup(){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);}}voidloop(){// wait for samples to be read
if(samplesRead){// print samples to the serial monitor or plotter
for(inti=0;i<samplesRead;i++){Serial.println(sampleBuffer[i]);// check if the sound value is higher than 500
if(sampleBuffer[i]>=500){digitalWrite(LEDR,LOW);digitalWrite(LEDG,HIGH);digitalWrite(LEDB,HIGH);}// check if the sound value is higher than 250 and lower than 500
if(sampleBuffer[i]>=250&&sampleBuffer[i]<500){digitalWrite(LEDB,LOW);digitalWrite(LEDR,HIGH);digitalWrite(LEDG,HIGH);}//check if the sound value is higher than 0 and lower than 250
if(sampleBuffer[i]>=0&&sampleBuffer[i]<250){digitalWrite(LEDG,LOW);digitalWrite(LEDR,HIGH);digitalWrite(LEDB,HIGH);}}// clear the read count
samplesRead=0;}}voidonPDMdata(){// query the number of bytes available
intbytesAvailable=PDM.available();// read into the sample buffer
PDM.read(sampleBuffer,bytesAvailable);// 16-bit, 2 bytes per sample
samplesRead=bytesAvailable/2;}