Hey guys hope this is the right place for my questions.
Given this is my first crack at C++ Please excuse my ignorance/noobness ![]()
So Im trying to Use this PID code as the basis for my Temperature Controller, and this example/sketch as the basis for the OLED code.
The first sketch file works perfectly, holds temps steady!
Second One not so much! the temp keeps ramping up with no sighs of adjustments :- , wile the all OLED code works fine with the exception of the Temperature value(displaying 3717.950 instead of thermocouple value)
Im failing to see whats breaking it
and the longer I stare at the code the less is seems to make sense ![]()
any help would be humbling
-John
Sketch 1
#include "SPI.h"
#include "MAX31855.h"
#include "PID_v1.h"
//
//Arduino Pins
//
//CSB of MAX31855 thermocouple. By using separate CSBs, multiple devices can use the same SPI bus.
byte thermo1_csb = 7;
//Pin to control relay that switches the heating element
byte temp_relay0_Pin = 0;
//
//PID calculation parameters
//
//Value read by thermocouple sensor
double temp_read1;
//Temperature that system is currently maintaining
double temp_set1 = 125;
//Control value resulting from PID calculation
double Output_T1;
//
//Library Objects
//
MAX31855 thermo;
PID PID_temp1(&temp_read1, &Output_T1, &temp_set1, 1, 5, 0.5, DIRECT);
void setup()
{
 //Initiate thermocouple
 SPI.begin();
 thermo.setup(thermo1_csb);
 //Initiate heating element relay
 pinMode(temp_relay0_Pin, OUTPUT);
 digitalWrite(temp_relay0_Pin, LOW);
}
//This function reads the thermocouple sensor and translates the value into temp_read1
void read_sensors()
{
 thermo.read_temp();
 temp_read1 = thermo.thermocouple_temp * 1.8 + 32;
}
//This function computes the output control value from the thermocouple reading and current set point
//and then turns the heating element on and off accordingly
void run_PID()
{
 uint16_t low_WindowSize = 0;
 uint16_t high_WindowSize = 1000;
 uint32_t windowStartTime = millis();
 //Specify the links and initial tuning parameters
 PID_temp1.SetOutputLimits(low_WindowSize, high_WindowSize);
 //PID_temp1.SetTunings(Kp, Ki, Kd);
 PID_temp1.SetMode(AUTOMATIC);
 PID_temp1.Compute();
 //This section controls the heating element
 while (millis() < (high_WindowSize - low_WindowSize) + windowStartTime)
 {
  if (Output_T1 > (millis() - windowStartTime))
  {
   digitalWrite(temp_relay0_Pin, HIGH);
  }
  else
  {
   digitalWrite(temp_relay0_Pin, LOW);
  }
 }
}
void loop()
{
 read_sensors();
 run_PID();
}
Sketch 2
#include "SPI.h"
#include "MAX31855.h"
#include "PID_v1.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
// this is the Width and Height of Display which is 128 xy 32
#define LOGO16_GLCD_HEIGHT 32
#define LOGO16_GLCD_WIDTHÂ 128
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//
//Arduino Pins
//
//CSB of MAX31855 thermocouple. By using separate CSBs, multiple devices can use the same SPI bus.
byte thermo1_csb = 7;
//Pin to control relay that switches the heating element
byte temp_relay0_Pin = 0;
//
//PID calculation parameters
//
//Value read by thermocouple sensor
double temp_read1;
//Temperature that system is currently maintaining
double temp_set1 = 125;
//Control value resulting from PID calculation
double Output_T1;
//
//Library Objects
//
MAX31855 thermo;
PID PID_temp1(&temp_read1, &Output_T1, &temp_set1, 1, 5, 0.5, DIRECT);
void setup()
{
 //Initiate thermocouple
 SPI.begin();
 thermo.setup(thermo1_csb);
 //Initiate heating element relay
 pinMode(temp_relay0_Pin, OUTPUT);
 digitalWrite(temp_relay0_Pin, LOW);
 Serial.begin(9600);
 // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
 display.clearDisplay(); /* Clear display */
 display.setCursor(15, 5); /* Set x,y coordinates */
 display.setTextSize(1); /* Select font size of text. Increases with size of argument. */
 display.setTextColor(WHITE); /* Color of text*/
 display.println("DIY V1"); /* Text to be displayed */
 display.display();
 delay(3000);
 display.clearDisplay();
}
//This function reads the thermocouple sensor and translates the value into temp_read1
void read_sensors()
{
 thermo.read_temp();
 temp_read1 = thermo.thermocouple_temp * 1.8 + 32;
}
//This function computes the output control value from the thermocouple reading and current set point
//and then turns the heating element on and off accordingly
void run_PID()
{
 uint16_t low_WindowSize = 0;
 uint16_t high_WindowSize = 1000;
 uint32_t windowStartTime = millis();
 //Specify the links and initial tuning parameters
 PID_temp1.SetOutputLimits(low_WindowSize, high_WindowSize);
 //PID_temp1.SetTunings(Kp, Ki, Kd);
 PID_temp1.SetMode(AUTOMATIC);
 PID_temp1.Compute();
 //This section controls the heating element
 while (millis() < (high_WindowSize - low_WindowSize) + windowStartTime)
 {
  if (Output_T1 > (millis() - windowStartTime))
  {
   digitalWrite(temp_relay0_Pin, HIGH);
  }
  else
  {
   digitalWrite(temp_relay0_Pin, LOW);
  }
 }
}
void loop()
{
 read_sensors();
 run_PID();
 String vString = String(temp_read1, 3);// using a float and the
 display.clearDisplay();
 Text("Temprature:     ", 4, 3, 1, false);
 Text(vString, 72, 3, 1, false);
 Text("F", 110, 3, 1, false);
 Text("test line", 4, 11, 1, false);
 display.drawLine(1, 37, 100, 37, WHITE);
 display.drawRect(1, 1, 126, 31, WHITE);
 display.display();
 delay(2000);
}
void Text(String text, int x, int y, int size, boolean d) {
 display.setTextSize(size);
 display.setTextColor(WHITE);
 display.setCursor(x, y);
 display.println(text);
 if (d)
  display.display();
}
test1.ino (1.89 KB)
test2.ino (3.41 KB)
