From 4f0890588defd77dfc4dd3c4b863db1ea5db2173 Mon Sep 17 00:00:00 2001 From: "kaia.ai" <33589365+kaiaai@users.noreply.github.com> Date: Sun, 29 Oct 2023 02:03:22 -0700 Subject: [PATCH] Create PID_Basic.ino --- examples/PID_Basic.ino | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/PID_Basic.ino diff --git a/examples/PID_Basic.ino b/examples/PID_Basic.ino new file mode 100644 index 0000000..a69bf10 --- /dev/null +++ b/examples/PID_Basic.ino @@ -0,0 +1,38 @@ +/******************************************************** + * PID Basic Example + * Reading analog input 0 to control analog PWM output 3 + ********************************************************/ + +#include + +#define PIN_INPUT 0 +#define PIN_OUTPUT 3 + +//Define Variables we'll be connecting to +double Setpoint, Input, Output; + +//Specify the links and initial tuning parameters +double Kp=2, Ki=5, Kd=1; +PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, 0.01, DIRECT); // 10 msec default sampling rate + +void setup() +{ + //initialize the variables we're linked to + Input = analogRead(PIN_INPUT); + Setpoint = 100; + + // Set new Kp, Ki, Kd at any time if necessary + // myPID.SetTunings(Kp, Ki, Kd); + + // Set PID output limits if necessary + // myPID.SetOutputLimits(-1, 1); + + myPID.enable(true); +} + +void loop() +{ + Input = analogRead(PIN_INPUT); + myPID.Compute(0.01); // Specify the actual time elapsed since last myPID.Compute() + analogWrite(PIN_OUTPUT, Output); +}