#include <PID_v1.h>
#include <OneWire.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
const int TEMP_PIN=2;
const int ssrPin=0;
const int upPin=8;
const int downPin=3;
//const int Setpoint_address=0;
const int SvHiByteAddress=10;
const int SvLoByteAddress=11;
int TempF100;
const int WindowSize = 1000;
double Setpoint, Input, Output;
unsigned long EEPROMTime, windowStartTime, markTime, displayTime;
LiquidCrystal lcd(10,1,9,4,7,5,6);
OneWire ds(TEMP_PIN);
PID myPID(&Input, &Output, &Setpoint,120,0.05,1.0, DIRECT);
void setup()
{
lcd.begin(8,2);
pinMode(ssrPin,OUTPUT);
pinMode(upPin,INPUT_PULLUP);
pinMode(downPin,INPUT_PULLUP);
int SetpointAsInt = word(EEPROM.read(SvHiByteAddress),EEPROM.read(SvLoByteAddress));
Setpoint = SetpointAsInt;
myPID.SetOutputLimits(0, WindowSize);
myPID.SetMode(AUTOMATIC);
}
void loop()
{
getTemp();
displayLCD();
getOutput();
controlSSR();
readButtons();
saveSetpoint();
}
void getTemp()
{
byte data[12];
ds.reset();
ds.skip();
ds.write(0x44,1);
byte present = ds.reset();
ds.skip();
ds.write(0xBE);
for (int i = 0; i < 9; i++)
{
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
int TempR = ((MSB << 8) | LSB);
Input = TempR;
}
void saveSetpoint()
{
if (millis()>EEPROMTime)
{
int SetpointAsInt=(int)Setpoint;
byte SetpointLoByte=lowByte(SetpointAsInt);
if (EEPROM.read(SvLoByteAddress)!=SetpointLoByte) EEPROM.write(SvLoByteAddress,SetpointLoByte);
byte SetpointHiByte=highByte(SetpointAsInt);
if (EEPROM.read(SvHiByteAddress)!=SetpointHiByte) EEPROM.write(SvHiByteAddress,SetpointHiByte);
EEPROMTime+=1000;
}
}
void controlSSR()
{
if ((millis()-markTime)>WindowSize) markTime=millis();
if ((millis()-markTime)<Output) digitalWrite(ssrPin,HIGH);
else digitalWrite(ssrPin,LOW);
}
void displayLCD()
{
lcd.setCursor(0,0);
TempF100=((Setpoint*90/8)+3200);
printtemp();
lcd.setCursor(0,1);
TempF100=((Input*90/8)+3200);
printtemp();
}
void printtemp()
{
byte TempF_whole = TempF100 / 100;
byte TempF_dec = TempF100 % 100;
if (TempF_whole<100) lcd.print(" ");
lcd.print(TempF_whole);
lcd.print(".");
if (TempF_dec<10) lcd.print("0");
lcd.print(TempF_dec);
lcd.print(char(223));
lcd.print("F");
}
void getOutput()
{
int TempError=abs(Setpoint-Input);
if (TempError>64) myPID.SetTunings(120,0.0,0.0);
else myPID.SetTunings(60,0.05,0.0);
myPID.Compute();
}
void readButtons()
{
if (digitalRead(upPin)==LOW)
{
Setpoint++;
delay(5);
}
if (digitalRead(downPin)==LOW)
{
Setpoint--;
delay(5);
}
}
Enter your email address to join: