enhance coding style
This commit is contained in:
parent
d46dded42d
commit
d7c61d2556
|
@ -11,6 +11,9 @@
|
||||||
|
|
||||||
#include <PID_v1.h>
|
#include <PID_v1.h>
|
||||||
|
|
||||||
|
#define PIN_INPUT 0
|
||||||
|
#define PIN_OUTPUT 3
|
||||||
|
|
||||||
//Define Variables we'll be connecting to
|
//Define Variables we'll be connecting to
|
||||||
double Setpoint, Input, Output;
|
double Setpoint, Input, Output;
|
||||||
|
|
||||||
|
@ -24,7 +27,7 @@ PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
//initialize the variables we're linked to
|
//initialize the variables we're linked to
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
Setpoint = 100;
|
Setpoint = 100;
|
||||||
|
|
||||||
//turn the PID on
|
//turn the PID on
|
||||||
|
@ -33,10 +36,10 @@ void setup()
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
|
|
||||||
double gap = abs(Setpoint-Input); //distance away from setpoint
|
double gap = abs(Setpoint-Input); //distance away from setpoint
|
||||||
if(gap<10)
|
if (gap < 10)
|
||||||
{ //we're close to setpoint, use conservative tuning parameters
|
{ //we're close to setpoint, use conservative tuning parameters
|
||||||
myPID.SetTunings(consKp, consKi, consKd);
|
myPID.SetTunings(consKp, consKi, consKd);
|
||||||
}
|
}
|
||||||
|
@ -47,7 +50,7 @@ void loop()
|
||||||
}
|
}
|
||||||
|
|
||||||
myPID.Compute();
|
myPID.Compute();
|
||||||
analogWrite(3,Output);
|
analogWrite(PIN_OUTPUT, Output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,16 +5,20 @@
|
||||||
|
|
||||||
#include <PID_v1.h>
|
#include <PID_v1.h>
|
||||||
|
|
||||||
|
#define PIN_INPUT 0
|
||||||
|
#define PIN_OUTPUT 3
|
||||||
|
|
||||||
//Define Variables we'll be connecting to
|
//Define Variables we'll be connecting to
|
||||||
double Setpoint, Input, Output;
|
double Setpoint, Input, Output;
|
||||||
|
|
||||||
//Specify the links and initial tuning parameters
|
//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()
|
void setup()
|
||||||
{
|
{
|
||||||
//initialize the variables we're linked to
|
//initialize the variables we're linked to
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
Setpoint = 100;
|
Setpoint = 100;
|
||||||
|
|
||||||
//turn the PID on
|
//turn the PID on
|
||||||
|
@ -23,9 +27,9 @@ void setup()
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
myPID.Compute();
|
myPID.Compute();
|
||||||
analogWrite(3,Output);
|
analogWrite(PIN_OUTPUT, Output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,16 +15,20 @@
|
||||||
********************************************************/
|
********************************************************/
|
||||||
|
|
||||||
#include <PID_v1.h>
|
#include <PID_v1.h>
|
||||||
#define RelayPin 6
|
|
||||||
|
#define PIN_INPUT 0
|
||||||
|
#define RELAY_PIN 6
|
||||||
|
|
||||||
//Define Variables we'll be connecting to
|
//Define Variables we'll be connecting to
|
||||||
double Setpoint, Input, Output;
|
double Setpoint, Input, Output;
|
||||||
|
|
||||||
//Specify the links and initial tuning parameters
|
//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;
|
int WindowSize = 5000;
|
||||||
unsigned long windowStartTime;
|
unsigned long windowStartTime;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
windowStartTime = millis();
|
windowStartTime = millis();
|
||||||
|
@ -41,18 +45,18 @@ void setup()
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
Input = analogRead(0);
|
Input = analogRead(PIN_INPUT);
|
||||||
myPID.Compute();
|
myPID.Compute();
|
||||||
|
|
||||||
/************************************************
|
/************************************************
|
||||||
* turn the output pin on/off based on pid output
|
* turn the output pin on/off based on pid output
|
||||||
************************************************/
|
************************************************/
|
||||||
if(millis() - windowStartTime>WindowSize)
|
if (millis() - windowStartTime > WindowSize)
|
||||||
{ //time to shift the Relay Window
|
{ //time to shift the Relay Window
|
||||||
windowStartTime += WindowSize;
|
windowStartTime += WindowSize;
|
||||||
}
|
}
|
||||||
if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
|
if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
|
||||||
else digitalWrite(RelayPin,LOW);
|
else digitalWrite(RELAY_PIN, LOW);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue