i found this but of code online, it works well but instead of each object being created automatically, id like to have them set as individual items
currently the Serial results are:
T0=72.12 T1=74.38 T2=24.27 T3=74.09
T0=72.22 T1=74.34 T2=107.61 T3=74.15
T0=72.15 T1=74.30 T2=109.40 T3=74.15
ideally Id like CHTM = (temp) CHTP = (temp) EGTM = (temp) EGTP = (temp)
#include <MAX31856.h>
#define SCK 13
#define CS0 10
#define CS1 9
#define CS2 8
#define CS3 7
#define SDI 11
#define SDO 12
#define NUM_MAX31856 4
#define CR0_INIT (CR0_AUTOMATIC_CONVERSION + CR0_OPEN_CIRCUIT_FAULT_TYPE_K /* + CR0_NOISE_FILTER_50HZ */)
#define CR1_INIT (CR1_AVERAGE_2_SAMPLES + CR1_THERMOCOUPLE_TYPE_K)
#define MASK_INIT (~(MASK_VOLTAGE_UNDER_OVER_FAULT + MASK_THERMOCOUPLE_OPEN_FAULT))
MAX31856 *TemperatureSensor[NUM_MAX31856] = {
new MAX31856(SDI, SDO, CS0, SCK),
new MAX31856(SDI, SDO, CS1, SCK),
new MAX31856(SDI, SDO, CS2, SCK),
new MAX31856(SDI, SDO, CS3, SCK)
};
void setup() {
// Display temperatures using the serial port
Serial.begin(9600);
delay(3000);
Serial.println("Quad MAX31856 Sample application");
// Initializing the MAX31855's registers
for (int i = 0; i < NUM_MAX31856; i++) {
TemperatureSensor[i]->writeRegister(REGISTER_CR0, CR0_INIT);
TemperatureSensor[i]->writeRegister(REGISTER_CR1, CR1_INIT);
TemperatureSensor[i]->writeRegister(REGISTER_MASK, MASK_INIT);
}
// Wait for the first samples to be taken
delay(200);
}
void loop () {
for (int i =0 ; i < NUM_MAX31856; i++) {
double temperature = TemperatureSensor[i]->readJunction(FAHRENHEIT);
if (temperature == NO_MAX31856)
continue;
temperature = TemperatureSensor[i]->readThermocouple(FAHRENHEIT);
if (temperature == NO_MAX31856)
continue;
Serial.print("T");
Serial.print(i);
Serial.print("=");
printTemperature(temperature);
Serial.print("\t");
}
Serial.println();
delay(1000);
}
void printTemperature(double temperature) {
switch ((int) temperature) {
case FAULT_OPEN:
Serial.print("FAULT_OPEN");
break;
case FAULT_VOLTAGE:
Serial.print("FAULT_VOLTAGE");
break;
case NO_MAX31856:
Serial.print("NO_MAX31856");
break;
default:
Serial.print(temperature);
break;
}
Serial.print(" ");
}
gfvalvo
September 17, 2019, 2:26am
#2
Qhergt1987:
it works well but instead of each object being created automatically, id like to have them set as individual items
Why? Using an array is the logical way to handle a situation like this. Although, I don't understand why you're using an array of pointers rather than an array of objects. There's no advantage to that since, in this case, you're creating an array of globals. Just do:
MAX31856 TemperatureSensors[] = {
{SDI, SDO, CS0, SCK},
{SDI, SDO, CS1, SCK},
{SDI, SDO, CS2, SCK},
{SDI, SDO, CS3, SCK}
}
const uint8_t NUM_MAX31856 = sizeof(TemperatureSensors) / sizeof(TemperatureSensors[0]);
how would i get them to display on an i2c 20x4 LCD?
if i do an LCD.print as it is now, id end up with the T0,T1,T3 showing up with the related temps.
i want to do something like
lcd.setCursor(0,0);
lcd.print("CM:");
lcd.setCursor(0,4);
lcd.print(t0);
lcd.setCursor(0,9);
lcd.print(" CP:");
lcd.setCursor(0,14);
lcd.print(t1);
So you want this
Serial.print("T");
Serial.print(i);
Serial.print("=");
to put out the names that you want ? so print the names that you stored in an array.
gfvalvo
September 17, 2019, 10:15am
#5
It’s a programming best practice to group related elements of an object into a struct or class. This binds all the elements that belong together into a single entity. You can then make an array of these objects:
#include <MAX31856.h>
#define SCK 13
#define CS0 10
#define CS1 9
#define CS2 8
#define CS3 7
#define SDI 11
#define SDO 12
struct SensorStruct {
const char * const sensorName;
MAX31856 tempSensor;
SensorStruct(const char * const n, uint8_t sdi, uint8_t sdo, uint8_t cs, uint8_t ck) :
sensorName(n), tempSensor(sdi, sdo, cs, ck) {}
};
SensorStruct sensorArray[] = {
{"CHTM", SDI, SDO, CS0, SCK},
{"CHTP", SDI, SDO, CS1, SCK},
{"EGTM", SDI, SDO, CS2, SCK},
{"EGTP", SDI, SDO, CS3, SCK}
};
Then, the individual elements of array member ‘i’ would be accessed as:
Serial.println(sensorArray[i].sensorName);
Serial.println(sensorArray[i].tempSensor.readThermocouple(FAHRENHEIT));
i tried implementing that, but nothing appears in my serial monitor now.
#include <MAX31856.h>
#define SCK 13
#define CS0 10
#define CS1 9
#define CS2 8
#define CS3 7
#define SDI 11
#define SDO 12
#define NUM_MAX31856 4
#define CR0_INIT (CR0_AUTOMATIC_CONVERSION + CR0_OPEN_CIRCUIT_FAULT_TYPE_K /* + CR0_NOISE_FILTER_50HZ */)
#define CR1_INIT (CR1_AVERAGE_2_SAMPLES + CR1_THERMOCOUPLE_TYPE_K)
#define MASK_INIT (~(MASK_VOLTAGE_UNDER_OVER_FAULT + MASK_THERMOCOUPLE_OPEN_FAULT))
MAX31856 *TemperatureSensor[NUM_MAX31856];
struct SensorStruct {
const char * const sensorName;
MAX31856 tempSensor;
SensorStruct(const char * const n, uint8_t sdi, uint8_t sdo, uint8_t cs, uint8_t ck) :
sensorName(n), tempSensor(sdi, sdo, cs, ck) {}
};
SensorStruct sensorArray[] = {
{"CHTM", SDI, SDO, CS0, SCK},
{"CHTP", SDI, SDO, CS1, SCK},
{"EGTM", SDI, SDO, CS2, SCK},
{"EGTP", SDI, SDO, CS3, SCK}
};
void setup() {
// Display temperatures using the serial port
Serial.begin(9600);
delay(500);
Serial.println("Quad MAX31856 Sample application");
// Initializing the MAX31855's registers
for (int i = 0; i < NUM_MAX31856; i++) {
TemperatureSensor[i]->writeRegister(REGISTER_CR0, CR0_INIT);
TemperatureSensor[i]->writeRegister(REGISTER_CR1, CR1_INIT);
TemperatureSensor[i]->writeRegister(REGISTER_MASK, MASK_INIT);
}
// Wait for the first samples to be taken
delay(200);
}
void loop() {
for (int i = 0 ; i < NUM_MAX31856; i++) {
double temperature = TemperatureSensor[i]->readJunction(FAHRENHEIT);
if (temperature == NO_MAX31856)
continue;
temperature = TemperatureSensor[i]->readThermocouple(FAHRENHEIT);
if (temperature == NO_MAX31856)
continue;
Serial.println(sensorArray[i].sensorName);
Serial.println(sensorArray[i].tempSensor.readThermocouple(FAHRENHEIT));
Serial.print("\t");
}
Serial.println();
}
void printTemperature(double temperature) {
switch ((int) temperature) {
case FAULT_OPEN:
Serial.print("OPEN");
break;
case FAULT_VOLTAGE:
Serial.print("VOLT");
break;
case NO_MAX31856:
Serial.print("NOM");
break;
default:
Serial.print(temperature);
break;
}
Serial.print(" ");
}
gfvalvo
September 17, 2019, 7:58pm
#7
That's because you're still trying to do stuff using the array of pointers. All access to the MAX31856 functions should be through the array of structs. Get rid of the pointer array.
What stuff needs to be deleted?
gfvalvo
September 17, 2019, 11:23pm
#9
Qhergt1987:
What stuff needs to be deleted?
As I said, the pointer array:
MAX31856 *TemperatureSensor[NUM_MAX31856];
and any accesses to MAX31856 objects through it (like your're doing with writeRegister()).
Instead, use the tempSensor member of each element in the struct array that you defined:
SensorStruct sensorArray[] = {
I showed you the correct syntax for doing this with:
Serial.println(sensorArray[i].tempSensor.readThermocouple(FAHRENHEIT));
So:
sensorArray[i].tempSensor.writeRegister(REGISTER_CR0, CR0_INIT);
etc, etc, etc.