reballing

this is the second post for this topic i posted it in project guidence that part is pritty much there im actually work now on the code for this project so im sorry about the double post but im having trouble with this i got the code to read from my thermal couple max6675 now my issue is its not turnng on my ssr here is my code any advice would be great

/********************************************************
 * 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"  it'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>
#include "max6675.h"
#define RelayPin 7

//MAX6675 STUFF

int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;
//End MAX
//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 = 1000; //default is 5000
unsigned long windowStartTime;
void setup()
{
  windowStartTime = millis();
  
  //initialize the variables we're linked to
  Setpoint = 100;  // I think this is the temp in Celcius we are aiming for - default is 100

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

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  //More MAX6675
   Serial.begin(9600);
  pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
  pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
  //END MAX
}

void loop()
{
  delay(500);
  Input = thermocouple.readCelsius();
  myPID.Compute();
   Serial.print(" Input: ");
   Serial.print(Input);
   Serial.print(" Output: ");
   Serial.print(Output);
   Serial.print(" Setpoint: ");
   Serial.println(Setpoint);
  /************************************************
   * turn the output pin on/off based on pid output
   ************************************************/
  if(millis() - windowStartTime > WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if(Output > millis() - windowStartTime) 
  {Serial.print("relaypin HIGH - ");  //digitalWrite(RelayPin,HIGH);
  }
  else {Serial.print("relaypin LOW - ");//digitalWrite(RelayPin,LOW);
   }
  
}

solved that issue i had to set the pin as an output thought it was already done now say if i wanted to use two pins as relaypin i would just put in #define RelayPin 7-8 then they would automatically once i set relay pin as output they would both work like that ???????

