starting my PID controller and running into a few issues. I can’t seem to fix the getcurrenttemperature not being declared issue.
#include <PID_v1.h>
#include <OneWire.h>
#define heatingPin 2
#define coolingPin 12
int RelayPin = 0;
const double RoomTemp = 22;
double heatingPID[] = {2,5,1}; //This needs to be tuned
double coolingPID[] = {3,6,4}; //This needs to be tuned
double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint,heatingPID[0],heatingPID[1],heatingPID[2],DIRECT);
double temps[]={50,28,31}; //Set Point temperatures are 50C 28C and 31C
unsigned long times[]={120000,60000,120000}; //Durations are 2min, 1min, 2min
unsigned long setPointStartTime;
unsigned long duration = 0;
int direction = 0;
int index = 0;
int WindowSize = 5000;
unsigned long windowStartTime;
void setup(){
pinMode( coolingPin, OUTPUT);
pinMode( heatingPin, OUTPUT );
digitalWrite( coolingPin, LOW ); // ensure initial state is inactive
digitalWrite( heatingPin, LOW );
windowStartTime = millis();
Setpoint = temps[index];
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
setDirection(temps[index], getCurrentTemperature());
}
void loop(){
Input = getCurrentTemperature();
if((Input - Setpoint)*direction>0 && duration == 0){ //You have reached the setpoint
duration = times[index];
setPointStartTime = millis();
//setDirection(SetPoint, RoomTemp);
}else if(duration > 0 && (millis()-setPointStartTime)>duration){
duration = 0;
if(index<2){
index++;
Setpoint = temps[index];
//setDirection(SetPoint, temps[index - 1]);
}
}
setDirection(Setpoint, Input);
myPID.Compute();
unsigned long now = millis();
if(now - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output > now - windowStartTime){
digitalWrite(RelayPin,HIGH);
}else{
digitalWrite(RelayPin,LOW);
}
}
void setDirection(double tempA, double tempB){
if(tempA>tempB){
if(direction < 1){
digitalWrite(coolingPin,LOW); //Turn of Cooling
direction = 1;
RelayPin = heatingPin;
myPID.SetControllerDirection(DIRECT);
myPID.SetTunings(heatingPID[0] , heatingPID[1] , heatingPID[2]);
}
}else{
if(direction > -1){
digitalWrite(heatingPin,LOW); //Turn of Heating
direction = -1;
RelayPin = coolingPin;
myPID.SetControllerDirection(REVERSE);
myPID.SetTunings(coolingPID[0] , coolingPID[1] , coolingPID[2]);
}
}
}