I am new to Programming please help!
The project is using Arduino MEGA, Adafruit MAX31856 universal thermocouple amplifier W/ K-type thermocouple, 16X2 LCD and SSR. I am using the pid algorithm for my temperature controller but it seems the Pid is not working. A got an output read of 0.0 all the time and my SSR is always high. The project is using two push button switch to change the value of the Setpoint. I want to set the Kp,Ki and Kd values manually if the PID works. I already double check the wiring.
I dont know what is the problem with my coding.
Is there someone that has the same Project that i am creating now?
Please help!
here is the code
#include <SPI.h>
#include <PID_v1.h>
#include <Adafruit_MAX31856.h>
#include <LiquidCrystal.h>
#define RELAY 45
const int rs = 2, en = 3, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Adafruit_MAX31856 max = Adafruit_MAX31856(10, 11, 12, 13);
const int LED_RED=53; //Red LED
const int LED_GREEN=51; //Green LED
//Key connections with arduino
const int up_key=44;
const int down_key=46;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
double kp=45,ki=0,kd=0;
int SetPoint = 200;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,kp,ki,kd, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup() {
// put your setup code here, to run once:
pinMode(LED_RED,OUTPUT);
pinMode(LED_GREEN,OUTPUT);
pinMode(RELAY,OUTPUT);
pinMode(up_key,INPUT);
pinMode(down_key,INPUT);
//Pull up for setpoint keys
digitalWrite(up_key,HIGH);
digitalWrite(down_key,HIGH);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("PID temp control");
lcd.setCursor(0,1); //Move coursor to second Line
lcd.print("Temp. Controller");
digitalWrite(LED_GREEN,HIGH); //Green LED Off
digitalWrite(LED_RED,LOW); //Red LED On
digitalWrite(RELAY,LOW); //Turn off Relay
Serial.begin(115200);
max.begin();
max.setThermocoupleType(MAX31856_TCTYPE_K);
Serial.print("Thermocouple type: ");
switch ( max.getThermocoupleType() ) {
case MAX31856_TCTYPE_B: Serial.println("B Type"); break;
case MAX31856_TCTYPE_E: Serial.println("E Type"); break;
case MAX31856_TCTYPE_J: Serial.println("J Type"); break;
case MAX31856_TCTYPE_K: Serial.println("K Type"); break;
case MAX31856_TCTYPE_N: Serial.println("N Type"); break;
case MAX31856_TCTYPE_R: Serial.println("R Type"); break;
case MAX31856_TCTYPE_S: Serial.println("S Type"); break;
case MAX31856_TCTYPE_T: Serial.println("T Type"); break;
case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break;
case MAX31856_VMODE_G32: Serial.println("Voltage x8 Gain mode"); break;
default: Serial.println("Unknown"); break;
}
windowStartTime = millis();
//initialize the variables we're linked to
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Serial.print("Cold Junction Temp: "); Serial.println(max.readCJTemperature());
Serial.print("Thermocouple Temp: "); Serial.println(max.readThermocoupleTemperature());
Serial.print("Setpoint: "); Serial.println(SetPoint);
Serial.print("Output: "); Serial.println(Output);
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(max.readThermocoupleTemperature());
//Get user input for setpoints
if(digitalRead(down_key)==LOW)
{
if(SetPoint>0)
{
SetPoint--;
}
}
if(digitalRead(up_key)==LOW)
{
if(SetPoint<600)
{
SetPoint++;
}
}
if( Input > SetPoint)
{
digitalWrite(RELAY,LOW);
digitalWrite(LED_RED,LOW);
digitalWrite(LED_GREEN,HIGH); //Turn on Green LED
}
else
{
digitalWrite(RELAY,HIGH);
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_RED,HIGH); //Turn on RED LED
}
//Display Set point on LCD
lcd.setCursor(0,1);
lcd.print("Set Point:");
lcd.print(SetPoint);
lcd.print("C ");
Input = max.readThermocoupleTemperature()- 25;
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,HIGH);
else digitalWrite(RELAY,LOW);
}
Here's the data sheet and libraries