So i'm working on a project for an arduino PID controller to maintain a room's temperature as stable as possible, and i have trouble with the PID library...
The Compute function always brings me HIGH on the output for the relay i have it connected. At the moment i don't have a heater so i manually heat the sensor or lower the Target temperature (Ttemp) lower to the actual temperature (temp). The relay is connected and i can see its led but i also have a plot at the end to see the graphs. I tried different Kp,i,d insert but nothing changed..
I am thinking to do it manually but my mind just tries to find another way to re-invent the wheel..
//Libraries
#include <DHT.h>
#include <LiquidCrystal.h>
#include<PID_v1.h>
//Constants
#define buttonU 14
#define buttonD 15
#define relayPin 21
#define DHTPIN 8 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor
//Variables
int chk;
int line;
double temp; //Stores temperature value
double temp1=0;
double Ttemp; //target temp
double Ttemp1=0;
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
unsigned long pretime;
double input,output,setpoint;
double Kp=1,Ki=0,Kd=0;
PID myPID(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
Serial.begin(9600);
pinMode(relayPin,OUTPUT);
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(buttonU, INPUT_PULLUP);
pinMode(buttonD, INPUT_PULLUP);
dht.begin();
Ttemp = 25.0; //target value
windowStartTime = millis();
myPID.SetOutputLimits(0, WindowSize);
myPID.SetMode(AUTOMATIC);
}
void loop()
{
temp = dht.readTemperature(); //Read data and store it to variable temp
/* some crazy things i am trying... ignore
if(Ttemp > temp)
{
if(((temp-temp1) / (millis() - pretime)) < ((Ttemp - temp) / ((millis() - pretime))) )
{
digitalWrite(relayPin,HIGH);
line=25;
}
}
else
{
digitalWrite(relayPin,LOW);
line=20;
}
*/
/*------------------------PID---------------------------------*/
setpoint = Ttemp;
input = temp;
myPID.Compute();
if (millis() - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if (output < millis() - windowStartTime)
{
digitalWrite(relayPin, HIGH);
line=25;
}
else
{
digitalWrite(relayPin, LOW);
line=20;
}
/*-------------------------PID--------------------------------*/
if(digitalRead(buttonU) == LOW)
{
Ttemp = Ttemp + 0.1;
delay(200);
}
if(digitalRead(buttonD) == LOW)
{
Ttemp = Ttemp - 0.1;
delay(200);
}
if(temp1 != temp or Ttemp1 != Ttemp)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(temp);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Target:");
lcd.print(Ttemp);
lcd.print((char)223);
lcd.print("C");
temp1= temp;
pretime=millis();
Ttemp1= Ttemp;
}
Serial.println(Ttemp);
Serial.print(temp);
Serial.print(",");
Serial.println(line);
}
also for somereason ( i believe something with timings..) when i use the serial.begin my lcd shows very weird stuff.. but i don't really care about that at the moment.