//---- Arduino PID Library - Version 1.2.1 by Brett Beauregard <[email protected]> brettbeauregard.com ----
#include "PID_v1.h"
//------------------ VARIABEL PID ------------------------------
double setpoint, input, output;
//------------------ VARIABEL KONSTANTA PID --------------------
double kp = 20, ki = 5, kd = 2;
//----------------- FUNGSI PID ------------------------------
PID pids(&input, &output, &setpoint, kp, ki, kd, P_ON_E, DIRECT);
//---------------- INISIALISASI PIN ----------------------------
const int PWM_PIN = 3;
const int INPUT_PIN = A1;
const int SETPOINT_PIN = A0;
const int LED_GREEN = 6;
const int LED_RED = 7;
void setup() {
Serial.begin(115200);
Serial.println(__FILE__); //---WOKWI ONLY---
pids.SetOutputLimits(0, 255);
pids.SetSampleTime(5000);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
//------------ MENGATUR/MENYALAKAN MODE PID -----------
pids.SetMode(AUTOMATIC);
}
void loop() {
if(INPUT_PIN > 0){
input = map(analogRead(INPUT_PIN), 115.0, 511.2, 80, 25);
}
//komputasi PID
if(pids.Compute()){
analogWrite(PWM_PIN, (int)output); //------ MENAMPUNG NILAI OUTPUT PID KE VARIABLE PWM_PIN----
setpoint = map(analogRead(SETPOINT_PIN), 0, 1023, 16, 80);
}
//---------- MEMANGGIL FUNGSI REPORT -----------------------------------
report();
}
//---------- FUNGSI UNTUK MENCETAK NILAI PADA SERIAL MONITOR--------------
void report(void){
static uint32_t last = 0;
const int interval = 1000; //------ INTERVAL PENCETAKAN PADA SERIAL MONITOR -------
if(millis() - last > interval){
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();
}
}