Boa tarde galera, estou com um pequeno problema para escrever no SD card usando o arduino mega+Ethernet Shield, eu consigo abrir o arquivo e escrever nele uma vez, mas na segunda vez ele da erro, segue o codigo, abraços.
#include <glcd.h>
#include <fonts/allFonts.h>
#include <Keypad.h>
#include <SD.h>
//DECLARAÇÃO DO TECLADO
unsigned long startMillis;
unsigned int iter = 0;
unsigned int iterStart = 0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{
'1','2','3','A' }
,
{
'4','5','6','B' }
,
{
'7','8','9','C' }
,
{
'*','0','#','D' }
};
byte rowPins[ROWS] = {
A12, A13, A14, A15}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
A8, A9, A10,A11}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key = keypad.getKey();
char id[5];
int a=0;
const int chipSelect =10;
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);
//INICIALIZAÇÃO DO DISPLAY
GLCD.Init(NON_INVERTED); // initialise the library
GLCD.ClearScreen(); //limpa lcd
GLCD.SelectFont(System5x7); // select fixed width system font
GLCD.SetTextMode(SCROLL_UP);
}
void loop(){
int i=0;
delay(150);
GLCD.ClearScreen();
GLCD.CursorTo(0,0);
GLCD.print("ID=");
//SE NÃO FOI DIGITADO NENHUM ID AINDA ID=0000
if (a == 0){ //A VARIAVEL FOR FALSA ELE JOGA O ID = 0000
for(i=0;i<5;i++){
id[i]=0;
}
}
while (i < 5 ){ //MOSTRA O ID
GLCD.CursorTo(i+4,0);
GLCD.print(id[i]);
i++;
}
GLCD.CursorTo(0,1);
GLCD.println("PRECIONE A FUNCAO DESEJADA");
GLCD.println("A=Identificação");
GLCD.println("B=Mensagens Diversas");
//MENU DE ESCOLHA
switch (key = keypad.getKey()){
case 'B':
funcoes();
break;
}
}
//**************--------------****************************************---------------------------------------**********
void funcoes(){
File myFile;
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("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.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// 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("error opening test.txt");
}
delay(2000);
}