conflitto libreria SD?

salve a tutti,
sto testando uno sketch webserver che mostra tramite ajax lo stato di due pulsanti. lo sketch funziona e fin qui nessun problema.
ora però ho la necessità di fare anche alcune operazioni sulla SD (ho un arduino ethernet + IDE 1.0.1).
il problema nasce quando vado ad includere nello sketch la libreria SD.h. il compilatore non segnala nessun errore ne prima ne dopo l'inclusione della libreria, ma il risultato è alquanto strano. in pratica sulla pagina web viene ripetuto il titolo H1 e lo stato dei pulsanti appare a tratti poi viene rimpiazzato da un altro messaggio. non saprei come spiegarlo in poche parole per cui allego lo sketch se qualcuno ha voglia di provarlo. fate la prova con e senza SD.h.

#include <Ethernet.h>
#include <SPI.h>
//#include <SD.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x01, 0x21 };
IPAddress ip(192, 168, 1, 199); 
EthernetServer server(80); 

String HTTP_req;            // stores the HTTP request

void setup()
{
  Ethernet.begin(mac, ip);  // initialize Ethernet device
  server.begin();           // start to listen for clients
  Serial.begin(9600);       // for diagnostics
  pinMode(2, INPUT);        // switch is attached to Arduino pin 2
  pinMode(3, INPUT);        // switch is attached to Arduino pin 3    
}

void loop()
{
  EthernetClient client = server.available();  // try to get client

  if (client) {  // got client?
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {   // client data available to read
        char c = client.read(); // read 1 byte (character) from client
        HTTP_req += c;  // save the HTTP request 1 char at a time
        // last line of client request is blank and ends with \n
        // respond to client only after last line received
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: keep-alive");
          client.println();

          if (HTTP_req.indexOf("ajax_switch") > -1) {      // AJAX request for switch state
            // read switch state and send appropriate paragraph text
            GetSwitchState(client);
          }
          else {  // HTTP request for web page
            // send web page - contains JavaScript with AJAX calls
            client.println("<!DOCTYPE html>");
            client.println("<html>");
            client.println("<head>");
            client.println("<title>Arduino Web Page</title>");
            client.println("<script>");
            client.println("function GetSwitchState() {");
            client.println("nocache = \"&nocache=\"\  + Math.random() * 1000000;");
            client.println("var request = new XMLHttpRequest();");
            client.println("request.onreadystatechange = function() {");
            client.println("if (this.readyState == 4) {");
            client.println("if (this.status == 200) {");
            client.println("if (this.responseText != null) {");
            client.println("document.getElementById(\"switch_txt\")\.innerHTML = this.responseText;");
            client.println("}}}}");
            client.println("request.open(\"GET\", \"ajax_switch\" + nocache, true);");
            client.println("request.send(null);");
            client.println("setTimeout('GetSwitchState()', 1000);");
            client.println("}");
            client.println("</script>");
            client.println("</head>");
            client.println("<body onload=\"GetSwitchState()\">");
            client.println("<h1>Arduino AJAX Switch Status</h1>");
            client.println("<p id=\"switch_txt\">Switch state: Not requested...</p>");
            client.println("</body>");
            client.println("</html>");
          }
          // display received HTTP request on serial port
          Serial.print(HTTP_req);
          HTTP_req = "";            // finished with request, empty string
          break;
        }
        // every line of text received from the client ends with \r\n
        if (c == '\n') {
          // last character on line of received text
          // starting new line with next character read
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // a text character was received from client
          currentLineIsBlank = false;
        }
      } // end if (client.available())
    } // end while (client.connected())
    delay(1);      // give the web browser time to receive the data
    client.stop(); // close the connection
  } // end if (client)
}

// send the state of the switch to the web browser
void GetSwitchState(EthernetClient cl)
{
  for( int pin = 2; pin < 4; pin++){
    if (digitalRead( pin ) ) {
      cl.println("<div id='on'> Switch ");
      cl.println(pin);
      cl.println(" state: ON </div>");        
    }
    else {
      cl.println("<div id='off'>Switch ");
      cl.println(pin);
      cl.println(" state: OFF</div>"); 
    }
  }
}

Usano tutti e due l'SPI, devi selezionarli con il CS.

usa la macro F() per non consumare RAM quando stampi un testo.
client.println("HTTP/1.1 200 OK"); diventa
client.println(F("HTTP/1.1 200 OK"));

Usa il IDE 1.0.5 perché risolve alcuni problemi.

Ciao Uwe

Concordo con Cece: la SD usa il pin 4, mentre la Ethernet usa il pin 10 come chip select (attivo = LOW).

Quindi prima di usare una o l'altra devi fare:
digitalWrite(ETPin, HIGH); // Ethernet not selected
digitalWrite(SDPin, LOW); // SD selected
oppure il viceversa.

grazie ragazzi, ho provato a seguire i vostri consigli, ma purtoppo il problema non si è risolto:

includo la libreria SD, imposto i CS 4 high e 10 low ( nel setup() ) come suggerito, ma il sorgente della pagina web che ottengo è vuoto... non stampa nulla, o per meglio dire stampa soltanto il risultato della funzione (è una funzione giusto?) void GetSwitchState(EthernetClient cl).

ho anche aggiornato l'IDE al 1.0.5 e usato la macro F(), ma il compilatore mi segnala dei warnigs (uno per ogni riga nella quale è usata la macro):

"Arduino_AJAX_Switch_Status.ino:77: warning: only initialized variables can be placed into program memory area"

La funzione F la puoi usare solo per il testo, non puoi usare variabili

Serial.print(F(val));

è errato.

Serial.print(F("testo"));

è corretto.