Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PID_v1.h>
- #define PIN_INPUT A0
- //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
- Serial.begin(9600); //Start a serial session
- Input = analogRead(PIN_INPUT);
- Setpoint = 336;
- //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();
- Serial.print("Setpoint = ");
- Serial.print(Setpoint);
- Serial.print(" Input = ");
- Serial.print(Input);
- Serial.print(" Output = ");
- Serial.print(Output);
- Serial.print("n");
- // analogWrite(PIN_OUTPUT, Output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement