Can anyone see why this won't produce a fan output?
I'm not sure if the error part is calculating correctly,
And also currently the temp shows 70 odd degrees which isnt right
#include <PID_v1.h>
double sensed_output, control_signal;
double setpoint;
double Kp= 2.1; //proportional gain
double Ki= 3.30; //integral gain
double Kd= 0.3255; //derivative gain
int T= 50; //sample time of 50 milliseconds (ms)
unsigned long last_time;
double total_error, last_error;
int sensorPin = A0; // assigns the analog input pin from the temp sensor
int sensorVoltage; // the variable storing the analog signal
double temperature; // sensed temperature
int OutputPin = 6; // PWM output pin to the motor
void setup(){
pinMode(OutputPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
sensorVoltage = analogRead(sensorPin);
temperature = sensorVoltage*0.4882; // changing voltage values from analog to Digital, (5/1024)
// temperature = analogRead(tempPin)*5/1024.0;
//temperature = temperature - 0.5;
//temperature = temperature / 0.01;
sensed_output= temperature;
setpoint = 25; // setpoint to 25 degC
PID_Control(); //calls the PID function every T interval and outputs a control signal
analogWrite(OutputPin, control_signal) // sends PWM signal
;}
void PID_Control(){
unsigned long current_time = millis(); //returns the number of milliseconds passed since the Arduino started running the program
int delta_time = current_time - last_time; //delta time interval
if (delta_time >= T){
double error = setpoint - sensed_output;
total_error += error; //accumalates the error - integral term
if (total_error >= 255) total_error = 255;
else if (total_error <= 0) total_error = 0;
double delta_error = error - last_error; //difference of error for derivative term
control_signal = Kp*error + (Ki*T)*total_error + (Kd/T)*delta_error; //PID control compute
if (control_signal >= 255) control_signal = 255;
else if (control_signal <= 0) control_signal = 0;
last_error = error;
last_time = current_time;
}
{
Serial.print ("Temperature: ");
Serial.println(temperature);
Serial.print ("Fan Speed:");
Serial.println(control_signal);
delay (1000);
}
}