How to record audio and save it to wav file using Electret Mic Amp - MAX9814?

Hi @ScruffR, Thanks for the help. I managed to get the samples using this code

for (int i=0; i < SAMPLES_IN_BUFFER; i++)
   Serial.println(samples[i]);

Just an update, I manage to write the file inside my sd card using this example. Thanks @shanevanj .

However, I tried to understand the code in this thread and I only understand it a little bit.

This is what i have done.

// Simple ADC DMA example

// Repository: https://github.com/rickkas7/ADCDMAGen3_RK
// License: MIT (free for use in open or closed source products)

#include "Particle.h"
#include "ADCDMAGen3_RK.h"
#include "adc_hal.h"
#include "gpio_hal.h"
#include "pinmap_hal.h"
#include "pinmap_impl.h"
#include <SdFat.h>

void buttonHandler(system_event_t event, int data);

SdFat sd;
File myFile;
int fileCount=0;

int button=D2;
int buttonstate;

const int SPI_CS = A5;
const size_t SAMPLES_IN_BUFFER = 1024;
const size_t SAMPLE_FREQ = 16000; // Hz
const unsigned long MAX_RECORDING_LENGTH_MS = 10000;

enum State { STATE_WAITING, STATE_CONNECT, STATE_RUNNING, STATE_FINISH };
State state = STATE_WAITING;

unsigned long recordingStart;

// Print debug message to USB serial
SerialLogHandler logHandler;

// Works threaded or not
SYSTEM_THREAD(ENABLED);

// This is where the samples are stored
static nrf_saadc_value_t buffer0[SAMPLES_IN_BUFFER];
static nrf_saadc_value_t buffer1[SAMPLES_IN_BUFFER];
static nrf_saadc_value_t *bufferReady = 0;

ADCDMAGen3 adc;

void myBufferCallback(nrf_saadc_value_t *buf, size_t size);
int result = 0;

void setup() {
	// Optional, just for testing so I can see the logs below
	// waitFor(Serial.isConnected, 10000);
	pinMode(button, INPUT);
	Serial.begin(9600);

	if(sd.begin(SPI_CS, SPI_FULL_SPEED))
	//the sd is underlined.
		Serial.println("SD initialised");
	else
		Serial.println("failed to open card");

	ret_code_t err = adc
		.withSampleFreqHz(SAMPLE_FREQ)
		.withDoubleBuffer(SAMPLES_IN_BUFFER, buffer0, buffer1)
		.withSamplePin(A0)
		.withBufferCallback(myBufferCallback)
		.init();

	Log.info("adc.init %lu", err);

	adc.start();
}

void loop() {

buttonstate=digitalRead(button);

if(buttonstate = HIGH){
State state = STATE_CONNECT;
}
else
{
	switch(state){
		case STATE_WAITING:
		//// Waiting for the user to press the tactile switch
		break;

		case STATE_CONNECT:
			char fileName[128];
			snprintf(fileName, sizeof(fileName), "rec%04d.pcm", fileCount+1);
		if(myFile.open(fileName, O_RDWR | O_CREAT | O_TRUNC))
		{
			fileCount++;
			digitalWrite(D7,HIGH);
			Serial.printlnf("Writing to %s", fileName);
		  	recordingStart = millis();

			if (bufferReady) 
			{
				int16_t *samples = (int16_t *)bufferReady;
				bufferReady = 0;
			}
			state = STATE_RUNNING;
		}
		else 
		{
			Serial.printlnf("opening %s for write failed", fileName);
			state = STATE_WAITING;
	    }
		break;

		case STATE_RUNNING:
		//how to continue
		break;


		case STATE_FINISH:
		//how to continue
		break;
	}
}
}

void myBufferCallback(nrf_saadc_value_t *buf, size_t size) {
	// This gets executed after each sample buffer has been read.
	// Note: This is executed in interrupt context, so beware of what you do here!

	// We just remember the buffer and handle it from loop
	bufferReady = buf;
}

Am I on the right track? Is there anything wrong so far? How do I continue this since I'm not using Photon. What I want to change is to record the audio for 10 seconds when the tactile button is pressed. Then the data will be converted into wav file and sent to the sd card. Once again, thank you very much.:grin: