Hi,
i'm making a temperature controller (PID with slow response)
it worked normal until i wanted to try to round the output of the PID.
because I only want to controm a PWM pin with integers and not with decimals.
but since i tried to cast, round or make an integer, the heater stays on.
When i look on my LCD, I get strange values like 330,... and my serialPlotter says always 0?
can anyone help me?
(here's my code)
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <math.h>
#define DHTPIN 8
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
#include <PCD8544.h>
PCD8544 lcd;
//----------------------PID
#include <PID_v1.h>
#define PIN_OUTPUT 11
double Setpoint, Input, OutputRaw;
double Output;
double Kp=5, Ki=0.4, Kd=0.08;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup() {
Serial.begin(9600);
dht.begin();
sensor_t sensor;
dht.temperature().getSensor(&sensor);
lcd.begin(84, 48);
Setpoint = 37;
myPID.SetMode(AUTOMATIC);
}
float temp;
float previousTemp[8];
int temp2;
int humidy;
unsigned long previousMillis = 0;
const long interval = 3000;
void loop() {
getTemp();
Input=temp;
myPID.Compute();
analogWrite(PIN_OUTPUT, round(Output));
delay(250);
lcd.setCursor(0, 0);
lcd.print("temp1: ");
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.print("temp DHT: ");
lcd.print(temp2);
lcd.setCursor(0, 3);
lcd.print("humidity: ");
lcd.print(humidy);
lcd.setCursor(0, 4);
lcd.print("PID:: ");
lcd.print(round(Output));
Serial.println(round(Output));
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.print(temp);
Serial.print(" ");
Serial.print("37");
Serial.print(" ");
Serial.println(round(Output)/255*37);
//Serial.print(" ");
//Serial.println(Output/255*20);
}
}
int ThermistorPin = A0;
int Vo;
float R1 = 1000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
void getTemp(){
///-----------------------------DHT---------------
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {}else {temp2=event.temperature;}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {}else {humidy=event.relative_humidity;}
///----------------------------PTC----------------
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
Tc = T - 273.15;
previousTemp[7]=Tc;
float totaalTempArray=0;
for(int i=0;i<7;i++){
previousTemp*=previousTemp[i+1];*
_ totaalTempArray=totaalTempArray+previousTemp*;_
_ }_
_ totaalTempArray=totaalTempArray+previousTemp[7];_
_ temp=totaalTempArray/8;_
_ }*_