### PID_v2 Start Method Example (C++) Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Shows how to initialize the PID controller using the `Start` method. This method should be called once in the `setup()` function to begin automatic control, taking the current input, output, and desired setpoint as arguments. ```cpp #include #define PIN_INPUT 0 #define PIN_OUTPUT 3 double Kp = 2, Ki = 5, Kd = 1; PID_v2 myPID(Kp, Ki, Kd, PID::Direct); void setup() { myPID.Start(analogRead(PIN_INPUT), // current input value 0, // current output value 100); // desired setpoint } ``` -------------------------------- ### Basic PID Control Setup and Execution in Arduino C++ Source: https://github.com/imax9000/arduino-pid-library/blob/master/README.md This C++ code snippet demonstrates the basic setup and execution of the PID_v2 library in an Arduino environment. It includes including the library, defining input/output pins, initializing PID parameters, and running the PID control loop within the Arduino setup and loop functions. It requires the PID_v2 library to be installed. ```c++ #include #define PIN_INPUT 0 #define PIN_OUTPUT 3 // Specify the links and initial tuning parameters double Kp = 2, Ki = 5, Kd = 1; PID_v2 myPID(Kp, Ki, Kd, PID::Direct); void setup() { myPID.Start(analogRead(PIN_INPUT), // input 0, // current output 100); // setpoint } void loop() { const double input = analogRead(PIN_INPUT); const double output = myPID.Run(input); analogWrite(PIN_OUTPUT, output); } ``` -------------------------------- ### PID_v2 Constructor Examples (C++) Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Demonstrates how to instantiate the PID_v2 controller with different configurations, including basic tuning parameters, controller direction, and proportional-on-measurement mode for reduced overshoot. ```cpp #include // Basic constructor with Direct action double Kp = 2, Ki = 5, Kd = 1; PID_v2 myPID(Kp, Ki, Kd, PID::Direct); // Constructor with Proportional on Measurement (reduces overshoot) PID_v2 smoothPID(2, 5, 1, PID::Direct, PID::P_On::Measurement); // Reverse acting controller (output increases when input increases) PID_v2 reversePID(2, 5, 1, PID::Reverse); ``` -------------------------------- ### Setpoint Management with PID_v2 (C++) Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Demonstrates how to dynamically set and retrieve the target setpoint for the PID controller using `Setpoint()` and `GetSetpoint()`. This example shows changing the setpoint based on potentiometer input and logging the current setpoint. ```cpp #include PID_v2 myPID(2, 5, 1, PID::Direct); void setup() { myPID.Start(analogRead(0), 0, 100); // Initial setpoint: 100 } void loop() { // Change setpoint based on potentiometer double newSetpoint = map(analogRead(1), 0, 1023, 0, 255); myPID.Setpoint(newSetpoint); // Read current setpoint for display double currentSetpoint = myPID.GetSetpoint(); Serial.println(currentSetpoint); const double output = myPID.Run(analogRead(0)); analogWrite(3, output); } ``` -------------------------------- ### Runtime Tuning with PID_v2 SetTunings (C++) Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Shows how to adjust the PID tuning parameters (Kp, Ki, Kd) during runtime using the `SetTunings` method. This example implements adaptive tuning by switching between aggressive and conservative parameters based on the error gap to the setpoint. ```cpp #include #define PIN_INPUT 0 #define PIN_OUTPUT 3 // Aggressive tuning for large errors double aggKp = 4, aggKi = 0.2, aggKd = 1; // Conservative tuning near setpoint double consKp = 1, consKi = 0.05, consKd = 0.25; PID_v2 myPID(consKp, consKi, consKd, PID::Direct); void setup() { myPID.Start(analogRead(PIN_INPUT), 0, 100); } void loop() { const double input = analogRead(PIN_INPUT); double gap = abs(myPID.GetSetpoint() - input); if (gap < 10) { // Close to setpoint: use conservative tuning myPID.SetTunings(consKp, consKi, consKd); } else { // Far from setpoint: use aggressive tuning myPID.SetTunings(aggKp, aggKi, aggKd); } const double output = myPID.Run(input); analogWrite(PIN_OUTPUT, output); } ``` -------------------------------- ### PID_v2 Run Method Example (C++) Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Illustrates the usage of the `Run` method within the main `loop()` function. This method executes the PID algorithm based on the current input and returns the computed output value. It respects the configured sample time. ```cpp #include #define PIN_INPUT 0 #define PIN_OUTPUT 3 double Kp = 2, Ki = 5, Kd = 1; PID_v2 myPID(Kp, Ki, Kd, PID::Direct); void setup() { myPID.Start(analogRead(PIN_INPUT), 0, 100); } void loop() { const double input = analogRead(PIN_INPUT); const double output = myPID.Run(input); analogWrite(PIN_OUTPUT, output); } ``` -------------------------------- ### Get PID Mode and Direction (C++) Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Query the current operating mode (Automatic/Manual) and controller direction (Direct/Reverse) of the PID controller. This is useful for status displays and implementing conditional logic within your Arduino sketch. It requires the PID_v2 library. ```cpp #include PID_v2 myPID(2, 5, 1, PID::Direct); void setup() { Serial.begin(9600); myPID.Start(analogRead(0), 0, 100); } void loop() { // Display controller status Serial.print("Mode: "); Serial.print(myPID.GetMode() == PID::Automatic ? "AUTO" : "MANUAL"); Serial.print(" Direction: "); Serial.println(myPID.GetDirection() == PID::Direct ? "DIRECT" : "REVERSE"); const double output = myPID.Run(analogRead(0)); analogWrite(3, output); delay(1000); } ``` -------------------------------- ### Retrieve PID Tuning Parameters Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Retrieves the current proportional (Kp), integral (Ki), and derivative (Kd) tuning parameters. This is useful for displaying settings on an LCD or serial monitor, or for implementing tuning interfaces. ```cpp #include PID_v2 myPID(2, 5, 1, PID::Direct); void setup() { Serial.begin(9600); myPID.Start(analogRead(0), 0, 100); } void loop() { // Display current tuning parameters Serial.print("Kp: "); Serial.print(myPID.GetKp()); Serial.print(" Ki: "); Serial.print(myPID.GetKi()); Serial.print(" Kd: "); Serial.println(myPID.GetKd()); const double output = myPID.Run(analogRead(0)); analogWrite(3, output); delay(1000); } ``` -------------------------------- ### Legacy PID Class Pointer-Based Control (C++) Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Utilizes the legacy base `PID` class which employs pointers for input, output, and setpoint variables. This ensures backward compatibility with older Arduino PID library code. The `Compute()` method should be used instead of `Run()` for this class. ```cpp #include #define PIN_INPUT 0 #define PIN_OUTPUT 3 double Input, Output, Setpoint; double Kp = 2, Ki = 5, Kd = 1; // Pointer-based constructor (legacy style) PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, PID::Direct); void setup() { Input = analogRead(PIN_INPUT); Setpoint = 100; // Enable the PID controller myPID.SetMode(PID::Automatic); } void loop() { Input = analogRead(PIN_INPUT); myPID.Compute(); // Returns true if computation occurred analogWrite(PIN_OUTPUT, Output); } ``` -------------------------------- ### Retrieve Individual PID Component Contributions Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Retrieves the individual Proportional (P), Integral (I), and Derivative (D) contributions from the last PID computation. This is essential for tuning diagnostics and understanding controller behavior. GetLastI returns the accumulated integral term (outputSum). ```cpp #include PID_v2 myPID(2, 5, 1, PID::Direct); void setup() { Serial.begin(9600); myPID.Start(analogRead(0), 0, 100); } void loop() { const double input = analogRead(0); const double output = myPID.Run(input); analogWrite(3, output); // Debug PID contributions for tuning analysis Serial.print("Input: "); Serial.print(input); Serial.print(" Output: "); Serial.print(output); Serial.print(" P: "); Serial.print(myPID.GetLastP()); Serial.print(" I: "); Serial.print(myPID.GetLastI()); Serial.print(" D: "); Serial.println(myPID.GetLastD()); delay(100); } ``` -------------------------------- ### Set PID Computation Interval Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Sets the PID computation interval in milliseconds, with a default of 100ms. Shorter intervals offer faster response but increase processor load. The library automatically adjusts Ki and Kd values when the sample time changes. ```cpp #include PID_v2 myPID(2, 5, 1, PID::Direct); void setup() { // Set faster 50ms sample rate for responsive control myPID.SetSampleTime(50); // Or slower 200ms for smoother, less aggressive control // myPID.SetSampleTime(200); myPID.Start(analogRead(0), 0, 100); } void loop() { const double output = myPID.Run(analogRead(0)); analogWrite(3, output); } ``` -------------------------------- ### Switch PID Controller Mode Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Switches the PID controller between Manual and Automatic modes. Manual mode bypasses PID computation for direct output control. Automatic mode initializes the controller for bumpless transfer upon activation. ```cpp #include PID_v2 myPID(2, 5, 1, PID::Direct); bool autoMode = true; void setup() { myPID.Start(analogRead(0), 0, 100); } void loop() { // Toggle mode with button on pin 2 if (digitalRead(2) == LOW) { autoMode = !autoMode; myPID.SetMode(autoMode ? PID::Automatic : PID::Manual); delay(200); // Debounce } if (myPID.GetMode() == PID::Automatic) { const double output = myPID.Run(analogRead(0)); analogWrite(3, output); } else { // Manual control via potentiometer analogWrite(3, analogRead(1) / 4); } } ``` -------------------------------- ### Set PID Controller Action Direction Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Sets whether the PID controller uses Direct or Reverse action. Direct action increases output with a positive error (e.g., heating), while Reverse action decreases output (e.g., cooling). This is typically set once during initialization. ```cpp #include // Direct acting: for heating (more output = more heat = higher temp) PID_v2 heaterPID(2, 5, 1, PID::Direct); // Reverse acting: for cooling (more output = more cooling = lower temp) PID_v2 coolerPID(2, 5, 1, PID::Reverse); void setup() { heaterPID.Start(analogRead(0), 0, 100); // Can also change direction at runtime if needed // heaterPID.SetControllerDirection(PID::Reverse); } ``` -------------------------------- ### Constrain PID Output Limits Source: https://context7.com/imax9000/arduino-pid-library/llms.txt Constrains the PID output to a specified range, defaulting to 0-255 for Arduino PWM. Adjust this range based on actuator requirements like servo ranges, relay timing, or motor driver inputs. ```cpp #include #define PIN_INPUT 0 #define RELAY_PIN 6 double Kp = 2, Ki = 5, Kd = 1; PID_v2 myPID(Kp, Ki, Kd, PID::Direct); const int WindowSize = 5000; // 5 second time window unsigned long windowStartTime; void setup() { windowStartTime = millis(); // Set output range for time-proportional relay control myPID.SetOutputLimits(0, WindowSize); myPID.Start(analogRead(PIN_INPUT), 0, 100); } void loop() { const double input = analogRead(PIN_INPUT); const double output = myPID.Run(input); // Time proportioning: convert analog output to relay on/off while (millis() - windowStartTime > WindowSize) { windowStartTime += WindowSize; } if (output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH); else digitalWrite(RELAY_PIN, LOW); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.