Hello, I am really frustrated, have check around 10 sketches on the net. Think I do understand usage of the SPI bus and the meaning of SS/CS.
The problem boils down to: As soon as I check for available ethernet data, the SD lib refuses to open files. What needs to be done? Please.. can anyone help?! :~
Here is the code:
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h> // Tested in version IDE 0022 Arduino UNO
#include <EthernetUdp.h>
byte myMac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xB4, 0xCB };//the mac adress of ethernet shield or uno shield board
byte myIp[] = { 10,0,0,250 };// the IP adress of your device, that should be in same universe of the network you are using, here: 192.168.1.x
int listenPort = 6455; // listen here for OSC (incomming)
File myFile;
EthernetUDP Udpin;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
// B): --------- Start Ethernet -----------------
Ethernet.begin(myMac ,myIp);
Serial.println("Open Port to listen for incomming OSC packages");
Udpin.begin(listenPort);
// A): --------- Init SD card -----------------
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Serial.println("done!");
}
void loop()
{
Serial.println("--------- Loop begin -----------");
// C): --------- Read File -----------------
myFile = SD.open("test3.txt");
if (myFile) {
Serial.println("test3.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
Serial.println("- EOF -");
}
else {
// if the file didn't open, print an error:
Serial.println("Read: error opening test2.txt");
}
// nothing happens after setup finishes.
delay(2000);
// D): --------- Check for packets arrived ----------------
int packetSize = Udpin.parsePacket();
if(packetSize){
Serial.print("Packet recieved..");
};
delay(1000);
}
Here is the output. As you can see the first time the code runs the file is found and read.
.. but then I check for available ethernet data. And then - on the next cycle - the SD lib cannot open the file anymore.
Open Port to listen for incomming OSC packages
Initializing SD card...initialization done.
done!
--------- Loop begin -----------
test3.txt:
22.22.--------- Loop begin -----------
Read: error opening test2.txt
--------- Loop begin -----------
Read: error opening test2.txt
--------- Loop begin -----------
Read: error opening test2.txt
..
If I take out the ethernet code from the main loop (block D). The the file is continously read correctly.
If I change the order of the code (swap C / D) - meaning first check for data arrived and then access the SD card, it fails immediately..
What can I do? I guess it is something about SPI and switching between the devices, but what.
I am using a Arduino UNO (R3) + the standard Ethernet Shield with WS5100. I compile with Arduino 1.0.5 and only use the standard libraries.
Jens