77 *
88 * to connect them together we use "time proportioning
99 * control" it's essentially a really slow version of PWM.
10- * first we decide on a window size (5000mS say.) we then
10+ * first we decide on a window size (5000mS say.) we then
1111 * set the pid to adjust its output between 0 and that window
1212 * size. lastly, we add some logic that translates the PID
13- * output into "Relay On Time" with the remainder of the
13+ * output into "Relay On Time" with the remainder of the
1414 * window being "Relay Off Time"
1515 ********************************************************/
1616
1717#include < PID_v1.h>
18- #define RelayPin 6
18+
19+ #define PIN_INPUT 0
20+ #define RELAY_PIN 6
1921
2022// Define Variables we'll be connecting to
2123double Setpoint, Input, Output;
2224
2325// Specify the links and initial tuning parameters
24- PID myPID (&Input, &Output, &Setpoint,2 ,5 ,1 , DIRECT);
26+ double Kp=2 , Ki=5 , Kd=1 ;
27+ PID myPID (&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
2528
2629int WindowSize = 5000 ;
2730unsigned long windowStartTime;
31+
2832void setup ()
2933{
3034 windowStartTime = millis ();
31-
35+
3236 // initialize the variables we're linked to
3337 Setpoint = 100 ;
3438
@@ -41,18 +45,18 @@ void setup()
4145
4246void loop ()
4347{
44- Input = analogRead (0 );
48+ Input = analogRead (PIN_INPUT );
4549 myPID.Compute ();
4650
4751 /* ***********************************************
4852 * turn the output pin on/off based on pid output
4953 ************************************************/
50- if (millis () - windowStartTime> WindowSize)
54+ if (millis () - windowStartTime > WindowSize)
5155 { // time to shift the Relay Window
5256 windowStartTime += WindowSize;
5357 }
54- if (Output < millis () - windowStartTime) digitalWrite (RelayPin, HIGH);
55- else digitalWrite (RelayPin, LOW);
58+ if (Output < millis () - windowStartTime) digitalWrite (RELAY_PIN, HIGH);
59+ else digitalWrite (RELAY_PIN, LOW);
5660
5761}
5862
0 commit comments