SPI SD Shield

Hello All, I have an SD shield setup for SPI on an UNO, the pin select is set to pin 6, and a radio transceiver is also on the SPI port with pin 10 as the pin to select HIGH/LOW for selecting either device.

In my sketch I first start by seeing the SD cardInfo, which works perfect, then initialize the radio, then begin to loop to receive a packet of data from the RFM69 and then save it immediately to the SD card. I set the radio CS pin to HIGH (to deselect is) and the SD CS pin to LOW (to select it) and save the data to a file.

BUT it only (100%) works the very first time.

I've been looking into the SPI datamode being the problem. I'm not sure where to look next. I have an O-scope and can see the CS pins moving correctly however the CS pin for the radio seems to jump around...

Anyone know what I should look at?

Thanks

You can not use the SPI for something other than an SD card if the file on the SD card is open. To use the SD card like this with the radio you must close the file, then access the radio. Then open the file, write to the end of the file and close the file before accessing the radio again.

It is completing the cycle Begin, Open, Write, close…

It works the first time only, then I always get this to return false…

  if (!SD.begin(chipSelect)) {
    Serial.println("init failed to write!");  
    return false;
  }
bool writeToFile(const char* file) {
  
  
  if (!SD.begin(chipSelect)) {
    Serial.println("init failed to write!");  
    return false;
  }
  
  // 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(file, FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to ");
    Serial.print(file);
    Serial.print("...");
    
      for (byte i = 0; i < radio.DATALEN; i++)
      {myFile.print((char)radio.DATA[i]);}
      
      myFile.print("\r\n");
  
    myFile.close();
    SD.end();
    Serial.println("done.");
    return true;
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening file");
    return false;
  }
  
}

SD.begin() should be run only once in the setup function. After that, it is "open, write, close".

Nice Call SurferTIM!!!! I put the SD.begin into the Setup() and it works like a charm!!!

Appreciate the help :-)