I have built a set up with an Arduino UNO hooked up to a switch, so that whenever the switch goes from HIGH to LOW it saves audio to an SD card. I am using the TMRpcm Library. The problem is the current code will only be able to record 1 audio file; I want to the Arduino to run for a long period of time collecting multiple audio files as the switch is turned on and off. I need a way that will allow it so everytime the switch is pushed back down the next file saved will have a different name than the one previously saved (preferably by an increased numeric value eg "1.wav" & "2.wav"). I have tried audio.startRecording(audiofile+".wav", 16000, A0); and it makes it so no files save to the SD card accept an empty file simple titled 'WAV'. I have tried using a switch case but it will not allow me to reach a case over 10, without running out of RAM. Currently, my code can save 1 audio file but I want it to reach a point where it can save as many as the SD card can hold, without having to restart the software.
My current code is here. Any help would be greatly appreciated
//////////////////////////////////////// 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
audio.startRecording("1.wav", 16000, A0);
}
}
////////////////////////////////////////
else if (reading == HIGH) {
recmode = 0;
Serial.println("Hung-Up");
audio.stopRecording("1.wav");
}
}
audiofile + ".WAV"
, because of the different types. But you can use achar
buffer andsprintf("%i.WAV",audiofile);