MAX 31856 & SD CARD reader misbehaving

Hello world,

I have been working on a project using two Adafruit MAX 31856, SD Card shield, Adafruit GPS shield and the Arduino Mega 2560. I have been able to have the GPS communicate to the Virtuabotix SD Card Reader/Writer and I have been able to have the both of the MAX 31856 thermocouples communicate to the serial monitor.

However there is a communication issue between the SD card library and the SPI library that governs the communication to the SD card and the thermocouple. From the serial monitor I can see that I am receiving room temperature values in Celsius of roughly 25 - 28 when the SD card library is not included. However when I attempt to log data and include the SD card library the thermocouples go to a null mode and spit out and log 0.0 repetitively.

Attached is the code I have been able to slam together,
thank you for your time.

#include <Adafruit_MAX31856.h>
#include <SPI.h>
#include <SD.h>
// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(9, 50, 51, 52);
// use hardware SPI, just pass in the CS pin
Adafruit_MAX31856 maxthermo2 = Adafruit_MAX31856(8, 50, 51, 52);


#include <Adafruit_GPS.h>    //Install the adafruit GPS library
#include <SoftwareSerial.h> //Load the Software Serial library
//Connect the GPS power pin to 5V
//Connect the Ground pin to Ground
//Connect the TX pin to Digital Pin 11
//Connect the RX pin to Digital Pin 10
SoftwareSerial mySerial(11, 10); //Initialize the Software Serial port
Adafruit_GPS GPS(&mySerial); //Create the GPS Object
#define GPSECHO true
//Leave 'true' to debug and listen to raw GPS sentences

//Adafruit_MAX31856 sd = Adafruit_MAX31856(53, 50, 51, 52);

//int chipSelect = 4;
float CJTOne;
float CJTTwo;
float TCTOne;
float TCTTwo;

#define pin_MOSI 51
#define pin_MISO 50
#define chipSelect 4

char leo_;


String NMEA1; //Variable for first NMEA sentence
String NMEA2; //Variable for second NMEA sentence
char c; //to read characters coming from the GPS

File mySensorData;

void setup()
{
  Serial.begin(115200);
  SPI.begin();
  
    
  Serial.println("Adafruit GPS library basic test!");
  //9600 NMEA is default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  //GPS Command to turn on RMC and GGA type data acquisition
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  //Update rate of the GPS set to 1Hz
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  //Requests update on the Antenna Status
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);
  //Ask for firmware version
  mySerial.println(PMTK_Q_RELEASE);

  Serial.println("MAX31856 thermocouple test");
  maxthermo.begin();  //Initate the first thermocouple
  maxthermo.setThermocoupleType(MAX31856_TCTYPE_K);
  Serial.print("Thermocouple type: ");
  switch (maxthermo.getThermocoupleType() ) {
    case MAX31856_TCTYPE_K: Serial.println("K Type"); break;
    case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break;
    case MAX31856_VMODE_G32: Serial.println("Voltage x8 Gain mode"); break;
    default: Serial.println("Unknown"); break;
  }
  maxthermo2.begin(); //Initiate the second thermocouple
  maxthermo2.setThermocoupleType(MAX31856_TCTYPE_K);
  Serial.print("Thermocouple type: ");
  switch (maxthermo2.getThermocoupleType())
  {
    case MAX31856_TCTYPE_K: Serial.println("K Type"); break;
    case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break;
    case MAX31856_VMODE_G32: Serial.println("Voltage x8 Gain mode"); break;
    default: Serial.println("Unknown"); break;
  }



//************************************************************************************
// Only a simulation to access the SD memory is enough to leave the MOSI signal HIGH 
    mySensorData = SD.open("index.txt", FILE_READ); 
    leo_= mySensorData.read();
    mySensorData.close();
  // I added this transfer to release the MISO line
    SPI.transfer(0x00);
//*****If the lines above are removed the MOSI pin remains LOW****************************

  delay(1000);


  SD.begin(chipSelect);
  
}

