PID library, controller always ON

hi everyone,

Im having trouble with the PID library and after looking for more info and other sketches I cant seem to find where my issue is… The controller is for turning ON/OFF a fan to cool down my fishtank… I basically used the relay example and adapted it to my sketch…

Input is temp from a ds18b20, output is a relay (LOW is ON, and HIGH is OFF)

#include <PID_v1.h>
#define RelayPin 28

/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                        PID VARIABLES                                                             * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */  

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

//Specify the links and initial tuning parameters
double consKp=50, consKi=0.5, consKd=0;
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, REVERSE);  //Reverse direction for cooling, ON to lower temp.
int WindowSize = 5000;
unsigned long windowStartTime;


void pid(){
  
  sensors.requestTemperatures();
  Input = sensors.getTempC(tankThermometer);
  myPID.Compute();

   if(millis() - windowStartTime>WindowSize){ //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  
  if(Output < millis() - windowStartTime){
    digitalWrite(RelayPin,LOW); //inverted logic turn output pin ON
    lcd.setCursor(13,3);
    lcd.print("PID:ON ");
  }
  
  else {
    digitalWrite(RelayPin,HIGH);  //inverted logic turn output pin OFF
    lcd.setCursor(13,3);
    lcd.print("PID:OFF");
  }
  
  Serial.flush();
  clear_serial();

}


/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                           S E T U P - PID                                                        * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */
  
  windowStartTime = millis();
  
  //initialize the variables we're linked to
  Setpoint = 25;

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on, defaults to OFF
  myPID.SetMode(AUTOMATIC);
  

void loop()
{
  onesecond();
  raspi();
  pid();

... LOOP keeps going, code for controlling the light fixture keeps going...

This program is crap. It doesn't even compile.

It doesn't compile because its only showing the segment of the PID... At the end of the main loop I stated the loop continued the bracket is not even closed.

I'll post the fukk sketch tomorrow if its what's needed and you cant see any errors are in the setup, pid function

Here is the full sketch…

Arduino_Aqu_Control_ML_serial_digitemp.ino (31.2 KB)

What values are you getting for Output?

Last time I read the output it was oscillating between 0-255... Which is wrong since the outputlimits have been set to the windowsize... I don't know if I'm doing something wrong? Or have to set the limits again or something? I'll try reading the output again today

Since the PID is controlling a fan to cool a large water volumel, I set the windowsize to 30000

The setpoint is 25

Current PID output value: PID: 6323.52

Current temperature tank_: 25.5

When the PID approaches the setpoint it turns OFF the relay for something like 1s - 2s, the time it takes to call the pid() function again. Even when the PID get to the setpoint 25 it keeps this behaviour...