//LCD Screen
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(14, 15, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
//PID
#include <PID_v1.h>
#define PIN_INPUT 0
#define RELAY_PIN 10
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=2, Ki=0, Kd=0;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
//Thermistor
int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
double TargetT;
// Buttons
//constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 8; // the number of the pushbutton pin
const int buttonPin2 = 9; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
double buttonCount = 80;
// Relay pin is controlled with D10. The active wire is connected to Normally Closed and common
//int relay = 10;
void setup() {
Serial.begin(9600);
//LCD Setup
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
//LED Light
pinMode(13, OUTPUT);
//Button
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
//Relay
// pinMode(relay, OUTPUT);
//digitalWrite(relay, LOW);
//PID
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = buttonCount;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop() {
//Thermistor
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
T = T - 273.15;
T = (T * 9.0)/ 5.0 + 32.0;
Input = T;
//Serial Print
Serial.print("Temperature: ");
Serial.print(T);
Serial.println(" F");
//Serial Print
Serial.print("Set Temp: ");
Serial.print(Setpoint);
Serial.println(" F");
//Serial Print
Serial.print("Input: ");
Serial.print(Input);
Serial.println(" F");
Serial.print("Output: ");
Serial.print(Output);
Serial.println();
//Button
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
//Button Count
if (buttonState1 == HIGH) {
buttonCount = buttonCount - 1;
}
if (buttonState2 == HIGH) {
buttonCount = buttonCount + 1;
}
//LCD Display
lcd.print("Set T= ");
lcd.print(buttonCount);
lcd.print(" F");
lcd.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Temp = "); // Prints "Arduino" on the LCD
lcd.print(T);
lcd.print(" F");
lcd.setCursor(0,0);
delay(100);
Setpoint=TargetT;
TargetT = buttonCount;
//Temp Control
if (T <= TargetT)
{
digitalWrite(13, HIGH); //Turn LED on
digitalWrite(relay, HIGH); //Turn Heater on
}
if (T > TargetT )
{
digitalWrite(13, LOW); //Turn LED off
digitalWrite(relay, LOW); //Turn Heater off
}
Input = T;
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if (millis() - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
else digitalWrite(RELAY_PIN, LOW);
}