Hello all! I'm in need of some assistance. I'm reading temperatures from a k type thermocouple from Omega (attached below) using a MAX31850. I'm getting temperature values using the onewire library but I'm having trouble extracting one of those temperatures so that the extracted temperature can be compared with the changing temperature readings (in order to turn a heating source on and off). Would anyone mind taking a look at my code and show me what I'm doing wrong or what I could be doing better to improve performance. Right now it displays the temperature and when I push a button it "saves" that temperature but only for a few seconds (or not at all). I've also tried using the dallastemp with onewire library, but that gives me incorrect temperature readings. I have green and red LEDs to act as the power supply turning on and off.
#include <OneWire.h>
#define buttonS 2
#define BLUE 4
#define RED 5
#define GREEN 6
int stateS = 0;
int currentS = 0;
float celsius1 = 0.0;
#define TYPE_DS18S20 0
#define TYPE_DS18B20 1
#define TYPE_DS18S22 2
#define TYPE_MAX31850 3
OneWire ds(7); // on pin 10 (a 4.7K pullup resistor is necessary)
void setup(void)
{
Serial.begin(9600);
pinMode(buttonS, INPUT);
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(BLUE, LOW);
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
}
void loop(void)
{
stateS = digitalRead(buttonS);
byte i;
byte present = 0;
byte temptype;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr))
{
ds.reset_search();
delay(250);
return;
}
if (OneWire::crc8(addr, 7) != addr[7])
{
Serial.println("CRC is not valid!");
return;
}
// the first ROM byte indicates which chip
switch (addr[0])
{
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
temptype = TYPE_DS18S20;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
temptype = TYPE_DS18B20;
break;
case 0x22:
Serial.println(" Chip = DS1822");
temptype = TYPE_DS18S22;
break;
case 0x3B:
Serial.println(" Chip = MAX31850");
temptype = TYPE_MAX31850;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(750);
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++)
{
data[i] = ds.read();
}
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (temptype == TYPE_DS18S20)
{
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10)
{
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
}
else if (temptype == TYPE_MAX31850)
{
if (raw & 0x01)
{
Serial.println("**FAULT!**");
return;
}
}
else
{
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
Serial.print("Temperature = ");
Serial.println(celsius);
Serial.println();
if (stateS != currentS)
{
if (stateS == LOW)
{
Serial.println("--------- TEMP SET ---------");
celsius1 = celsius;
Serial.println(celsius1);
digitalWrite(BLUE, HIGH);
delay(500);
digitalWrite(BLUE, LOW);
delay(500);
}
}
delay(100);
if ((celsius1 <= celsius) || (celsius1 = 0))
{
digitalWrite(GREEN, HIGH);
}
else if (celsius1 > celsius)
{
digitalWrite(RED, HIGH);
}
currentS = stateS;
Serial.print("SET TEMP = ");
Serial.println(celsius1);
}