okay im lost i think im missing something any advice plz heres the code im using and what i get from serial monitor
/********************************************************

  • 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" it’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>
#include “max6675.h”
#define RelayPin 13

//MAX6675 STUFF

int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;
//End MAX
//Define Variables we’ll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &relaypin, &Setpoint,2,5,1, DIRECT);

int WindowSize = 2000; //default is 5000
unsigned long windowStartTime;
void setup()
{
#define relaypin 13
pinMode(13, OUTPUT);
windowStartTime = millis();

//initialize the variables we’re linked to
Setpoint = 100; // I think this is the temp in Celcius we are aiming for - default is 100

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

//turn the PID on
myPID.SetMode(AUTOMATIC);
//More MAX6675
Serial.begin(9600);
pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
//END MAX
}

void loop()
{
delay(500);
Input = thermocouple.readCelsius();
myPID.Compute();
Serial.print(" Input: “);
Serial.print(Input);
Serial.print(” Output: “);
Serial.print(Output);
Serial.print(” Setpoint: ");
Serial.println(Setpoint);
/************************************************

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

}

1.00 Output: 0.00 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 237.00 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 276.50 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 319.13 Setpoint: 100.00
relaypin HIGH - Input: 20.50 Output: 359.38 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 393.50 Setpoint: 100.00
relaypin LOW - Input: 21.25 Output: 429.38 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 480.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 511.50 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 556.63 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 590.63 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 635.75 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 669.75 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 711.75 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 754.38 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 791.50 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 831.13 Setpoint: 100.00
relaypin LOW - Input: 20.25 Output: 877.00 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 902.50 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 952.63 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 989.75 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 1029.38 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 1065.88 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 1107.88 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 1147.38 Setpoint: 100.00
relaypin LOW - Input: 21.25 Output: 1183.75 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 1231.88 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 1266.50 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 1303.00 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 1345.00 Setpoint: 100.00
relaypin HIGH - Input: 20.50 Output: 1390.75 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 1422.38 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 1461.38 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 1503.38 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 1542.88 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 1585.50 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 1619.50 Setpoint: 100.00
relaypin LOW - Input: 20.75 Output: 1664.63 Setpoint: 100.00
relaypin HIGH - Input: 20.50 Output: 1704.88 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 1739.00 Setpoint: 100.00
relaypin HIGH - Input: 21.25 Output: 1774.88 Setpoint: 100.00
relaypin LOW - Input: 21.00 Output: 1822.38 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 1862.50 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 1899.63 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 1936.13 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 1981.25 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.25 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.25 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.50 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.50 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.50 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.25 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.25 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 21.00 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.25 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.50 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00
relaypin HIGH - Input: 20.75 Output: 2000.00 Setpoint: 100.00

i would just put in #define RelayPin 7-8

7 minus 8 is -1. Do you really have a pin -1?

Whatever it is you are doing with Output, it is NOT helping, so the PID process keeps cranking up the value. I can't imagine how writing the value to the serial port is going to raise the temperature you are measuring.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Thanks...... :)

This looks like the program is working correctly but the problem is:

now my issue is its not turnng on my ssr

(Punctuation and line breaks would make it easier to read and find the actual problem in the original description.)

What is the SSR? Have you read its datasheet? Is the Arduino even capable of supplying enough current or voltage to turn on the SSR? Can you put a multimeter on the pins and verify the output?

[u]Programming advice:[/u] Don't set the pin mode every time in loop(). Set it once in setup() and you don't need to do it again.

Can you try to rename the double Output to some other name?

OUTPUT is also a #define and same for relayPin ...

interference is just around the corner ....

#include <PID_v1.h>
#include "max6675.h"
#define RelayPin 13
#define Relaypin OUTPUT
//MAX6675 STUFF

int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;
//End MAX
//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 = 2000; //default is 5000
unsigned long windowStartTime;
void setup()
{
#define relaypin 13
pinMode(13, OUTPUT);
windowStartTime = millis();

//initialize the variables we're linked to
Setpoint = 100;  // I think this is the temp in Celcius we are aiming for - default is 100

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

//turn the PID on
myPID.SetMode(AUTOMATIC);
//More MAX6675
Serial.begin(9600);
pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
//END MAX
}

void loop()
{
delay(500);
Input = thermocouple.readCelsius();
myPID.Compute();
Serial.print(" Input: ");
Serial.print(Input);
Serial.print(" Output: ");
Serial.print(Output);
Serial.print(" Setpoint: ");
Serial.println(Setpoint);
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if(millis() - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output > millis() - windowStartTime) 
{Serial.print("relaypin HIGH - ");  //digitalWrite(RelayPin,HIGH);
}
else {Serial.print("relaypin LOW - ");//digitalWrite(RelayPin,LOW);
}

}

okay let me know how this looks im not sure why i would rename double out put the only thing i can think that wrong is the fact that im telling the uno to only print to serial not actually changing the state of pin 13 to high low any advice and opinion

light0070702:
the only thing i can think that wrong is the fact that im telling the uno to only print to serial not actually changing the state of pin 13 to high low

If that is wrong you should probably un-comment the digitalWrite’s

#include <PID_v1.h>
#include "max6675.h"
const int RelayPin = 13;
//MAX6675 STUFF
int gndPin = 2;
int vccPin = 3;
int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
//End MAX6675 STUFF
//Define PID Variables
double Setpoint, Input, Output;
//Specify the PID tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, DIRECT);
const unsigned long WindowSize = 2000; //default is 5000
unsigned long windowStartTime;
void setup() {
  Serial.begin(9600);
  digitalWrite(RelayPin, LOW);
  pinMode(RelayPin, OUTPUT);
  windowStartTime = millis();
  //initialize the variables we're linked to
  Setpoint = 100.0;  // I think this is the temp in Celcius we are aiming for - default is 100
    //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0.0, (double)WindowSize);
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  // Power up the MAX6675
  pinMode(vccPin, OUTPUT); 
  digitalWrite(vccPin, HIGH);
  pinMode(gndPin, OUTPUT); 
  digitalWrite(gndPin, LOW);
  //END MAX
}
void loop() {
  delay(500);
  Input = thermocouple.readCelsius();
  myPID.Compute();  // Calculate 'Output' from 'Input' and 'Setpoint'
  Serial.print(" Input: ");
  Serial.print(Input);
  Serial.print(" Output: ");
  Serial.print(Output);
  Serial.print(" Setpoint: ");
  Serial.println(Setpoint);
  /************************************************
   * turn the output pin on/off based on pid output
   ************************************************/
  if(millis() - windowStartTime > WindowSize) { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if(Output > millis() - windowStartTime) {
    Serial.print("relaypin HIGH - ");
    digitalWrite(RelayPin,HIGH);
  }
  else {
    Serial.print("relaypin LOW - ");
    digitalWrite(RelayPin,LOW);
  }
}

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom...... :)

