From 8a4bf641109095b729ae175f618d24de2f895c89 Mon Sep 17 00:00:00 2001 From: budulinek Date: Fri, 6 Oct 2023 22:41:11 +0200 Subject: [PATCH] Update MAX31865_Single.ino --- examples/MAX31865_Single/MAX31865_Single.ino | 93 +++++++++++--------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/examples/MAX31865_Single/MAX31865_Single.ino b/examples/MAX31865_Single/MAX31865_Single.ino index 4ba0c8d..4c27941 100644 --- a/examples/MAX31865_Single/MAX31865_Single.ino +++ b/examples/MAX31865_Single/MAX31865_Single.ino @@ -61,14 +61,13 @@ void setup() { } void loop() { - // Read and print temperature and faults (non-blocking) - readRtd(); + // non-blocking state machine, read and print temperature and faults + stateMachine(); // Place your other functions here... - } -void readRtd() { +void stateMachine() { if (rtdTimer.isOver() == false) { return; } @@ -89,45 +88,8 @@ void readRtd() { break; case 2: { - uint16_t rtdVal = rtd.getRTD(); - - Serial.print("RTD value: "); - Serial.println(rtdVal); - float ratio = rtdVal; - ratio /= 32768; - Serial.print("Ratio = "); - Serial.println(ratio, 8); - Serial.print("Resistance = "); - Serial.println(RREF * ratio, 8); - Serial.print("Temperature = "); - Serial.println(rtd.getTemp(RNOMINAL, RREF)); - - // Check and print any faults - uint8_t fault = rtd.getFault(); - if (fault) { - Serial.print("Fault 0x"); - Serial.println(fault, HEX); - if (fault & MAX31865::FAULT_HIGHTHRESH_BIT) { - Serial.println("RTD High Threshold"); - } - if (fault & MAX31865::FAULT_LOWTHRESH_BIT) { - Serial.println("RTD Low Threshold"); - } - if (fault & MAX31865::FAULT_REFINLOW_BIT) { - Serial.println("REFIN- > 0.85 x Bias"); - } - if (fault & MAX31865::FAULT_REFINHIGH_BIT) { - Serial.println("REFIN- < 0.85 x Bias - FORCE- open"); - } - if (fault & MAX31865::FAULT_RTDINLOW_BIT) { - Serial.println("RTDIN- < 0.85 x Bias - FORCE- open"); - } - if (fault & MAX31865::FAULT_OVUV_BIT) { - Serial.println("Under/Over voltage"); - } - rtd.clearFault(); - } - Serial.println(); + printTemp(); + printFault(); rtd.enableBias(false); // disable bias voltage rtdTimer.sleep(1000); @@ -138,3 +100,48 @@ void readRtd() { break; } } + +void printTemp() { + uint16_t rtdVal = rtd.getRTD(); + + Serial.print("RTD value: "); + Serial.println(rtdVal); + float ratio = rtdVal; + ratio /= 32768; + Serial.print("Ratio = "); + Serial.println(ratio, 8); + Serial.print("Resistance = "); + Serial.println(RREF * ratio, 8); + Serial.print("Temperature = "); + Serial.println(rtd.getTemp(RNOMINAL, RREF)); +} + +// Check and print any faults +void printFault() { + uint8_t fault = rtd.getFault(); + + if (fault) { + Serial.print("Fault 0x"); + Serial.println(fault, HEX); + if (fault & MAX31865::FAULT_HIGHTHRESH_BIT) { + Serial.println("RTD High Threshold"); + } + if (fault & MAX31865::FAULT_LOWTHRESH_BIT) { + Serial.println("RTD Low Threshold"); + } + if (fault & MAX31865::FAULT_REFINLOW_BIT) { + Serial.println("REFIN- > 0.85 x Bias"); + } + if (fault & MAX31865::FAULT_REFINHIGH_BIT) { + Serial.println("REFIN- < 0.85 x Bias - FORCE- open"); + } + if (fault & MAX31865::FAULT_RTDINLOW_BIT) { + Serial.println("RTDIN- < 0.85 x Bias - FORCE- open"); + } + if (fault & MAX31865::FAULT_OVUV_BIT) { + Serial.println("Under/Over voltage"); + } + rtd.clearFault(); + } + Serial.println(); +}