arduino audio recording and playback

both recording and playback working fine separately when combine together no errors found but it does record and playback…can anyone please let me know what I have to change in this code…

// Include libraries
#include <SdFat.h>
#include <TMRpcm.h>
TMRpcm tmrpcm;           
#include <SPI.h>
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

// SD card objects
SdFat sd;
SdFile rec;

// Constants
const int chipSelect = 4;
unsigned long fileSize = 0L;
unsigned long waveChunk = 16;
unsigned int waveType = 1;
unsigned int numChannels = 1;
unsigned long sampleRate = 22050;
unsigned long bytesPerSec = 22050;
unsigned int blockAlign = 1;
unsigned int bitsPerSample = 8;
unsigned long dataSize = 0L;
unsigned long recByteCount = 0L;
unsigned long recByteSaved = 0L;

const int ledStart = 7;

int recPressed = 0;
int stopPressed = 0;

unsigned long oldTime = 0L;
unsigned long newTime = 0L;

// Buffers
byte buf00[512]; // buffer array 1
byte buf01[512]; // buffer array 2
byte byte1, byte2, byte3, byte4;
unsigned int bufByteCount;
byte bufWrite;

// Counter
byte recordingEnded = false;
unsigned int counter;
unsigned int initial_count;
unsigned int maxCount = 10 * 1000; // 10 Seconds

void setup() { // THIS RUNS ONCE
tmrpcm.speakerPin = 9;
 // Setup timer & ADC
 Setup_timer2();
 Setup_ADC();

 // Set output pins
 pinMode(10, OUTPUT);
 pinMode(ledStart, OUTPUT);

 // Init SD card
 if (sd.begin(chipSelect, SPI_FULL_SPEED)) {
   for (int dloop = 0; dloop < 4; dloop++) {
     digitalWrite(ledStart,!digitalRead(ledStart));
     delay(100);
   }
 }

 // If error
 else { 
   while(1) {
     digitalWrite(ledStart,!digitalRead(ledStart));
     delay(500);
   }
 }

 // Start recording
 StartRec();

 // Init counter
 initial_count = millis();

}

void loop() { // THIS RUNS LOTS!

 // Update counter
 counter = millis() - initial_count;

 // Stop recording if counter reached
 if (counter > maxCount && !recordingEnded) {
   recordingEnded = true;
   StopRec();  
 }

 // Update recording
 if (recByteCount % 1024 == 512 && recPressed == 1) { rec.write(buf00,512); recByteSaved+= 512; } // save buf01 to card
 if (recByteCount % 1024 == 0 && recPressed == 1) { rec.write(buf01,512); recByteSaved+= 512; } // save buf02 to card

}

void StartRec() { // begin recording process

 digitalWrite(ledStart,HIGH);
 recByteCount = 0;
 recByteSaved = 0;
 recPressed = 1; // recording button has been pressed
 stopPressed = 0;
 writeWavHeader();
 sbi (TIMSK2, OCIE2A); // enable timer interrupt, start grabbing audio

}

void StopRec() { // stop recording process, update WAV header, close file
   
 cbi (TIMSK2, OCIE2A); // disable timer interrupt
 writeOutHeader();
 digitalWrite(ledStart,LOW); // turn off recording LED
 recPressed = 0;
 tmrpcm.play("rec0000.wav");
}
 
void writeOutHeader() { // update WAV header with final filesize/datasize

 rec.seekSet(4);
 byte1 = recByteSaved & 0xff;
 byte2 = (recByteSaved >> 8) & 0xff;
 byte3 = (recByteSaved >> 16) & 0xff;
 byte4 = (recByteSaved >> 24) & 0xff;  
 rec.write(byte1);  rec.write(byte2);  rec.write(byte3);  rec.write(byte4);
 rec.seekSet(40);
 rec.write(byte1);  rec.write(byte2);  rec.write(byte3);  rec.write(byte4);
 rec.close();
 
}