It looks like you're trying to control the relay using pin 13. (Do you realize that pin 13 has less drive strength than other pins because much of the current is going towards lighting the on-board LED? Does your relay driver take that into account?)

But first, it looks like you don't understand some basic underlying concepts, and are using code phrases that you've seen elsewhere and are including them because they look right, not because you know what they do. (This is not meant to be a negative comment, everybody had to learn it at some point, and we are here trying to teach.)

#define RelayPin 13
#define Relaypin OUTPUT

You first define RelayPin to be "13" then you redefine RelayPin to be "OUTPUT" which is defined by the Arduino environment to have the value 0x1 (one in hexadecimal notation.) So, after this point, anytime you use the word RelayPin, the compiler will substitute 0x1.

That first definition of RelayPin is good. You really don't want the second one. Get rid of it.

void setup()
{
#define relaypin 13
pinMode(13, OUTPUT);

Now, in the beginning of setup(), you now define relaypin to be 13. So from here out, whenever you use the word relayin, it will be as if you had typed 13 instead. Note that this is NOT redefining the previous definition of RelayPin, as all names are case sensitive. You probably don't want to introduce yet another version of relaypin, this line should not be here, especially since it is not referenced anywhere else in the code.

In the next line, you then set pin 13 to be an output. At that point, you probably should rely on the first definition of RelayPin (the one that was set to the value 13.) So get rid of the #define of relaypin, and change the pinMode call to "pinMode(RelayPin, OUTPUT);"

if(Output > millis() - windowStartTime) 
{Serial.print("relaypin HIGH - ");  //digitalWrite(RelayPin,HIGH);
}
else {Serial.print("relaypin LOW - ");//digitalWrite(RelayPin,LOW);
}

This is the real meat of the relay control. The digitalWrite calls look good, but they won't do anything since they are comments. If you remove the comment indicators, then they should cause the relay to switch (assuming correct hardware.) While the code should be functional, it's difficult to read. Get in the habit of formatting the code better, and you (and others) will have an easier time reading and maintaining it. For example, better formatting for that code would be:

  if (Output > millis() - windowStartTime) 
  {
    Serial.print("relaypin HIGH - ");
    digitalWrite(RelayPin,HIGH);
  }
  else
  {
    Serial.print("relaypin LOW - ");
    digitalWrite(RelayPin,LOW);
  }

I personally like the opening curly braces on their own line, others (including the Arduino IDE's formatter) like to put the opening brace on the same line as the if and else. That's a matter of personal style. Formatting the code (using any style) doesn't change the way the code works, but does make it easier for you to write and change it. A few seconds now formatting the code can save hours of debugging time. Read this: Indenting Styles -- I like the Allman style, the Arduino IDE uses the Stroustrup style. Look at the various formatting styles, pick one you like, and stick to it.

And to expand on what PaulS said earlier:

light0070702: now say if i wanted to use two pins as relaypin i would just put in #define RelayPin 7-8 then they would automatically once i set relay pin as output they would both work like that ???????

Nope, as PaulS pointed out, this won't work. C++ (and therfore Arduino code) does not have the concept of specifying a range like "7-8" but instead will treat this as a simple math expression. So if you "#define RelayPin 7-8" then anywhere you use RelayPin the compiler will substitute 7-8. So pinMode(RelayPin, OUTPUT) would turn into pinMode(7-8, OUTPUT) which would be the same thing as saying pinMode(-1, OUTPUT). Instead of setting the output mode of pins 7 and 8, the code would try to set pin -1 as an output, which is of course erroneous. The same thing would happen when calling DigitalWrite().