/********************************************************
* PID Basic Example
* Reading analog input 0 to control analog PWM output 3
********************************************************/
#include <PID_v2.h>
#define PIN_INPUT A0
int Keluaran;
bool status = true;
int Sleep_Lvl = 750;
int Wakeup_Lvl = 650;
int minSpeed=1800;
int maxSpeed=3000;
int maxPressure=1000;
int pressure;
int input;
int output;
int setPoint=700;
// Specify the links and initial tuning parameters
int Kp = 2, Ki = 5, Kd = 1;
PID_v2 myPID(Kp, Ki, Kd, PID::Direct);
void setup() {
Serial.begin(115200);
pressure = analogRead(PIN_INPUT);
input = map (pressure,0,1023,0,maxPressure);
myPID.Start(input, 0, setPoint);
myPID.SetSampleTime(200); // Initialise sample rate in ms (200ms default)
myPID.SetOutputLimits(minSpeed, maxSpeed);
myPID.SetMode(AUTOMATIC);
}
void loop() {
pressure = analogRead(PIN_INPUT);
input = map (pressure,0,1023,0,maxPressure);
output = myPID.Run(input);
if (input > Sleep_Lvl){
status =false;
}
if (input < Wakeup_Lvl && Keluaran == 0){
status = true;
}
if (status == true)
Keluaran = output;
else if (status == false)
Keluaran = 0;
Serial.print(String(input) + " ");
Serial.println(Keluaran);
delay(200);
}