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...