SD - Ethernet toggle

Hi to all, i'm creating an arduino program that have to send some data to pachube and have to write some other data in the microSD cart attached to ethernet shield. Il possible to switch this two modalities during the loop?

Thank you :slight_smile:

yes,

try something like this

void loop()
{
  // MAKE MEASUREMENTS
  ....

  // Time for pachube?
  if (millis() - lastUpload > PACHUBE_INTERVAL)  // e.g 60000L = once per minute
  {
     // do pachube code here
     ...
     lastUpload = millis();
  }  

  // Time for SDcard?
  if (millis() - lastSDcard > SDCARD_INTERVAL)  // e.g 10000L = once per 10 seconds 
  {
     // do SDcard code here
     ...
     lastSDcard = millis();
  }

  // Time for display update?
  if (millis() - lastDisplay > DISPLAY_INTERVAL)  // e.g. 1000L = once per second  
  {
     // do display code here
     ...
     lastDisplay = millis();
  }
 etc

Well.. i think that you didn't understand me. I've already create a similar structure, but i don't know how to toggle the ethernet shield modality, because when I have to send parameters to the SD, i have to stop ethernet modality and start SD modality. How to do this with the official ethernet shield?

(I know that my english is quite incomprehensible :D)

OK, second try :wink:

Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). This is on digital pins 11, 12, and 13 on the Duemilanove and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general i/o. On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won't work.
Note that because the W5100 and SD card share the SPI bus, only one can be active at a time. If you are using both peripherals in your program, this should be taken care of by the corresponding libraries. If you're not using one of the peripherals in your program, however, you'll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and write a high to it. For the W5100, set digital pin 10 as a high output.

So the EthernetCard and SDcard work both over the hardware SPI, by makingthe right pin HIGH and the other LOW, one can select the device.

if (ethernet)
{
  digitalWrite(PIN4, LOW);
  digitalWrite(PIN10, HIGH);
}

if (SDcard)
{
  digitalWrite(PIN10, LOW);
  digitalWrite(PIN4, HIGH);
}

Note that I first deselect a device before making the other active. This way the two devices will never be active simultaneously.

Does this answer the question?

(otherwise post your question in your native language and translate it by google translate)

Well, if i use this method, when i've to execute the Ethernet.begin() function? each time i activate the pin 10 or only the first time?

Now, i've coded something like this

Client cl(ip_cl, 80);
void setup(){
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(10, HIGH);
  digitalWrite(4, LOW);
  Ethernet.begin(mac, ip, gateway, subnet);
}

void loop(){
  if(is_time_eth && cl.connect()){
    //Send data
  }
  if(is_time_sd){
    digitalWrite(10, LOW);
    digitalWrite(4, HIGH);
    if(SD.begin(4)){
      //Write data. ! I never initiate, should always be in error !
    }
    digitalWrite(10, HIGH);
    digitalWrite(4, LOW);
  }
}

It's wrong?

Well, i've find that with the SD standard library I don't have to write HIGH in the pin 4: that's the right way.

You can also initialize simoultaneously the ethernet and SD modality.

Thank you :wink:

Well, if i use this method, when i've to execute the Ethernet.begin() function? each time i activate the pin 10 or only the first time?

Only at the begin, The CS line will say to the ethernetchip "listen to the PSI bus" or "ignore the SPI bus" the state of the chip will not change.