Hi all!
i started apart a project to test the sd read/write functions with succes than i created separeted functions to use in my code.
Code: Select all
void scrivi(String path, String message)
{
Serial.println(F("Initializing SD card..."));
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(5, OUTPUT);
SPIClass SPISD;
SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS
if (!SD.begin(5, SPISD))
{
Serial.println(F("initialization failed!"));
return;
}
Serial.println(F("initialization done."));
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File myFile = SD.open(path, FILE_APPEND);
// if the file opened okay, write to it:
if (myFile)
{
Serial.print(F("Writing to: "));
Serial.println(path);
myFile.println(message);
// close the file:
myFile.close();
Serial.println(F("done."));
}
else
{
// if the file didn't open, print an error:
Serial.println(F("error opening: "));
Serial.println(path);
}
}
I write once in the setup and then in the loop every 10sec using same call to function.
Code: Select all
scrivi("/test.txt", "message");
In the setup it works (i also checked the file in sd it is there written even multiple lines if didnt cancel the file) but when got the first call in the loop i got guru meditation error, it seems to happen here:
Code: Select all
File myFile = SD.open(path, FILE_APPEND);
Code: Select all
Initializing SD card...
initialization done.
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4014f799 PS : 0x00060230 A0 : 0x800da592 A1 : 0x3ffb18b0
A2 : 0x000f4240 A3 : 0x3ffb1ca4 A4 : 0x3ffb1c50 A5 : 0x3ffc87b8
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x800ef511 A9 : 0x3ffb19d0
A10 : 0x3ffb1c1c A11 : 0x3ffc777c A12 : 0x00000000 A13 : 0x00000000
A14 : 0x3ffd4e03 A15 : 0x00000001 SAR : 0x00000009 EXCCAUSE: 0x0000001c
EXCVADDR: 0x000f4240 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffd
Backtrace: 0x4014f799:0x3ffb18b0 0x400da58f:0x3ffb18d0 0x400da849:0x3ffb1900 0x400dad0d:0x3ffb1930 0x400f0a26:0x3ffb1970 0x400ed1f1:0x3ffb1990 0x400ee52e:0x3ffb19b0 0x400ee67d:0x3ffb19d0 0x400ef50e:0x3ffb1a00 0x400f064c:0x3ffb1c70 0x4010a4c9:0x3ffb1de0 0x4000bcc5:0x3�
Code: Select all
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
int a;
void leggi(String path)
{
Serial.println(F("Initializing SD card..."));
pinMode(5, OUTPUT);
SPIClass SPISD;
SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS
if (!SD.begin(5, SPISD))
{
Serial.println(F("initialization failed!"));
return;
}
Serial.println(F("initialization done."));
Serial.print("------------Leggo:");
Serial.println(path);
// re-open the file for reading:
File myFile = SD.open(path);
if (myFile)
{
Serial.println(path);
// read from the file until there's nothing else in it:
while (myFile.available())
{
Serial.write(myFile.read());
}
// close the file:
myFile.close();
}
else
{
// if the file didn't open, print an error:
Serial.println(F("error opening test.txt"));
}
}
void scrivi(String path, String message)
{
Serial.println(F("Initializing SD card..."));
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(5, OUTPUT);
SPIClass SPISD;
SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS
if (!SD.begin(5, SPISD))
{
Serial.println(F("initialization failed!"));
return;
}
Serial.println(F("initialization done."));
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File myFile = SD.open(path, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile)
{
Serial.print(F("Writing to:"));
Serial.println(path);
myFile.println(message);
// close the file:
myFile.close();
Serial.println(F("done."));
}
else
{
// if the file didn't open, print an error:
Serial.println(F("error opening: "));
Serial.println(path);
}
}
void sovrascrivi(String path, String message)
{
Serial.println(F("Initializing SD card..."));
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(5, OUTPUT);
SPIClass SPISD;
SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS
if (!SD.begin(5, SPISD))
{
Serial.println(F("initialization failed!"));
return;
}
Serial.println(F("initialization done."));
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File myFile = SD.open(path, FILE_APPEND);
// if the file opened okay, write to it:
if (myFile)
{
Serial.print(F("Writing to: "));
Serial.println(path);
myFile.println(message);
// close the file:
myFile.close();
Serial.println(F("done."));
}
else
{
// if the file didn't open, print an error:
Serial.println(F("error opening: "));
Serial.println(path);
}
}
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
a=0;
}
void loop()
{
String messaggio = String(a);
messaggio = messaggio + ": ";
messaggio = messaggio + millis();
messaggio = messaggio + "\n";
scrivi("/test_scrivi.txt",messaggio);
delay(100);
sovrascrivi("/test_sovrascrivi.txt", messaggio);
delay(1000);
leggi("/test_scrivi.txt");
delay(100);
leggi("/test_sovrascrivi.txt");
a++;
}
I dont know what to do, seems to be some memory problem, anyone had already experienced it???