MLX+PID Library

Hi,

i tried to use the PID library together with the MLX temperature sensor (SDA/SCL)

program compiles and uploads but nothing happens.
cant figure it out. does the PID lib only work with Analog input?

please review someone thanks! :slight_smile:

#include <PID_v1.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
#define RELAY_PIN 12

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;

PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

int WindowSize = 5000;
int temp = mlx.readObjectTempC(); 

unsigned long windowStartTime;

void setup()
{
  
Serial.begin(9600);

Serial.println("Adafruit MLX90614 test");  

mlx.begin();  

  windowStartTime = millis();
 
  //initialize the variables we're linked to
  Setpoint = 100;

  //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("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C"); 
  
 

Serial.println();
delay(50);
  
  Input = temp;
  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);

}

note that PID_v1 has a fixed DeltaT

bool PID::Compute()
{
   if(!inAuto) return false;
   unsigned long now = millis();
   unsigned long timeChange = (now - lastTime);
   if(timeChange>=SampleTime)
   {
///... PID Calculation
      return ture;
   }
}

so doing something like

// ... In the setup() add:
    myPID.SetSampleTime(50); // PID loop will only be true every 50 ms Rather than unsin delay(50); in your loop
}

void loop()
{
  temp = mlx.readObjectTempC(); //<<< missing in the loop <<< don't forget to get your temperature again
  Input = temp;
  if(myPID.Compute()){
    Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C"); 
    Serial.println();
  /************************************************
   * 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);
  }
}

Z