What is RELAY_PIN in PID_RelayOutput example

Hello. (I'm new).

I've been tinkering around with the PID_v1.h library, particularly with the PID_RelayOuput example.

I don't really understand what "RELAY_PIN 13" is for. (Internet searching didn't help)

Can please someone explain?

/********************************************************
   PID RelayOutput Example
   Same as basic example, except that this time, the output
   is going to a digital pin which (we presume) is controlling
   a relay.  The pid is designed to output an analog value,
   but the relay can only be On/Off.

     To connect them together we use "time proportioning
   control"  Tt's essentially a really slow version of PWM.
   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
   window being "Relay Off Time"
 ********************************************************/

#include <PID_v1.h>
#define RelayPin 13

//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);

int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
  pinMode(RelayPin, OUTPUT);

  windowStartTime = millis();

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

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

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

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

It defines the pin that the relay is connected to.

Later, in setup(), the pin is configured as output.

And in loop(), you control the relay with digitalWrite.

I don't really understand what "RELAY_PIN 13" is for. (Internet searching didn't help)

There is NO complete statement in your code that looks like that.

There is a #define statement, that associates the value 13 with the name RELAY_PIN.

The preprocessor will search for all occurrences of RELAY_PIN and will substitute the value in place of the name, before the code is seen by the compiler.

Thank you!

I'm sorry about the confusion I might have caused with the way I worded my question.

As a follow up, while I understand that the RelayPin is on when this happens...

if (Output > now - windowStartTime) digitalWrite(RelayPin, HIGH);
  else digitalWrite(RelayPin, LOW);

... I don't really understand what this means. Any help?

Hello.

I understand the library more now. I stumbled upon PID Library's author trying to explain it.
Thanks for all the help :slight_smile:

http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/comment-page-1/