Many thanks for the replies - also to Klaus_K for responding again to the initial general question.
I've spent a couple of hours trying to complete the code posted by pcbbc and have reached an impasse and have some I'm-a-bit-out-of-my-depth/am-I-going-in-the-right-direction questions:
1 - the readRegister and writeRegister functions are not public in the library. I am assuming that moving them to public in the header file is ok? Implicit in this assumption is a second one i.e. that the intention was for the new read function to reside in the project code and not the library.
2 - my inelegant solution for exiting the wait state 0 was to pass a variable to use as the trigger for a state change. I am sure that this is not how it is supposed to be done. I am also not really clear about how the function is intended to be used as the delayTime is set to 1000 in state 3 which suggests to me that the intention is for the function to be called continuously in a loop. Which leads me to wonder why there is a wait state at all...
3 - finally, i ran into a bug which has me stumped. As written, the code passes correctly from state 0 through 2 but hangs before it reaches state 3. After exiting state 2 the lastStateChangeTime is always the same as now
Code and modified library header below.
As always, all comments and aid are massively appreciated!
#include <SPI.h>
#include "Adafruit_MAX31865_custom.h"
int state = 0;
unsigned long delayTime = 1000;
unsigned long lastStateChangeTime;
// existing hardware needs this pin
#define SOLENOID_PIN 12
// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31865 max = Adafruit_MAX31865(14, 15, 16, 17);
void setup() {
Serial.begin(115200);
pinMode(SOLENOID_PIN, OUTPUT);
digitalWrite(SOLENOID_PIN, HIGH);
max.begin(MAX31865_3WIRE);
}
unsigned long lastTime;
void loop() {
unsigned long timeNow = millis();
if (timeNow - lastTime >= 100) {
DoSPIWorkInBackground(true);
lastTime = timeNow;
}
// DoSPIWorkInBackground(true);
}
void DoSPIWorkInBackground(boolean readNow)
{
unsigned long now = millis();
if (now - lastStateChangeTime >= delayTime)
{
switch (state)
{
case 0:
Serial.print("state: ");
Serial.println(state);
if (readNow) state = 1;
//Waiting for more work
break;
case 1:
Serial.print("state: ");
Serial.println(state);
max.clearFault();
max.enableBias(true);
state = 2;
delayTime = 10;
break;
case 2:
Serial.print("state: ");
Serial.println(state);
uint8_t t = max.readRegister8(MAX31856_CONFIG_REG);
t |= MAX31856_CONFIG_1SHOT;
max.writeRegister8(MAX31856_CONFIG_REG, t);
state = 3;
Serial.print("state set to 3 in state 2: ");
Serial.println(state);
delayTime = 65;
break;
case 3:
Serial.print("state: ");
Serial.println(state);
uint16_t rtd = max.readRegister16(MAX31856_RTDMSB_REG);
// remove fault
rtd >>= 1;
// do something with new reading
// back to waiting state...
Serial.print("reading: ");
Serial.println(rtd);
state = 0;
delayTime = 1000;
break;
}
lastStateChangeTime = now;
}
Serial.print("now: ");
Serial.print(now); Serial.print(" | ");
Serial.print("lastStateChangeTime: ");
Serial.print(lastStateChangeTime); Serial.print(" | ");
Serial.print("now - lastStateChangeTime: ");
Serial.println(now - lastStateChangeTime);
Serial.print("delayTime: ");
Serial.print(delayTime); Serial.print(" | ");
Serial.println();
}
custom library header
/***************************************************
This is a library for the Adafruit PT100/P1000 RTD Sensor w/MAX31865
Designed specifically to work with the Adafruit RTD Sensor
----> https://www.adafruit.com/products/3328
This sensor uses SPI to communicate, 4 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#ifndef ADAFRUIT_MAX31865_CUSTOM_H
#define ADAFRUIT_MAX31865_CUSTOM_H
#define MAX31856_CONFIG_REG 0x00
#define MAX31856_CONFIG_BIAS 0x80
#define MAX31856_CONFIG_MODEAUTO 0x40
#define MAX31856_CONFIG_MODEOFF 0x00
#define MAX31856_CONFIG_1SHOT 0x20
#define MAX31856_CONFIG_3WIRE 0x10
#define MAX31856_CONFIG_24WIRE 0x00
#define MAX31856_CONFIG_FAULTSTAT 0x02
#define MAX31856_CONFIG_FILT50HZ 0x01
#define MAX31856_CONFIG_FILT60HZ 0x00
#define MAX31856_RTDMSB_REG 0x01
#define MAX31856_RTDLSB_REG 0x02
#define MAX31856_HFAULTMSB_REG 0x03
#define MAX31856_HFAULTLSB_REG 0x04
#define MAX31856_LFAULTMSB_REG 0x05
#define MAX31856_LFAULTLSB_REG 0x06
#define MAX31856_FAULTSTAT_REG 0x07
#define MAX31865_FAULT_HIGHTHRESH 0x80
#define MAX31865_FAULT_LOWTHRESH 0x40
#define MAX31865_FAULT_REFINLOW 0x20
#define MAX31865_FAULT_REFINHIGH 0x10
#define MAX31865_FAULT_RTDINLOW 0x08
#define MAX31865_FAULT_OVUV 0x04
#define RTD_A 3.9083e-3
#define RTD_B -5.775e-7
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
typedef enum max31865_numwires {
MAX31865_2WIRE = 0,
MAX31865_3WIRE = 1,
MAX31865_4WIRE = 0
} max31865_numwires_t;
class Adafruit_MAX31865 {
public:
Adafruit_MAX31865(int8_t spi_cs, int8_t spi_mosi, int8_t spi_miso, int8_t spi_clk);
Adafruit_MAX31865(int8_t spi_cs);
boolean begin(max31865_numwires_t x = MAX31865_2WIRE);
uint8_t readFault(void);
void clearFault(void);
uint16_t readRTD();
void setWires(max31865_numwires_t wires);
void autoConvert(boolean b);
void enableBias(boolean b);
float temperature(float RTDnominal, float refResistor);
uint8_t readRegister8(uint8_t addr);
uint16_t readRegister16(uint8_t addr);
void writeRegister8(uint8_t addr, uint8_t reg);
private:
int8_t _sclk, _miso, _mosi, _cs;
void readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n);
//uint8_t readRegister8(uint8_t addr);
//uint16_t readRegister16(uint8_t addr);
// void writeRegister8(uint8_t addr, uint8_t reg);
uint8_t spixfer(uint8_t addr);
};
#endif