Go Down

Topic: back and forth between Ethernet and SD on Wiznet 5100 shield (Read 180 times) previous topic - next topic

ieee488

I have placed a formatted empty microSD card into the microSD slot.

I have combined code from Webserver sketch of Ethernet library and listfiles sketch of the SD library.

In setup(), I
a.) list the files on the SD card
b.) display the IP address of the Ethernet shield
c.) list the files on the SD card
c.) display the IP address of the Ethernet shield

Trying to understand how to access both.

But I am not getting a valid IP address.
Then when the sketch goes back to the SD card, no files are displayed.

What am I doing wrong?


Code: [Select]

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

File root;


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] =
{
     0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

IPAddress ip(192, 168, 2, 50);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);


void setup()
{
    Serial.begin(9600);

    while (!Serial)
    {
        ; // wait for serial port to connect. Needed for native USB port only
    }

    pinMode(4, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(10, LOW);
    //pinMode(4, HIGH);
    delay(5);

    Serial.print("\nInitializing SD card...");
    if (!SD.begin(4))
    {
        Serial.println("failed!");
        return;
    }
    else
    {
        Serial.println("done");
    }
    //display list of files
    root = SD.open("/");
    printDirectory(root, 0);

    pinMode(10, HIGH);
    pinMode(4, LOW);
    delay(5);
    // start the Ethernet connection and the server:
    Ethernet.begin(mac, ip);
    server.begin();

    Serial.println("-------------------------"); //added 16Feb2016
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
    Serial.println("-------------------------"); //added 16Feb2016

    pinMode(10, LOW);
    pinMode(4, HIGH);
    delay(5);
    Serial.println("back to SD");
    //go back to SD
    root = SD.open("/");
    printDirectory(root, 0);


    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
    Serial.println("-------------------------"); //added 16Feb2016

}


void loop()
{
 
}


void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}


AWOL

Code: [Select]
    pinMode(10, HIGH);
    pinMode(4, LOW);
Did you mean to use digitalWrite?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

ieee488

Code: [Select]
    pinMode(10, HIGH);
    pinMode(4, LOW);
Did you mean to use digitalWrite?
Darn!
Yes.
Thank you!

And I also realize that setting the pin HIGH actually disables it.

I turns out that all I need to do is disable the shield's SPI with digitalWrite(10, HIGH); so that the SD card can be initialized. Found it on a post on these forum.

After that it appears I don't need to do anything else.

Go Up