void loop()
{

  //Set SPI data mode to 1
  SPI.setDataMode(SPI_MODE1);

  digitalWrite(chipSelect, HIGH);
  SPI.transfer(0x80); //Address byte
  SPI.transfer(0x04);  //Control byte
  digitalWrite(chipSelect, LOW);

  digitalWrite(chipSelect, HIGH);
  SPI.transfer(0x02);  //address byte
  // read the three data from TC72
  int result1 = SPI.transfer(maxthermo.readCJTemperature());
  int result2 = SPI.transfer(0);
  int result3 = SPI.transfer(0);
  digitalWrite(chipSelect, LOW);

  // set SPI mode back to 0
  SPI.setDataMode(SPI_MODE0);

  Serial.print (maxthermo.readCJTemperature());
  Serial.print ("  ");
  Serial.print (result2);
  Serial.print ("  ");
  Serial.println (result3);
  delay(10);  


/*  
  readGPS();
  thermoSerialWriter();
  CJTOne = maxthermo.readCJTemperature() ;   //Read Cold Junction Temperature from Thermo One
  CJTTwo = maxthermo2.readCJTemperature() ;  //Read Cold Junction Temperature from Thermo Two
  TCTOne = maxthermo.readThermocoupleTemperature() ;   //Read Thermo Temperature from Thermo One
  TCTTwo = maxthermo2.readThermocoupleTemperature() ;  //Read Thermo Temperature from Thermo Two
  String dataStringOne = String(CJTOne) + "," + String(CJTOne);
  Serial.println(dataStringOne);
  String dataStringTwo = String(CJTTwo) + "," + String(CJTTwo);
  thermoSensorWriter(dataStringOne, dataStringTwo);
*/
}




void readGPS()
{

  clearGPS();
  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
  NMEA1 = GPS.lastNMEA();

  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
  NMEA2 = GPS.lastNMEA();

  Serial.println(NMEA1);
  Serial.println(NMEA2);
  Serial.print("Location: ");
  Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
  Serial.print(", ");
  Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);

  Serial.print("Speed (knots): "); Serial.println(GPS.speed);
  Serial.print("Angle: "); Serial.println(GPS.angle);
  Serial.print("Altitude: "); Serial.println(GPS.altitude);
  Serial.print("Satellites: "); Serial.println((int)GPS.satellites);

  Serial.println("");

}

void clearGPS()
{ //Clear old and corrupt data from serial port
  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

}




void thermoSerialWriter() //When called it will write the sensor data on the Serial monitor
{  
  Serial.print("Cold Junction Temp: ");
  Serial.println(maxthermo.readCJTemperature());

  Serial.print("Thermocouple Temp: ");
  Serial.println(maxthermo.readThermocoupleTemperature());

  Serial.print("Cold Junction Temp Two: ");
  Serial.println(maxthermo2.readCJTemperature());

  Serial.print("Thermocouple Temp Two: ");
  Serial.println(maxthermo2.readThermocoupleTemperature());
}



void thermoSensorWriter(String dataStringOne, String dataStringTwo) //When called it will write the sensor data on to the SD card
{
  delay(1000);
  mySensorData = SD.open("TLog1.txt", FILE_WRITE);  //Produce a .txt for thermocouple one
  if (mySensorData)
  {
    mySensorData.print("Cold Junction Temp for Thermocouple One ");
    mySensorData.print(CJTOne);
    mySensorData.print(", ");
    mySensorData.print("Thermocouple Temp one ");
    mySensorData.println(TCTOne);
    mySensorData.print(dataStringOne);
    mySensorData.println(" ");
    mySensorData.close();   //Close the file
    //Take a measurement every 100 milliseconds
  }
  mySensorData = SD.open("TLog2.txt", FILE_WRITE);  //Produce a .txt for thermocouple one
  if (mySensorData)
  {
    mySensorData.print("Cold Junction Temp for Thermocouple One ");
    mySensorData.print(CJTTwo);
    mySensorData.print(", ");
    mySensorData.print("Thermocouple Temp one ");
    mySensorData.println(TCTTwo);
    mySensorData.print(dataStringTwo);
    mySensorData.println(" ");
    mySensorData.close();   //Close the file
    //Take a measurement every 100 milliseconds
  }
}

Templogger_rev1.1.ino (7.69 KB)