Wav Recorder - Incrementing and Decrementing Recording File Names on to SD card

Heya, Ive been working on an arduino recorder / player on the arduino Mega with the TMRcpm Library,

Heres how it works (in my mind).

  • When turned on, the arduino goes through the files on the sd card and figures out which is the last file number so it doesnt overwrite anything when the recorder is turned off and on again after use.
    -Pressing the record button creates a new file incremented from the last found on card.
    -Pressing record again, stops the recording and allows playback.
    -The forward and back buttons allow the selection of respective file numbers to playback

  • ISSUE:

When selecting the track and playing the track selected, the play button plays the next track instead of the selected file.

Ive been going over things but cant seem to locate the issue. I know its something about the way Ive structured the increment / decrement code.

If you can see where Ive gone wrong, please give me a hand as this is the last piece of the puzzle for a weird steampunk recorder thingo.

Theres a bunch of code for the LED of the buttons, changing strings into character arrays etc, but the main problem is that increment decrement. The mega can handle the extra fluff of code so thats not an issue right now.

Photo of the setup is attached

Thank you so much for your attention and help!

#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 53
TMRpcm audio;

bool recMode = 0;
bool record = false;
char trackNameArray[8];
char selectTrackNameArray[8];
int trackNumber = -1;
int selectTrackNumber = -1;
int fileBuffer = 0;
bool sync = false;

const int recButtonPin = 42; //Blue
const int recLedPin =  43; //White-Blue

const int playButtonPin = 32;
const int playLedPin =  33;
int playButtonState = 0;

const int stopButtonPin = 34;
const int stopLedPin =  35;
int stopButtonState = 0;

const int previousButtonPin = 36;
const int previousLedPin =  37;
int previousButtonState = 0;

const int nextButtonPin = 38;
const int nextLedPin =  39;
int nextButtonState = 0;

void setup() {

  Serial.begin(9600);

  //AudioWrite Inputs

  pinMode(A0, INPUT); //MIC


  pinMode(playLedPin, OUTPUT); //play led
  pinMode(playButtonPin, INPUT_PULLUP); //play button

  pinMode(stopLedPin, OUTPUT); //stop led
  pinMode(stopButtonPin, INPUT_PULLUP); //stop button

  pinMode(previousLedPin, OUTPUT); //previous led
  pinMode(previousButtonPin, INPUT_PULLUP); //previous button

  pinMode(nextLedPin, OUTPUT); //next led
  pinMode(nextButtonPin, INPUT_PULLUP); //next button

  pinMode(recLedPin, OUTPUT); //rec Led
  pinMode(recButtonPin, INPUT_PULLUP); //rec button

  audio.CSPin = SD_ChipSelectPin;

  //SD Card Sync

  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // Audio Settings

  audio.speakerPin = 11; //11 on Mega, 9 on Uno, Nano, etc
  audio.volume(5);

}

void loop() {

  // File Synchronisation

  while (!sync) {

    if (SD.exists(String(fileBuffer) + ".WAV")) { //begins with 0.wav

      fileBuffer++;
      Serial.println(String(fileBuffer) + " loaded files"); //increment file number reading
      sync = false;

    } else {
      trackNumber = fileBuffer;
      selectTrackNumber = 0;
      Serial.println(String(fileBuffer) + " finalised files");
      sync = true;
    }
  }



  //// File Name and Selector Name Constructors

  String playString = String(selectTrackNumber) + ".wav"; //selectTrackNumber is how many files are loaded
  playString.toCharArray(selectTrackNameArray, 8);

  //// Button Controls

  if (digitalRead(recButtonPin) == LOW) {
    delay(100);
    digitalWrite(recLedPin, HIGH);
    while (record == false) {
      audio.stopPlayback();
      recMode = 1;


      ++trackNumber;
      ++selectTrackNumber;

      String recString = String(trackNumber) + ".wav";
      recString.toCharArray(trackNameArray, 8);
      Serial.println("recording " + String(trackNameArray));
      audio.startRecording(trackNameArray, 16000, A0);

      digitalWrite(recLedPin, LOW);
      record = true;
    }
  }

  if (digitalRead(recButtonPin) == HIGH) {
    delay(100);
    digitalWrite(recLedPin, LOW);

    while (record == true) {
      audio.stopRecording(trackNameArray);
      Serial.println("Recording of " + String(trackNameArray) + " stopped");
      record = false;
      recMode = 0;
    }
  }

  //Play


  playButtonState = digitalRead(playButtonPin);

  if (recMode == 0) {
    if (playButtonState == HIGH) {
      digitalWrite(playLedPin, HIGH);
    } else {
      digitalWrite(playLedPin, LOW);
      delay(100);
      audio.play(selectTrackNameArray);
      Serial.println("Playing " + String(selectTrackNameArray));

    }
  }

  //Stop

  stopButtonState = digitalRead(stopButtonPin);

  if (recMode == 0) {
    if (stopButtonState == HIGH) {
      digitalWrite(stopLedPin, HIGH);
    } else {
      digitalWrite(stopLedPin, LOW);
      delay(100);
      Serial.println("Playback Stopped");
      audio.stopPlayback();
    }
  }



  //Next

  nextButtonState = digitalRead(nextButtonPin);

  if (nextButtonState == HIGH)  {
    digitalWrite(nextLedPin, HIGH);
  }

  if ((nextButtonState == LOW) && (selectTrackNumber < trackNumber)) {
    digitalWrite(nextLedPin, LOW);
    delay(200);
    ++selectTrackNumber;
    Serial.println("Selected " + String(selectTrackNameArray));
  }




  //Previous

  previousButtonState = digitalRead(previousButtonPin);

  if ((previousButtonState == HIGH) && (selectTrackNumber <= trackNumber)) { //if 5 < 8 then increment, if 8 < 8 then keep at the level of trackNumber till that increases
    digitalWrite(previousLedPin, HIGH);
  } else {
    digitalWrite(previousLedPin, LOW);
    delay(200);
    --selectTrackNumber;
    Serial.println("Selected " + String(selectTrackNameArray));
  }


} //End Loop