Hello everyone, I'm experiencing an issue with writing two files to an SD card. I have some simple code that prompts you with three questions and expects three answers. These answers, along with some sensor data, are stored in a CSV file on the SD card. Additionally, a relay is activated and audio is recorded, which also needs to be saved to the SD card.
The problem arises when both saving functions are combined in a single code. An error message stating 'Error saving file!' occurs. Interestingly, this error occurs when the CSV file is being created, even though the audio file hasn't been generated yet. It's puzzling why the CSV writing function, which worked perfectly fine in a separate code, is now causing issues. It seems to fail during the call to the CSV writing function, even before the second file operation is performed.
I attempted to initialize and open the SD card in each function separately, ensuring that it is closed before the other function is called. I even tried creating a new instance for the second operation, but unfortunately, these attempts had no effect on resolving the issue.
Furthermore, when both functions are present in the code and only the CSV writing function is called, the code works without any issues. However, when both functions are included in the code and both are called, the execution stops at the first function.
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <SD.h>
#include <TMRpcm.h>
// BME280 Sensor Setup
Adafruit_BME280 bme;
// Global Variables
int questionNumber = 1;
String answer1 = "";
String answer2 = "";
String answer3 = "";
// Relay Setup
const int relayPin = 8;
const int mic_pin = A3;
const int sample_rate = 16000;
TMRpcm audio;
int file_number = 0;
bool recording_now = false;
// Function to save the data to the SD card
void saveData(const char* filename, const String& answer1, const String& answer2, const String& answer3, float temperature, float humidity, float pressure) {
// Initialize the SD card
if (!SD.begin(10)) {
Serial.println("SD Card initialization failed!");
while (1); // Stop the program
}
// Open the file in write mode
File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile) {
unsigned long currentMillis = millis(); // Get the current timestamp
dataFile.print(currentMillis);
dataFile.print(",");
dataFile.print(answer1);
dataFile.print(",");
dataFile.print(answer2);
dataFile.print(",");
dataFile.print(answer3);
dataFile.print(",");
dataFile.print(temperature);
dataFile.print(",");
dataFile.print(humidity);
dataFile.print(",");
dataFile.println(pressure);
dataFile.close(); // Close the file
} else {
Serial.println("Error saving file!");
delay(2000); // Display error message for 2 seconds
}
// Close the SD card connection
SD.end();
}
void start_recording() {
// Opens the SD card
if (SD.begin(10)) {
// Combines the necessary info to get a char array of the file name
char file_nam[20] = "hellothere";
itoa(file_number, file_nam, 10);
strcat(file_nam, ".wav");
// Starts recording and turns LED on
recording_now = true;
audio.CSPin = 10;
audio.startRecording(file_nam, sample_rate, mic_pin);
Serial.println(file_nam);
// Stops recording after 10 seconds
delay(10000);
audio.stopRecording(file_nam);
file_number++;
recording_now = false;
// Closes the SD card
SD.end();
}
}
void setup() {
Serial.begin(9600); // Initialize the serial communication
pinMode(mic_pin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
// Initialize the BME280 sensor
if (!bme.begin(0x76)) {
Serial.println("BME280 initialization failed!");
while (1); // Stop the program
}
}
void loop() {
if (questionNumber == 1) {
Serial.println("Enter Cannister:");
} else if (questionNumber == 2) {
Serial.println("Enter Row:");
} else if (questionNumber == 3) {
Serial.println("Enter Level:");
}
while (Serial.available() == 0) {
// Wait for input from the Serial Monitor
}
char key = Serial.read();
if (key == '#') {
questionNumber++;
if (questionNumber > 3) {
questionNumber = 1; // Reset the question number for the next iteration
// Read BME280 sensor data
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0; // Convert pressure to hPa
// Save data to CSV file
char filename[] = "data.csv";
saveData(filename, answer1, answer2, answer3, temperature, humidity, pressure);
// Reset the answer variables
answer1 = "";
answer2 = "";
answer3 = "";
digitalWrite(relayPin, HIGH); // Activate the relay
delay(50); // Activation time of the relay
digitalWrite(relayPin, LOW); // Deactivate the relay
delay(2000);
//start_recording();
//RVK(); // Call the RVK function
}
} else if (isdigit(key) || key == '.') {
if (questionNumber == 1) {
answer1 += key;
} else if (questionNumber == 2) {
answer2 += key;
} else if (questionNumber == 3) {
answer3 += key;
}
}
}