Hello all,
I am making a sous vide cooker and am using a SSR and the Maxim one wire temperature sensor.
I have been struggling with the code, I am trying to use the PID library V1, but cannot make the output go high no matter what the temperature is. Here is my code so far:
#include <OneWire.h>
#include <PID_v1.h>
#define RelayPin 6
//init the one wire interface on pin 10
OneWire ow(10);
//write here the address you receive from the other program
byte sensor[8] = {0x10, 0x50, 0xC, 0x51, 0x02, 0x08, 0x00, 0x9A};
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup() {
Serial.begin(9600);
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 95;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void writeTimeToScratchpad(byte* address){
//reset the bus
ow.reset();
//select our sensor
ow.select(address);
//CONVERT T function call (44h) which puts the temperature into the scratchpad
ow.write(0x44,1);
//sleep a second for the write to take place
delay(1000);
}
void readTimeFromScratchpad(byte* address, byte* data){
//reset the bus
ow.reset();
//select our sensor
ow.select(address);
//read the scratchpad (BEh)
ow.write(0xBE);
for (byte i=0;i<9;i++){
data = ow.read();
-
}*
}
float getTemperature(byte* address){ -
int tr;*
-
byte data[12];*
-
writeTimeToScratchpad(address);*
-
readTimeFromScratchpad(address,data);*
-
//put in temp all the 8 bits of LSB (least significant byte)*
-
tr = data[0];*
-
//check for negative temperature*
-
if (data[1] > 0x80){*
-
tr = !tr + 1; //two's complement adjustment*
_ tr = tr * -1; //flip value negative._ -
}*
-
//COUNT PER Celsius degree (10h)*
-
int cpc = data[7];*
-
//COUNT REMAIN (0Ch)*
-
int cr = data[6];*
-
//drop bit 0*
-
tr = tr >> 1;*
-
//calculate the temperature based on this formula :*
-
//TEMPERATURE = TEMP READ - 0.25 + (COUNT PER Celsius Degree - COUNT REMAIN)*
// (COUNT PER Celsius Degree) -
return tr - (float)0.25 + (cpc - cr)/(float)cpc;*
}
//fahrenheit to celsius conversion
float f2c(float val){
- float aux = val - 32;*
_ return (aux * 5 / 9);_
}
//celsius to fahrenheit conversion
float c2f(float val){
_ float aux = (val * 9 / 5);_
- return (aux + 32);*
}
void loop()
{ - float temp;*
- float tmp2;*
- tmp2 = getTemperature(sensor);*
- temp = c2f(tmp2);*
// Serial.print("Temp = ");
// Serial.print(temp);
Serial.println(" ");
// Serial.print(" F or ");
//Serial.print(tmp2);
//Serial.println(" C");
-
//wait 30 seconds*
// delay(30000); -
Input = (double)temp;*
-
myPID.Compute();*
-
Serial.print((double)Input);*
-
//Serial.print(temp);*
_ /************************************************_
_ * turn the output pin on/off based on pid output_
_ ************************************************/_ -
unsigned long now = millis();*
-
if(now - windowStartTime>WindowSize)*
-
{ //time to shift the Relay Window*
-
windowStartTime += WindowSize;*
-
}*
-
if(Output > now - windowStartTime) digitalWrite(RelayPin,HIGH);*
-
else digitalWrite(RelayPin,LOW);*
-
delay(1500);*
}
Any input would be greatly appreciated.
Thanks