### Automatic PID Temperature Control Example (C++) Source: https://github.com/gyverlibs/gyverpid/blob/main/README.md This snippet shows a basic implementation of a PID controller using the GyverPID library. It initializes the PID object, sets control parameters (Kp, Ki, Kd), limits, and the target setpoint. The main loop reads a simulated temperature input and uses `getResultTimer()` to calculate and apply the control output (simulated PWM) based on an internal timer. ```cpp /* Пример работы ПИД регулятора в автоматическом режиме по встроенному таймеру Давайте представим, что на 3 пине у нас спираль нагрева, подключенная через мосфет, управляем ШИМ сигналом И есть какой то абстрактный датчик температуры, на который влияет спираль */ // перед подключением библиотеки можно добавить настройки: // сделает часть вычислений целочисленными, что чуть (совсем чуть!) ускорит код // #define PID_INTEGER // режим, при котором интегральная составляющая суммируется только в пределах указанного количества значений // #define PID_INTEGRAL_WINDOW 50 #include "GyverPID.h" GyverPID regulator(0.1, 0.05, 0.01, 10); // коэф. П, коэф. И, коэф. Д, период дискретизации dt (мс) // или так: // GyverPID regulator(0.1, 0.05, 0.01); // можно П, И, Д, без dt, dt будет по умолч. 100 мс void setup() { regulator.setDirection(NORMAL); // направление регулирования (NORMAL/REVERSE). ПО УМОЛЧАНИЮ СТОИТ NORMAL regulator.setLimits(0, 255); // пределы (ставим для 8 битного ШИМ). ПО УМОЛЧАНИЮ СТОЯТ 0 И 255 regulator.setpoint = 50; // сообщаем регулятору температуру, которую он должен поддерживать // в процессе работы можно менять коэффициенты regulator.Kp = 5.2; regulator.Ki += 0.5; regulator.Kd = 0; } void loop() { int temp; // читаем с датчика температуру regulator.input = temp; // сообщаем регулятору текущую температуру // getResultTimer возвращает значение для управляющего устройства // (после вызова можно получать это значение как regulator.output) // обновление происходит по встроенному таймеру на millis() analogWrite(3, regulator.getResultTimer()); // отправляем на мосфет // .getResultTimer() по сути возвращает regulator.output } ``` -------------------------------- ### Implementing PID Temperature Control with GyverPID in C++ Source: https://github.com/gyverlibs/gyverpid/blob/main/README_EN.md This C++ example demonstrates how to use the GyverPID library for automatic temperature control. It initializes the PID controller, sets parameters like setpoint, direction, and output limits, and updates the controller periodically using the built-in timer (`getResultTimer()`) to generate a PWM signal for a heating element. It requires the GyverPID library. ```C++ /* An example of the work of the controller PID in automatic mode according to the built -in timer Let's imagine that on 3 pin we have a heating spiral connected via Mosfet, We control the PWM signal And there is some kind of abstract temperature sensor on which the spiral affects */ // Before connecting the library, you can add settings: // will make part of the calculations integer, which a little (just a little!) will accelerate the code // #define pid_integer // mode in which the integral component is summarized only within the specified number of values // #define pid_integral_window 50 #include "gyverpid.h" Gyverpid Regulator (0.1, 0.05, 0.01, 10);// coeff.P, coeff.And, coeff.D, DT sampling period (MS) // or so: // gyverpid regulator (0.1, 0.05, 0.01);// You can, and, d, without dt, dt will be a silence.100 ms VOID setup () { regulator.setdirection (Normal);// Direction of regulation (Normal/Reverse).The default is Normal regulator.setlimits (0, 255);// limits (set for 8 bit PWM).The default cost 0 and 255 regulator.setpoint = 50;// inform the regulator of the temperature that he must support // In the process of work, you can change the coefficients regulator.kp = 5.2; regulator.ki += 0.5; regulator.kd = 0; } VOID loop () { Int TEMP;// read the temperature from the sensor regulator.input = TEMP;// inform the regulator the current temperature // Getressulttimer returns the value for the control device // (after a call, you can get this value as regulator.Output) // update takes place according to the built -in timer on millis () analogwrite (3, regulator.getresulttimer ());// Send to Mosfet // .Getressulttimer () essentially returns regulator.Output } ``` -------------------------------- ### Initializing Gyverpid Object (C++) Source: https://github.com/gyverlibs/gyverpid/blob/main/README_EN.md Demonstrates the three different ways to initialize a Gyverpid object: with default settings (zero coefficients, 100ms DT), with specified KP, KI, KD coefficients, or with specified coefficients and a custom DT (in milliseconds). ```C++ // You can initialize the object in three ways: Gyverpid Regulator;// initialize without settings (all by zero, DT 100 ms) Gyverpid Regulator (KP, Ki, KD);// initialize with coefficients.DT will be standard 100 ms Gyverpid Regulator (KP, Ki, KD, DT);// initialize with coefficients and DT (in milliseconds) ``` -------------------------------- ### Using Gyverpid Functions and Variables (C++) Source: https://github.com/gyverlibs/gyverpid/blob/main/README_EN.md Illustrates common variables used with the Gyverpid controller (setpoint, input, output) and lists key methods for obtaining the control output (manually or via timer), setting the control direction, operating mode, output limits, and the sampling time (DT). ```C++ Datatype setpoint = 0;// a given value that the regulator should support Datatype Input = 0;// signal from the sensor (for example, the temperature that we adjust) DATATYPE OUTPUT = 0;// Exit from the regulator to the control device (for example, the size of the PWM or the angle of rotation servo) Datatype Getressult ();// returns a new value when calling (if we use our timer with a period DT!) Datatype Getressulttimer ();// returns a new value no earlier than through DT milliseconds (built -in timer with a DT period) VOID Setdirection (Boolean Direction);// Direction of Regulation: Normal (0) or Reverse (1) vCranberries OID setmode (Boolean Mode);// mode: Work on the input error on_error (0) or on change on_rate (1) VOID setlimits (int min_outPut, int max_outPut);// Limit output (for example, for Shim, put 0-255) VOID setdt (int16_t new_dt);// Installation of sampling time (for Getressulttimer) Float kp = 0.0; Float Ki = 0.0; Float KD = 0.0; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.