I used the below code (which is basically the one from post #119), speeding up the process by using delay(10) instead of delay(100) and adding a "done" when the process is complete.
// writing Test
#include <SD.h>
#include <SPI.h> //for the SD card module
String POWER = "";
const int chipSelect = A0;
int N = 0;
int Nmax = 16000;
File myFile;
//_______________________________________________
void setup() {
/* Debugging serial */
Serial.begin(9600);
SPI.begin();
SPI.setClockDivider(4);
Serial.println(F("Ini SD cardN.."));
if (!SD.begin(chipSelect)) {
Serial.println(F("ini failed!"));
while (1)
; // la ledOFF reste allumée en continu pas la ledRec verte
}
Serial.println(F("ini done."));
myFile = SD.open("TESTX3.txt", FILE_WRITE);
// lcd.setCursor(0, 0);
Serial.print("Welcome");
// lcd.setCursor(0, 1);
Serial.print("TEST");
}
//______________________________________________________
void loop() {
int heure = 22;
int MIN = 33;
int jour = 2;
delay(10);
float voltage = 200;
float current = 12;
float power = 11;
float energy = 12;
float frequency = 12;
float pf = 55;
char POWER[20]; //plenty of room for the output
sprintf(POWER, "%02d;%02d;%02d;", jour, heure, MIN);
N = N + 1;
// lcd.setCursor(0, 0);
Serial.print("Nb de lignes ");
Serial.println(N);
Serial.print(" POWER ");
Serial.print(POWER);
Serial.print(voltage);
Serial.print(",");
Serial.print(current);
Serial.print(",");
Serial.print(power);
Serial.print(",");
Serial.print(energy, 1);
Serial.print(",");
Serial.print(frequency, 1);
Serial.print(",");
Serial.println(pf);
delay(10);
if (N < Nmax) {
myFile.print(N);
myFile.print(" ");
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(POWER);
myFile.print(",");
myFile.print(voltage);
myFile.print(",");
myFile.print(current);
myFile.print(",");
myFile.print(power);
myFile.print(",");
myFile.print(energy, 3);
myFile.print(",");
myFile.print(frequency, 1);
myFile.print(",");
myFile.println(pf);
myFile.flush();
}
if (N > Nmax - 2) {
Serial.println("Done");
while (1)
;
}
}
// loop finish
and very strange also the fact that 12000loops are OK but if I restart new 12000loops with another file name, both are destroiyed
Below a screen capture
initial GOOD file was TESTXXA.txt
2nd should have been TESTXX.txt
Very far from that and file size inconsitant
I would start looking at the hardware. We're going around on circles.
Make sure that your wiring is solid. One intermittent contact can cause havoc. Breadboards can be a major cause of problems. If possible, solder.
I don't know if you have mentioned which card reader you're using, but please provide a link. If needed, replace with one that is guaranteed to work with 5V; AdaFruit has one if I'm not mistaken.
Use a brand spanking new SD card, formatted with the tool linked in post #90; your card(s) might be damaged.
This is indeed not the total solution but the fact that I have added the instruction file.position() makes that I can reach 32000 loops
You will find the code below but no hardware has been changed: I am still using the same 2 cards (one 8GB the other 16GB) UNO R3 and SD Shield 3.3V . No change in the wiring, USB Supply, SD formated by SdFat
Does someone know the explanation of this improvement?
// writing Test
#include <SD.h>
#include <SPI.h> //for the SD card module
String POWER = "";
const int chipSelect = 10;
int N = 0;
int Nmax = 32000;
File myFile;
//_______________________________________________
void setup() {
/* Debugging serial */
Serial.begin(9600);
SPI.begin();
//SPI.setClockDivider(4);
Serial.println(F("Ini SD cardN.."));
if (!SD.begin(chipSelect)) {
Serial.println(F("ini failed!"));
while (1)
; // la ledOFF reste allumée en continu pas la ledRec verte
}
Serial.println(F("ini done."));
myFile = SD.open("TESTXX.txt", FILE_WRITE);
// lcd.setCursor(0, 0);
Serial.print("Welcome");
// lcd.setCursor(0, 1);
Serial.print("TEST");
}
//______________________________________________________
void loop() {
int heure = 22;
int MIN = 33;
int jour = 2;
delay(100);
float voltage = 200;
float current = 12;
float power = 11;
float energy = 12;
float frequency = 12;
float pf = 55;
char POWER[20]; //plenty of room for the output
sprintf(POWER, "%02d;%02d;%02d;", jour, heure, MIN);
if (myFile) {
N = N + 1;
// lcd.setCursor(0, 0);
Serial.print("Nb de lignes ");
Serial.println(N);
Serial.print(" File Position ");
Serial.println(myFile.position());
Serial.print(" POWER ");
Serial.print(POWER);
Serial.print(voltage);
Serial.print(",");
Serial.print(current);
Serial.print(",");
Serial.print(power);
Serial.print(",");
Serial.print(energy, 1);
Serial.print(",");
Serial.print(frequency, 1);
Serial.print(",");
Serial.println(pf);
delay(100);
if (N < Nmax) {
myFile.print("ORDRE ");
myFile.print(N);
myFile.print(" ");
myFile.print(myFile.position());
myFile.print(" ");
for (int ii = 1; ii <= 20; ii++) {
myFile.print(POWER);
}
myFile.print(",");
myFile.print(voltage);
myFile.print(",");
myFile.print(current);
myFile.print(",");
myFile.print(power);
myFile.print(",");
myFile.print(energy, 3);
myFile.print(",");
myFile.print(frequency, 1);
myFile.print(",");
myFile.println(pf);
myFile.flush();
}
Serial.println(myFile.position());
}
if (N > Nmax - 2) {
while (1)
;
}
}
// loop finish