Please Help! MAX31855 Thermocouple

This is the code that I have found. Its a great code, but I need to be able to get the temp. in (int) so I can get the avg and enter into my pid code. Can anyone please help.

#include <MAX31855.h>

// Pin connections to the MAX31855x8 board
// The power requirement for the board is less than 2mA. Most microcontrollers can source or sink a lot more
// than that one each I/O pin. For example, the ATmega328 supports up to 20mA. For convenience, the board
// is placed directly on top of a row of I/O pins on the microcontroller. Power is supplied to the board by
// holding the GND pin low and the VIN pin high
#define GND 3
#define T0 4
#define T1 5
#define T2 6
#define VIN 7
#define MISO 8
#define CS 9
#define SCK 10

// Create the temperature object, defining the pins used for communication
MAX31855 temp = MAX31855(MISO, CS, SCK);

void setup() {
// Display temperatures using the serial port
Serial.begin(9600);

// Initialize pins
pinMode(GND, OUTPUT);
pinMode(T0, OUTPUT);
pinMode(T1, OUTPUT);
pinMode(T2, OUTPUT);
pinMode(VIN, OUTPUT);

// Power up the board
digitalWrite(GND, LOW);
digitalWrite(VIN, HIGH);
delay(200);
}

void loop () {
// Display the junction temperature
float temperature = temp.readJunction(FAHRENHEIT);
Serial.print(“J=”);
printTemperature(temperature);
//------------------------------------------------------------------------------------------------------------
// Display the temperatures of the 8 thermocouples ((THIS CODE USES ARRAY TO SPIT OUT THE THEMPERATURE VALUES
for (int therm=0; therm<8; therm++) { //((I NEED TO BE ABLE TO SEE AND USE THE SPECIFIC VALUES
// Select the thermocouple //((T0= ,T1= ,T2= … SO I CAN GET TAVG AND PLUG IT INTO PID CODE!!!
digitalWrite(T0, therm & 1? HIGH: LOW);
digitalWrite(T1, therm & 2? HIGH: LOW);
digitalWrite(T2, therm & 4? HIGH: LOW);
// The MAX31855 takes 100ms to sample the thermocouple.
// Wait a bit longer to be safe. We’ll wait 0.125 seconds
delay(125);

temperature = temp.readThermocouple(FAHRENHEIT);
if (temperature == FAULT_OPEN)
continue;
Serial.print(" T");
Serial.print(therm);
Serial.print("=");
printTemperature(temperature);
}
//---------------------------------------------------------------------------------------------------------
Serial.println();
}

// Print the temperature, or the type of fault
void printTemperature(double temperature) {
switch ((int) temperature) {
case FAULT_OPEN:
Serial.print(“FAULT_OPEN”);
break;
case FAULT_SHORT_GND:
Serial.print(“FAULT_SHORT_GND”);
break;
case FAULT_SHORT_VCC:
Serial.print(“FAULT_SHORT_VCC”);
break;
case NO_MAX31855:
Serial.print(“NO_MAX31855”);
break;
default:
Serial.print(temperature);
break;
}
Serial.print(" ");
}

So what's the issue?

The value you need is the one called temperature (that value gets set after the delay(125)) - instead of printing it out with printTemperature(), feed it to a function that will do the averaging in an appropriate manner

(when I just need to smooth inputs, I do quick things like oldval=(oldval*9+newval)/10; rather than storing all the recent values for a real rolling average)

I need to data log as well. So I need to be able to display all the temperatures and also the average. The rolling rolling average I will use as an input to my PID code. Thanks for the help.

mvaldezrubio:
I need to data log as well. So I need to be able to display all the temperatures and also the average. The rolling temp average I will use as an input to my PID code. Thanks for the help.

"// Pin connections to the MAX31855x8 board"
The code is for a board with eight separate MAX31855 chips, each connected to a separate thermocouple. Do you have that board? Did you want to calculate moving averages for all eight thermocouples?

