I2C OLED code Breaks working PID temperature controller

Hey guys hope this is the right place for my questions.

Given this is my first crack at C++ Please excuse my ignorance/noobness :confused:

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 :frowning: and the longer I stare at the code the less is seems to make sense :slightly_frowning_face:

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)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Hi,
OPs pic.


You are using pin0 as an output, this is along with pin1 are the programming pins, so you may improve things if you use a pin other than 0 or 1 as your output to the SSR.

Tom... :slight_smile:

Thank you for the reply Tom! I've edited my post to include the code as per your post! As for the pin I will review the build as soon as i get home from work, and post back my results.

The help is much appreciated!