const byte LED_PIN = 13;
const byte TEMP_PIN = A0;
const byte POT1_PIN = A1;
const byte POT2_PIN = A2;
#include "AdvancedPID.h"
// Kb = 0.0 means we are using "Clamping" Anti-Windup (Standard)
AdvancedPID myPID(20.4, 0.0, 0.0, 0.0); // Kp, Ki, Kd, Kb
// SP = Setpoint (Sollwert)
// CO = Controller Output
// PV = Process Variable (Istwert or. Input)
// PID Variablen
float Setpoint = 0.0;
float Input = 0.0;
float Output = 0.0;
void setup() {
Serial.begin(9600);
while (!Serial) {};
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Optional: Set a small deadband. If error is < 0.5, output won't change jitterily.
//myPID.setDeadband(0.5);
}
void loop() {
digitalWrite(LED_PIN, ANALOG(TEMP_PIN, POT1_PIN));
byte targetPowerPercent = PID(POT2_PIN);
}
bool ANALOG(byte TEMP_PIN, byte POTI_PIN) {
static bool init = false;
if (init == false) {
init = true;
pinMode(TEMP_PIN, INPUT_PULLUP);
}
const float BETA = 3950;
uint32_t currentTime = millis();
static uint32_t previousTime = currentTime;
if (currentTime - previousTime > 500) {
previousTime = currentTime;
uint16_t TEMP = analogRead(TEMP_PIN);
uint16_t POTI = analogRead(POTI_PIN);
Input = 0.0;
Setpoint = 0.0;
if (TEMP > 0 && TEMP < 1023) {
Input = 1.0 / (log(1.0 / (1023.0 / TEMP - 1.0)) / BETA + 1.0 / 298.15) - 273.15;
Setpoint = -24.0 + (POTI / 1023.0) * (80.0 + 24.0); // -24°C to 80°C
}
}
return Input < Setpoint;
}
byte PID(byte POTI_PIN) {
static bool init = false;
if (init == false) {
init = true;
}
uint32_t currentTime = millis();
static uint32_t previousTime = currentTime;
if (currentTime - previousTime > 500) {
previousTime = currentTime;
uint16_t POTI = analogRead(POTI_PIN);
myPID.setOutputLimits(0.0, POTI / 1023.0 * 255.0);
Output = myPID.run(Input, Setpoint);
if (POTI == 0) Output = 0.0;
else Output = Output * 100.0 / 255.0;
if (1) {
Serial.print(F("PV="));
Serial.print(Input);
Serial.print(F("°C"));
Serial.print(F("\t"));
Serial.print(F("SP="));
Serial.print(Setpoint);
Serial.print(F("°C"));
Serial.print(F("\t"));
Serial.print(F("CO="));
Serial.print(Output);
Serial.print(F(" %"));
Serial.println();
}
}
return (byte)Output;
}< --24°C
< +80°C
< +20°C
< +50°C
< +00°C
< +30°C
< +40°C
< +10°C
< +60°C
< +70°C
< --20°C
< --20°C
POWER
SETPOINT