// custom-chip-RCfilter.ino
// https://wokwi.com/projects/411487751748467713
//
// Code from https://github.com/br3ttb/Arduino-PID-Library/blob/master/examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino
//
// Built from 
// https://wokwi.com/projects/409325290405496833
// and https://github.com/drf5n/Wokwi-Chip-RCfilter 
//  
// An implementation of an RC filter in a Wokwi custom chip
//
// See also: 
// https://wokwi.com/projects/412997833205485569 -- PID_basic.ino with slider
// https://wokwi.com/projects/412042610362971137 -- PID_Basic.ino
// https://wokwi.com/projects/410423413759806465 -- PID_Relay.ino with slider
// https://wokwi.com/projects/411487751748467713 -- PID_AdaptiveTunings.ino
//
/********************************************************
 * PID Adaptive Tuning Example
 * One of the benefits of the PID library is that you can
 * change the tuning parameters at any time.  this can be
 * helpful if we want the controller to be agressive at some
 * times, and conservative at others.   in the example below
 * we set the controller to use Conservative Tuning Parameters
 * when we're near setpoint and more agressive Tuning
 * Parameters when we're farther away.
 ********************************************************/

#include <PID_v1.h>

#define PIN_INPUT 0
#define PIN_OUTPUT 3

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Define the aggressive and conservative Tuning Parameters
double aggKp=4, aggKi=0.2, aggKd=1;
double consKp=1, consKi=0.05, consKd=0.25;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);

void setup()
{
  //initialize the variables we're linked to
  Input = analogRead(PIN_INPUT);
  Setpoint = 100;

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(PIN_INPUT);

  double gap = abs(Setpoint-Input); //distance away from setpoint
  if (gap < 10)
  {  //we're close to setpoint, use conservative tuning parameters
    myPID.SetTunings(consKp, consKi, consKd);
  }
  else
  {
     //we're far from setpoint, use aggressive tuning parameters
     myPID.SetTunings(aggKp, aggKi, aggKd);
  }

  myPID.Compute();
  analogWrite(PIN_OUTPUT, Output);
}

Loading chip...chip-analog-rcfilter
Loading chip...chip-opamp
Loading chip...chip-scope