1

I am working on a circuit that can record audio to an SD when a switch is HIGH and stop recording when the switch is LOW. I am using the TMRpcm library. I previously had an issue solved on these two forumsRecording Audio - Film Name Errorand Trying to save .wav files with new name every loop. These solutions solved my problem but have left me with a new one. Currently the audio recorded on the new code (which I have pasted below) results is a very noisy and muffled audio quality; while the audio recorded on the old code (also posted below) is very clear. Could this be a physical issue and if so what should I do about it? Or is it an issue I am missing in the code?

OLD CODE - provides good quality audio

//////////////////////////////////////// SD CARD
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10
TMRpcm audio;
//////////////////////////////////////// SWITCH CASE
int audiofile = 0;     // # of recording
unsigned long i = 0;
bool recmode = 0;      // recording state
//////////////////////////////////////// SWITCH
int inPin = 2;         // input Switch Pin
int state = HIGH;      // the current state switch
int reading;           // the current reading from the switch

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);  // Microphone
  pinMode(inPin, INPUT_PULLUP); // Switch
  //////////////////////////////////////// SD CARD
  SD.begin(SD_ChipSelectPin);
  audio.CSPin = SD_ChipSelectPin;
}

void loop() {

  reading = digitalRead(inPin);
  ////////////////////////////////////////
  while (i < 300000) {
    i++;
  }
  i = 0;
  ////////////////////////////////////////
  if (reading == LOW) {
    if (recmode == 0) {
      recmode = 1;

      Serial.println("Recording");

      audiofile++; // To move case
      switch (audiofile) {
        case 1: audio.startRecording("1.wav", 16000, A0); break;
        case 2: audio.startRecording("2.wav", 16000, A0); break;
        case 3: audio.startRecording("3.wav", 16000, A0); break;
        case 4: audio.startRecording("4.wav", 16000, A0); break;
        case 5: audio.startRecording("5.wav", 16000, A0); break;
        case 6: audio.startRecording("6.wav", 16000, A0); break;
        case 7: audio.startRecording("7.wav", 16000, A0); break;
        case 8: audio.startRecording("8.wav", 16000, A0); break;
        case 9: audio.startRecording("9.wav", 16000, A0); break;
        case 10: audio.startRecording("10.wav", 16000, A0); break;
      }
    }
  }
  ////////////////////////////////////////
  else if (reading == HIGH) {
    recmode = 0;

    Serial.println("Hung-Up");
    switch (audiofile) {
      case 1: audio.stopRecording("1.wav"); break;
      case 2: audio.stopRecording("2.wav"); break;
      case 3: audio.stopRecording("3.wav"); break;
      case 4: audio.stopRecording("4.wav"); break;
      case 5: audio.stopRecording("5.wav"); break;
      case 6: audio.stopRecording("6.wav"); break;
      case 7: audio.stopRecording("7.wav"); break;
      case 8: audio.stopRecording("8.wav"); break;
      case 9: audio.stopRecording("9.wav"); break;
      case 10: audio.stopRecording("10.wav"); break;
    }
  }
}

NEW CODE - poor quality audio

//////////////////////////////////////// SD CARD
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10
TMRpcm audio;
//////////////////////////////////////// SWITCH CASE
unsigned int i = 0;     // # of recording
unsigned long d = 0;
bool recmode = 0;      // recording state
//////////////////////////////////////// SWITCH
int inPin = 2;         // input Switch Pin
int state = HIGH;      // the current state switch
int reading;           // the current reading from the switch

void setup() {
  //Serial.begin(9600);
  pinMode(A0, INPUT);  // Microphone
  pinMode(inPin, INPUT_PULLUP); // Switch
  //////////////////////////////////////// SD CARD
  SD.begin(SD_ChipSelectPin);
  audio.CSPin = SD_ChipSelectPin;
}

void loop() {
  char buffer[50];
  sprintf(buffer, "%i.wav", i++);
  reading = digitalRead(inPin);
  ////////////////////////////////////////
  while (d < 300000) {
    d++;
  }
  d = 0;
  ////////////////////////////////////////
  if (reading == LOW) {
    if (recmode == 0) {
      recmode = 1;

      //Serial.println("Recording");

      audio.startRecording(buffer, 1600, A0);
      //Serial.println(buffer);
    }
  }
  ////////////////////////////////////////
  else if (reading == HIGH) {
    recmode = 0;

    //Serial.println("Hung-Up");

    audio.stopRecording(buffer);
  }
}
2

Old code:

audio.startRecording("1.wav", 16000, A0);

New code:

audio.startRecording(buffer, 1600, A0);

Spot the typo?

  • Thank you, I have no idea how I missed that (16000) – TSauer52 Dec 28 '18 at 1:07
  • Easily done. It just takes a fresh pair of eyes. – Majenko Dec 28 '18 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.