// dual_max31856.ino // example of reading two MAX31856 modules /* (c)2019 Forward Computing and Control Pty. Ltd., NSW Australia All rights reserved. Subject to the BSD licence below, this code may be freely used for both private and commerical use. Provide this copyright is maintained. */ #include // install the loopTimer library from https://www.forward.com.au/pfod/ArduinoProgramming/RealTimeArduino/TimingDelaysInArduino.html // loopTimer.h also needs the millisDelay library installed from https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html #include #include // install SafeString library from Library manager or from https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html // to get BufferedOutput. See https://www.forward.com.au/pfod/ArduinoProgramming/Serial_IO/index.html for a full tutorial // on Arduino Serial I/O that Works #include #include // Use software SPI: CS, DI, DO, CLK MAX31856_noDelay maxthermo = MAX31856_noDelay(10, 11, 12, 13); // use hardware SPI, just pass in the CS pin //MAX31856_noDelay maxthermo = MAX31856_noDelay(10); // create the second thermocouple object controlled by CS pin 9 MAX31856_noDelay maxthermo2 = MAX31856_noDelay(9); // NOTE: this still uses software SPI set by maxthermo above // the SPI settings are set by the first call to MAX31856_noDelay(..) and ignored by any subsequent calls createBufferedOutput(bufferedOut, 80, DROP_UNTIL_EMPTY); millisDelay readingsTimer; unsigned long READINGS_REPEAT_MS = 1000; // should not be less than MAX31856_DELAY_MS below bool readingStarted = false; // set to true if reading already running millisDelay max31856Delay; unsigned long MAX31856_DELAY_MS = 200; // max 185ms for single shot reading so say 200ms void setup() { Serial.begin(9600); for (int i = 10; i > 0; i--) { Serial.println(i); delay(500); } Serial.println("Dual MAX31856 thermocouple test"); maxthermo.begin(); maxthermo2.begin(); // begin second board // SPI interface is only started once by the first call to begin() // but each begin() set the CS line for that MAX31856 // the defaults at the top of MAX31856_noDelay.cpp are set on the first call to any on of the library methods if resetDefaults() not called here //maxthermo.setThermocoupleType(MAX31856_TCTYPE_K); // this is the default Serial.print("Thermocouple 1 type: "); switch (maxthermo.getThermocoupleType() ) { // the call to getThermocoupleType(), or any other method, sets the defaults first case MAX31856_TCTYPE_B: Serial.println("B Type"); break; case MAX31856_TCTYPE_E: Serial.println("E Type"); break; case MAX31856_TCTYPE_J: Serial.println("J Type"); break; case MAX31856_TCTYPE_K: Serial.println("K Type"); break; case MAX31856_TCTYPE_N: Serial.println("N Type"); break; case MAX31856_TCTYPE_R: Serial.println("R Type"); break; case MAX31856_TCTYPE_S: Serial.println("S Type"); break; case MAX31856_TCTYPE_T: Serial.println("T Type"); break; case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break; case MAX31856_VMODE_G32: Serial.println("Voltage x32 Gain mode"); break; default: Serial.println("Unknown"); break; } Serial.print("Thermocouple 2 type: "); switch (maxthermo2.getThermocoupleType() ) { // the call to getThermocoupleType(), or any other method, sets the defaults first case MAX31856_TCTYPE_B: Serial.println("B Type"); break; case MAX31856_TCTYPE_E: Serial.println("E Type"); break; case MAX31856_TCTYPE_J: Serial.println("J Type"); break; case MAX31856_TCTYPE_K: Serial.println("K Type"); break; case MAX31856_TCTYPE_N: Serial.println("N Type"); break; case MAX31856_TCTYPE_R: Serial.println("R Type"); break; case MAX31856_TCTYPE_S: Serial.println("S Type"); break; case MAX31856_TCTYPE_T: Serial.println("T Type"); break; case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break; case MAX31856_VMODE_G32: Serial.println("Voltage x32 Gain mode"); break; default: Serial.println("Unknown"); break; } bufferedOut.connect(Serial); // connect this after any setup print statements readingsTimer.start(READINGS_REPEAT_MS); } /// =========== end of setup() ============== void loop() { bufferedOut.nextByteOut(); // call this one or more times each loop() to release buffered chars loopTimer.check(bufferedOut); if (readingsTimer.justFinished()) { readingsTimer.repeat(); if (!readingStarted) { maxthermo.oneShotTemperature(); // start one now maxthermo2.oneShotTemperature(); // start one now max31856Delay.start(MAX31856_DELAY_MS); // start delay to pick up results } } if (max31856Delay.justFinished()) { readingStarted = false; // can pick up both results now bufferedOut.print("Cold Junction 1 Temp: "); bufferedOut.println(maxthermo.readCJTemperature()); bufferedOut.print("Thermocouple 1 Temp: "); bufferedOut.println(maxthermo.readThermocoupleTemperature()); // Check and print any faults uint8_t fault1 = maxthermo.readFault(); if (fault1) { bufferedOut.println(" Thermocouple 1 Fault"); } bufferedOut.print("Cold Junction 2 Temp: "); bufferedOut.println(maxthermo2.readCJTemperature()); bufferedOut.print("Thermocouple 2 Temp: "); bufferedOut.println(maxthermo2.readThermocoupleTemperature()); // Check and print any faults uint8_t fault2 = maxthermo2.readFault(); if (fault2) { bufferedOut.println(" Thermocouple 2 Fault"); } } }