#include <ArduPID.h>
float kesalahan;
float integralKesalahan;
float derivatifKesalahan;
float kesalahanLalu = 0;
float Kp, Ki, Kd;
float input;
 
//Pin
byte sensorPin = 34;
byte Pemanas = 2;
 
//konstanta
#define Ksensor 0.2 //faktor koreksi sensor panas

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");
  pinMode(Pemanas, OUTPUT);

  // PID Parameter
  input = 100;
  Kp = 1;
  Ki = 0.5;
  Kd = -0.2;
}

void loop() {
  // put your main code here, to run repeatedly:
 float sensor = 1.0 * analogRead(sensorPin) * Ksensor;
   
  float respon = hitungPID(input, sensor);
  analogWrite(Pemanas, respon);
  Serial.print(input);
  Serial.print(",");
  Serial.println(sensor);
}

float hitungPID(float input, float sensor)
{
  kesalahan = input - sensor;
  integralKesalahan += kesalahan;
  derivatifKesalahan = kesalahan - kesalahanLalu;
  kesalahanLalu = kesalahan;
   
  return (Kp * kesalahan) + (Ki * integralKesalahan) + (Kd * derivatifKesalahan);
}