I am working on a project where I read 8 MAX31856 thermocouple sensors using the Adafruit Library. This works just fine and prints the read outs to serial.
I want to also save the readings to a microSD card. And this is where I hit a snag.
I am trying to use the SdFat.h
library. I can loop through my MAX31856 thermocouples, and write once to my SD card, but after that it hangs the SPI bus.
the MAX31856 works on SPI_MODE1 (as per Adafruit Library) and the microSD card works on SPI_MODE0, I believe. I am trying to switch SPI bus modes before and after writing to the SD card but I think there is something wrong after first successful write to the SD card. After the first loop iteration, it seems like all data is basically noise on the SPI bus.
I am using the Adafruit ADA254 microSD breakout Board, which has a card detect pin, so I first check to see if an SD card is inserted, and then write to the file.
Question: Is there another or better way to switch SPI_MODES on each loop? is there anything more I need to consider so I end up back to SPI_MODE1 appropriately for the MAX31856 thermocouples?
Below is the pertinent code for what I have going on...
void setup(){
int sdCard = 36;
char filename[10] = "test.txt";
}
void loop(){
....code to read thermocouples ...
///THEN THIS
//lets confirm SD card is still in socket before writing
CardDetect = digitalRead(37);
if(CardDetect == HIGH){
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setBitOrder(LSBFIRST);
digitalWrite(sdCard, LOW);
sd.begin(sdCard, SD_SCK_MHZ(1));
//LETS START SAVING DATA
myFile.open(filename, O_WRONLY | O_CREAT | O_EXCL);
myFile.write(now.unixtime());
myFile.write(",");
for(int i = 0; i<8; i++){
myFile.print(TEMP[i]);
myFile.write(",");
}
myFile.println();
myFile.close();
digitalWrite(sdCard, HIGH);
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setBitOrder(MSBFIRST);
delay(200);
}
}
how do I debug the code?
, then remove all sensor code and see if file is written without crashing