Skip to content

enhance coding style Examples #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

#include <PID_v1.h>

#define PIN_INPUT 0
#define PIN_OUTPUT 3

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

Expand All @@ -24,7 +27,7 @@ PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
void setup()
{
//initialize the variables we're linked to
Input = analogRead(0);
Input = analogRead(PIN_INPUT);
Setpoint = 100;

//turn the PID on
Expand All @@ -33,10 +36,10 @@ void setup()

void loop()
{
Input = analogRead(0);
Input = analogRead(PIN_INPUT);

double gap = abs(Setpoint-Input); //distance away from setpoint
if(gap<10)
if (gap < 10)
{ //we're close to setpoint, use conservative tuning parameters
myPID.SetTunings(consKp, consKi, consKd);
}
Expand All @@ -45,9 +48,9 @@ void loop()
//we're far from setpoint, use aggressive tuning parameters
myPID.SetTunings(aggKp, aggKi, aggKd);
}

myPID.Compute();
analogWrite(3,Output);
analogWrite(PIN_OUTPUT, Output);
}


12 changes: 8 additions & 4 deletions examples/PID_Basic/PID_Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

#include <PID_v1.h>

#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
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

void setup()
{
//initialize the variables we're linked to
Input = analogRead(0);
Input = analogRead(PIN_INPUT);
Setpoint = 100;

//turn the PID on
Expand All @@ -23,9 +27,9 @@ void setup()

void loop()
{
Input = analogRead(0);
Input = analogRead(PIN_INPUT);
myPID.Compute();
analogWrite(3,Output);
analogWrite(PIN_OUTPUT, Output);
}


22 changes: 13 additions & 9 deletions examples/PID_RelayOutput/PID_RelayOutput.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@
*
* to connect them together we use "time proportioning
* control" it's essentially a really slow version of PWM.
* first we decide on a window size (5000mS say.) we then
* first we decide on a window size (5000mS say.) we then
* set the pid to adjust its output between 0 and that window
* size. lastly, we add some logic that translates the PID
* output into "Relay On Time" with the remainder of the
* output into "Relay On Time" with the remainder of the
* window being "Relay Off Time"
********************************************************/

#include <PID_v1.h>
#define RelayPin 6

#define PIN_INPUT 0
#define RELAY_PIN 6

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

int WindowSize = 5000;
unsigned long windowStartTime;

void setup()
{
windowStartTime = millis();

//initialize the variables we're linked to
Setpoint = 100;

Expand All @@ -41,18 +45,18 @@ void setup()

void loop()
{
Input = analogRead(0);
Input = analogRead(PIN_INPUT);
myPID.Compute();

/************************************************
* turn the output pin on/off based on pid output
************************************************/
if(millis() - windowStartTime>WindowSize)
if (millis() - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
else digitalWrite(RELAY_PIN, LOW);

}

Expand Down