void writeWavHeader() { // write out original WAV header to file

 recByteSaved = 0;
 rec.open("rec00000.wav", O_CREAT | O_TRUNC | O_RDWR);
 rec.write("RIFF");
 byte1 = fileSize & 0xff;
 byte2 = (fileSize >> 8) & 0xff;
 byte3 = (fileSize >> 16) & 0xff;
 byte4 = (fileSize >> 24) & 0xff;  
 rec.write(byte1);  rec.write(byte2);  rec.write(byte3);  rec.write(byte4);
 rec.write("WAVE");
 rec.write("fmt ");
 byte1 = waveChunk & 0xff;
 byte2 = (waveChunk >> 8) & 0xff;
 byte3 = (waveChunk >> 16) & 0xff;
 byte4 = (waveChunk >> 24) & 0xff;  
 rec.write(byte1);  rec.write(byte2);  rec.write(byte3);  rec.write(byte4);
 byte1 = waveType & 0xff;
 byte2 = (waveType >> 8) & 0xff;
 rec.write(byte1);  rec.write(byte2); 
 byte1 = numChannels & 0xff;
 byte2 = (numChannels >> 8) & 0xff;
 rec.write(byte1);  rec.write(byte2); 
 byte1 = sampleRate & 0xff;
 byte2 = (sampleRate >> 8) & 0xff;
 byte3 = (sampleRate >> 16) & 0xff;
 byte4 = (sampleRate >> 24) & 0xff;  
 rec.write(byte1);  rec.write(byte2);  rec.write(byte3);  rec.write(byte4);
 byte1 = bytesPerSec & 0xff;
 byte2 = (bytesPerSec >> 8) & 0xff;
 byte3 = (bytesPerSec >> 16) & 0xff;
 byte4 = (bytesPerSec >> 24) & 0xff;  
 rec.write(byte1);  rec.write(byte2);  rec.write(byte3);  rec.write(byte4);
 byte1 = blockAlign & 0xff;
 byte2 = (blockAlign >> 8) & 0xff;
 rec.write(byte1);  rec.write(byte2); 
 byte1 = bitsPerSample & 0xff;
 byte2 = (bitsPerSample >> 8) & 0xff;
 rec.write(byte1);  rec.write(byte2); 
 rec.write("data");
 byte1 = dataSize & 0xff;
 byte2 = (dataSize >> 8) & 0xff;
 byte3 = (dataSize >> 16) & 0xff;
 byte4 = (dataSize >> 24) & 0xff;  
 rec.write(byte1);  rec.write(byte2);  rec.write(byte3);  rec.write(byte4);

}

void Setup_timer2() {

 TCCR2B = _BV(CS21);  // Timer2 Clock Prescaler to : 8
 TCCR2A = _BV(WGM21); // Interupt frequency  = 16MHz / (8 x 90 + 1) = 22191Hz
 OCR2A = 90; // Compare Match register set to 90

}

void Setup_ADC() {

 ADMUX = 0x65; // set ADC to read pin A5, ADLAR to 1 (left adjust)
 cbi(ADCSRA,ADPS2); // set prescaler to 8 / ADC clock = 2MHz
 sbi(ADCSRA,ADPS1);
 sbi(ADCSRA,ADPS0);
}

ISR(TIMER2_COMPA_vect) {

 sbi(ADCSRA, ADSC); // start ADC sample
 while(bit_is_set(ADCSRA, ADSC));  // wait until ADSC bit goes low = new sample ready
 recByteCount++; // increment sample counter
 bufByteCount++;
 if (bufByteCount == 512 && bufWrite == 0) { 
   bufByteCount = 0;
   bufWrite = 1;
 } else if (bufByteCount == 512 & bufWrite == 1) {
   bufByteCount = 0;
   bufWrite = 0;
 }

 if (bufWrite == 0) { buf00[bufByteCount] = ADCH; }
 if (bufWrite == 1) { buf01[bufByteCount] = ADCH; }
 

//  if (recByteCount % 1024 < 512) { // determine which buffer to store sample into
//    buf01[recByteCount % 512] = ADCH;
//  } else {
//    buf02[recByteCount % 512] = ADCH;
//  }

}

See the funny yellow faces with sunglasses? You have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

Which Arduino are you using? If it is a 328-based Arduino, you are likely using more than all the memory. 512 bytes for the SdFile object and 2 512 byte buffers use 3/4 of the SRAM that the 328-based Arduinos have.

Hi Paul

Thank you very much for your reply ..yes i am using UNO 328 only...if I use mega will it be working..

Hi aarg

Sorry for that. this is the first time i am posting in this forum. thanks for your advise.

if I use mega will it be working..

Well, you certainly stand a better chance.

thank you paul..

what change should I made in the code to keep the recording audio for only two minutes loop...

3000eleck: thank you paul..

what change should I made in the code to keep the recording audio for only two minutes loop...

 // Stop recording if counter reached
 if (counter > maxCount && !recordingEnded) {
   recordingEnded = true;
   StopRec();  
 }

To record for longer, the value of maxCount needs to be higher, doesn't it?

unsigned int maxCount = 10 * 1000; // 10 Seconds

Feel free to whack yourself with a clue-by-four.

I have done that..when I give value of maxcount 3000 it would give 47 sec of audio,if I more than that the audio file would be corrupted...Thanks

3000eleck: I have done that..when I give value of maxcount 3000 it would give 47 sec of audio,if I more than that the audio file would be corrupted...Thanks

I guess I fail to see how 10000 gives 10 seconds and 3000 gives 47 seconds. Must be dense, I guess.