#include "PID_v1.h"
#define PWM_PIN 3
#define INPUT_PIN A1
#define SETPOINT_PIN A0
#define LED_GREEN 6
#define LED_RED 7
// definisi variable kebutuhan PID
double Kp = 2, Ki= 0, Kd= 0;
double setpoint, input, output;
PID pidcs(&input, &output, &setpoint, Kp, Ki, Kd, P_ON_E, DIRECT);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println(__FILE__); // kebutuhan di wokwi
// Set PID Limit (PWM Range)
pidcs.SetOutputLimits(-4, 255);
// Nyalakann PID
pidcs.SetMode(AUTOMATIC);
// Set pin untuk LED sebagai OUTPUT
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
// Keterbalikan pembacaan, karena
// akibat menggunakan NTC
input = map(analogRead(INPUT_PIN), 115.0, 511.2, 80, 25);
// Define setpoint awal
setpoint = map(analogRead(SETPOINT_PIN), 0, 1023, 16, 80);
}
// function yg akan mencetak nilai ke Serial monitor
// Dia akan melakukan pencetakan selama 1 detik sekali
void report(void) {
static uint32_t last = 0;
const int interval = 400;
if (millis() - last > interval) {
// do action
last += interval;
if (output > 0) {
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_RED, LOW);
} else {
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH);
}
Serial.print("SP:");
Serial.print(setpoint);
Serial.print(" PV:");
Serial.print(input);
Serial.print(" CV:");
Serial.print(output);
Serial.print(' ');
Serial.println();
}
}
void loop() {
// Keterbalikan pembacaan, karena
// akibat menggunakan NTC
input = map(analogRead(INPUT_PIN), 115.0, 511.2, 80, 25);
if (pidcs.Compute()) {
analogWrite(PWM_PIN, (int) output);
setpoint = map(analogRead(SETPOINT_PIN), 0, 1023, 16, 80);
}
// Cetak report
report();
}
80°
Setpoint
16°