Arduino Mega + Ethernet Shield

I'm with a problem reading the Micro Sd through the Ethernet Shield, the first time it reads the file, but the second time he already failed, perhaps I'm using the keypad? or by using the library display 128x64 GLCD?

#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 =4;

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':
    Sd();
    break;
}}

}
}

//**************--------------******************************---------------------------------------

void Sd(){
  File myFile;
    delay(110);
   
  myFile = SD.open("test.txt", FILE_WRITE);

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");
  }
  delay(110);
  // 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());
    }
    myFile.close();

}
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

Try disabling the w5100 SPI. That caused the same problem with my SD card.

void setup() {
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  // add this
  digitalWrite(10,HIGH);

  // rest of your setup
}

Continue message initialization failed! =(

void Sd(){
   File myFile;
    delay(110);
    
   // you are opening this before the SD is initialized
   myFile = SD.open("test.txt", FILE_WRITE);
 
  // this should be in setup()
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  // rest of code

I still have problems, will be a problem of the Ethernet Shield? Sdfat16 tried to use the library but could not.

Try this sketch. Does the SD still fail?

#include <SD.h>

void setup() {
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print("Starting SD...");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");
}

void loop() {
}

edit: My bad on the serial command.
If it fails, explain what keypad and GLCD you are using, and how they are connected.

I managed to make it work with the library fat16, someone has some examples explaining the commands?

The 4x4 keypad is connected to pins A10 ... A15
The display is connected to pins traditional, is the type B.

The display is connected to pins traditional, is the type B.

Can you be more specific? The lcd stuff in the examples has pin assignments that will conflict with the SD.
You can't use digital pins 4 or 10. Those are used by the SD and w5100.

Pino do Display LCD 128×64 Pin Arduino Mega
1 GND
2 5V
3 -
4 36
5 35
6 37
7 22
8 23
9 24
10 25
11 26
12 27
13 28
14 29
15 33
16 34
17 Reset
18 -
19 5V
20 GND

Is this the GLCD you are using?

this is the display HTTP 301 This page has been moved

That looks ok. I see no conflict. You said above "I managed to make it work", so it is working now?

Yes,using SdFat16 worked, but it is much more complicated, I am having difficulty sending the data file for the serial.
Sorry for my english beginner. =(

It worked, thanks for the support. :slight_smile: