Dear Arduino friends,
I am currently working on my fifth Arduino project, the second with the Catalex Micro SD Card Adapter Module v1.0 11/01/2013 - which seems to save whenever it wants to, kind of heterogeneous in what it is doing. I had the same difficulties with the last project that includes this module. I have to admit that I am a beginner though.
In my sketch I added the variable "LOOPS_TO_SAVE_DATA" to define the loops when collected data from twi sensors should be saved. The SD card module always is initialized successfully when starting the Arduino and it does its job at the beginning, however it always stops working after a random number of loops. The number of loops it works fine differs without having a correlation, e.g. sometimes it works just for three data savings, another time (after resetting) for twenty - the maximum (after working with it the last three weeks) was 199 records. I shrank the code to another sketch to test it, even without the command "LOOPS_TO_SAVE_DATA". Same problem.
#define PROGRAM "1704X15H"
#define LOOPS_TO_SAVE_DATA 1 // Loops to save data to SD card [1-60 recommended]
#include <SD.h>
#include <SPI.h>
int chipSelect = 4; // Set chipSelect = 4;
File mySensors;
int z = 0; // Variable to save data to SDcard
int loops = 0; // Counting loops for test purposes
void setup(void) {
Serial.begin(9600);
pinMode(10, OUTPUT); // Reserve pin 10 as an OUTPUT (SDcard)
SD.begin(chipSelect); // Initialize SDcard with chipSelect on pin 4
}
void loop(void) {
mySensors = SD.open("data.txt", FILE_WRITE);
if ((mySensors && z == 0) || (mySensors && z >= LOOPS_TO_SAVE_DATA)) {
Serial.print("Sensor data successfully saved! Loops: ");
Serial.println(loops);
mySensors.print(PROGRAM);
mySensors.print(",");
mySensors.println(loops);
// [All data...]
mySensors.close();
z = 1;
} else if ((mySensors && z >= 1) && (z < LOOPS_TO_SAVE_DATA)) {
Serial.print("Sensor data will be saved each ");
Serial.print(LOOPS_TO_SAVE_DATA);
Serial.print(F(" loops. Current loop: "));
Serial.println(z);
z++;
} else if (z > LOOPS_TO_SAVE_DATA) {
Serial.print("Sensor data is NOT saved as it should!");
z++;
} else {
Serial.print("Sensor data is NOT saved!");
z++;
}
loops++;
delay(1000);
}
I formated the card with SD-Formatter 4.0; I tried a 471 ceramic capacitor between VCC and GND, another 100uF to support my Arduino Nano in general as I read on a website that this helped someone else (Interfacing SD card's to the Arduino and stability - a few tips..... - Storage - Arduino Forum). Still the same. After spending lots of hours to figure out any correlation I am pretty close to through it in the trash can, wondering why so little trouble shooting for this specific module is on the web. Has somebody any idea why it saves data but always randomly stops doing so?
Any suggestion is very much appreciated.