I have the MAX31855x8 board
It uses a binary code to output.
This is what I have as of right now. I got the data to come out through the counter and if statements but now if I try to see just T0 at any given time it gives me a random temperature. It may be any from T0 to T7. I do not know why it is doing this. :’(

//-------------------------------------------------------------------------------------------------------------------
//Moises Valdez-Rubio
//Composites Oven Controls System
//ALVA MANUFACTURING
//Start date: March 1,2015
//Finish date: March ,2015
//-------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------
//Thermocouple library
#include <MAX31855.h>
//-------------------------------------------------------------------------------------------------------------------
//Screen library
#include<LiquidCrystal.h> //include the library code:
LiquidCrystal lcd(12,11,53,52,51,50); //initialize the library with the numbers of the interface pins:
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
int inPin = 48; // choose the input pin (for a pushbutton)
int button = 0; // variable for reading the pin status
int led = 13; // LED indicator for Temp 0,1,2,3…
int counter = 0;
//-------------------------------------------------------------------------------------------------------------------
int T0=0, T1=0, T2=0, T3=0, T4=0, T5=0, T6=0, T7=0;
int TAVG = 0;
extern void Storefile(int); //initialize void (for storing data)
//-------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------
//Thermocouple setup
//Pin connections to the MAX31855x8 board
//The power requirement for the board is less than 2mA. Most microcontrollers can source or sink a lot more
//than that one each I/O pin. For example, the ATmega328 supports up to 20mA. For convenience, the board
//is placed directly on top of a row of I/O pins on the microcontroller. Power is supplied to the board by
//holding the GND pin low and the VIN pin high
#define GND 2
#define T0 3
#define T1 4
#define T2 5
#define VIN 6
#define MISO 7
#define CS 8
#define SCK 9

// Create the temperature object, defining the pins used for communication
MAX31855 temp = MAX31855(MISO, CS, SCK);
//-------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------
void setup() {
//-----------------------------------------------------------------------------------------------
pinMode(inPin, INPUT); // declare pushbutton as input

pinMode(led, OUTPUT);

lcd.begin(16,2); //set up the LCD’s number of columns and rows:
//-----------------------------------------------------------------------------------------------
lcd.setCursor(0,0);
lcd.print(" ALVA"); //print a message to the LCD;
lcd.setCursor(0,1);
lcd.print(" MANUFACTURING"); //print a message to the LCD;
delay(1000);
lcd.clear();

lcd.setCursor(0,0);
lcd.print(" WELCOME"); //print a message to the LCD;
delay(1000);

lcd.setCursor(0,0);
lcd.print(" COMPOSITES"); //print a message to the LCD;
lcd.setCursor(0,1);
lcd.print(" CURING OVEN");
delay(1000);
lcd.clear();
//-----------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------
// Display temperatures using the serial port
Serial.begin(9600);

// Initialize pins
pinMode(GND, OUTPUT);
pinMode(T0, OUTPUT);
pinMode(T1, OUTPUT);
pinMode(T2, OUTPUT);
pinMode(VIN, OUTPUT);

// Power up the board
digitalWrite(GND, LOW);
digitalWrite(VIN, HIGH);
delay(200);
}
//-------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------
void loop () {
// Display the junction temperature
float temperature = temp.readJunction(FAHRENHEIT);
Serial.print(“J=”);
printTemperature(temperature);

// Display the temperatures of the 8 thermocouples
for (int therm=0; therm<8; therm++) {
// Select the thermocouple
digitalWrite(T0, therm & 1? HIGH: LOW);
digitalWrite(T1, therm & 2? HIGH: LOW);
digitalWrite(T2, therm & 4? HIGH: LOW);
// The MAX31855 takes 100ms to sample the thermocouple.
// Wait a bit longer to be safe. We’ll wait 0.125 seconds
delay(125);

temperature = temp.readThermocouple(FAHRENHEIT);
if (temperature == FAULT_OPEN)
continue;
Serial.print(" T");
Serial.print(therm);
Serial.print("=");
printTemperature(temperature);
Storefile(temperature);
}

Serial.println();
}

// Print the temperature, or the type of fault
void printTemperature(double temperature) {
switch ((int) temperature) {
case FAULT_OPEN:
Serial.print(“FAULT_OPEN”);
break;
case FAULT_SHORT_GND:
Serial.print(“FAULT_SHORT_GND”);
break;
case FAULT_SHORT_VCC:
Serial.print(“FAULT_SHORT_VCC”);
break;
case NO_MAX31855:
Serial.print(“NO_MAX31855”);
break;
default:
Serial.print(temperature);
break;
}
Serial.print(" ");
}

void Storefile(int therm){

while(1){//while loop
if(counter==0){
T0==therm;
counter++;
lcd.print(" T0");
lcd.print("=");
lcd.print(therm);
break;
}//counter 0

if(counter==1){
T1==therm;
counter++;
lcd.setCursor(0,0);
lcd.print(" T1");
lcd.print("=");
lcd.print(therm);
lcd.clear();
//forward file here
break;
}
if(counter==2){
T2==therm;
counter++;
lcd.setCursor(0,0);
lcd.print(" T2");
lcd.print("=");
lcd.print(therm);
lcd.clear();
break;
}
if(counter==3){
T3==therm;
counter++;
lcd.setCursor(0,0);
lcd.print(" T3");
lcd.print("=");
lcd.print(therm);
lcd.clear();
break;
}
if(counter==4){
T4==therm;
counter++;
float temperature = temp.readJunction(FAHRENHEIT);
temperature = temp.readThermocouple(FAHRENHEIT);
if (temperature == FAULT_OPEN)
continue;
lcd.setCursor(0,1);
lcd.print(" T4");
lcd.print("=");
lcd.print(temperature);
break;
}
if(counter==5){
T5==therm;
counter++;
lcd.setCursor(0,0);
lcd.print(" T5");
lcd.print("=");
lcd.print(therm);
lcd.clear();
break;
}
if(counter==6){
T6==therm;
counter++;
lcd.setCursor(0,0);
lcd.print(" T6");
lcd.print("=");
lcd.print(therm);
lcd.clear();
break;
}
if(counter==7){
T7==therm;
counter++;//add counter
lcd.setCursor(0,0);
lcd.print(" T7");
lcd.print("=");
lcd.print(therm);
lcd.clear();
break;
}

if(counter==8){
TAVG=(T0+T1+T2+T3+T4+T5+T6+T7)/8;
counter=0;//reset counter to 0
lcd.setCursor(0,0);
lcd.print(" TAVG");
lcd.print("=");
lcd.print(therm);
lcd.clear();
//where to store
break;
}//if statement

}//while loop
}//void storefile

int T0=0, T1=0, T2=0, T3=0, T4=0, T5=0, T6=0, T7=0;
#define T0   3
#define T1   4
#define T2   5

Having the names T0, T1, and T2 defined twice is going to cause a lot of problems. One set or the other should use different names.

      T2==therm;

This statement doesn't do anything. You should be saying "T2 = therm;" but since you have T2 defined as '5' and you can't say "5 = therm;" you would get an error. Fix the first problem, then you can fix this one.

DrAzzy:

One thing I have not seen is that the rolling average

smoothed = (9.0*smoothed + newval)/10.0

needs to be kickstarted, since if you start with smoothed = 0.0, the first time through you will get newval/10.0, and it will take 10 iterations to get to a reasonable value.

You either need to use a regular average for the first 10 iterations, or set smoothing = newval the first time through.

I figured it would cause me problems. I renamed the bottom half to T00,T01, etc… I still get the same problem though. If I use the binary code to name each temp. would that help? My problem with that is that I have no idea how to do that. :’( I have looked around, but still do not know. Any pointers guys?