Hi,
I want my Arduino Nano to record my voice (and light up a LED), while I press a button and save the output file (.wav) to the connected SD card.
At the beginning, the code works perfectly, but after a while (~ 30 min. - 1,5h) it just stops running my function. So I press the button and nothing happens. After a while it will work again without a reset of the code. How can I ensure to be able to record my voice at any time?
If I modify the code to print something in the loop(), I can see that the program is still running without a reset, but just doesn't register my button presses.
I tried different boards, different power supplies, the wiring should be correct, because it works - until it randomly decides to stop working. No changes eliminated the "breaks" and I get really frustrated troubleshooting, because the "break"s appear after quite some different times..
Maybe someone can find the problem in my code or the wiring setup? Or is the Arduino Nano not suitable for this project?
Any help to solve my problem is much appreciated, thank you in advance!
// add necessary libraries
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
// create global variables
#define SD_ChipSelectPin 10
long debouncing_time = 250; // debouncing time in milliseconds
volatile unsigned long last_micros;
TMRpcm audio;
volatile int file_number = 0;
volatile bool recording_now = false;
const int button_pin = 2;
const int recording_led_pin = 3;
const int mic_pin = A0;
const int sample_rate = 16000;
void record() {
// name the file
char file_name[20] = "";
itoa(file_number,file_name,10);
strcat(file_name,".wav");
if (!recording_now) {
// not recording --> start recording and turn LED on
recording_now = true;
digitalWrite(recording_led_pin, HIGH);
audio.startRecording(file_name, sample_rate, mic_pin);
}
else {
// if recording --> stops recording & turns LED off
recording_now = false;
digitalWrite(recording_led_pin, LOW);
audio.stopRecording(file_name);
// Serial.println(file_name);
file_number++;
}
}
void button_pushed() { // if button state changes
if((long)(micros() - last_micros) >= debouncing_time * 1000) { // if last change is out of debouncing time
record();
last_micros = micros();
}
}
void setup() {
// initialise serial connection between arduino and serial device
Serial.begin(9600);
Serial.println("loading...");
randomSeed(analogRead(0));
// pin setup
pinMode(mic_pin, INPUT);
pinMode(recording_led_pin, OUTPUT);
pinMode(button_pin, INPUT_PULLUP);
// audio recording functionality
attachInterrupt(digitalPinToInterrupt(button_pin), button_pushed, CHANGE); // interrupt if button state changes
SD.begin(SD_ChipSelectPin);
audio.CSPin = SD_ChipSelectPin;
}
void loop() {
//
}
