Stop PID function (PID library: brettbeauregard)

Hi all,

I am using the PID library from brettbeauregard to control temperature with:
-Peltier with 2 fans
-Temperature sensor
-Joystick

The wiring and needed code around it has been checked and it is correct.

I want to be able to stop the PID function at will. For this, I am using a variable, "PeltierMode". When it is 0, I want the PID function to stop. When it is 1, I want the function running.

I am able to see when "PeltierMode" is 0 or 1 and all the other variables through the serial.

This is the basic part of the code with no modifications/tests:

  Input_Peltier = Temperature;
  Setpoint_Peltier = TSetPoint;
  PID_Peltier.Compute();
  analogWrite(PeltierOutputPin, Output_Peltier);
  

  if (PeltierMode = 1) {
  PID_Peltier.SetMode(AUTOMATIC);
  }

  if (PeltierMode = 0) {
  PID_Peltier.SetMode(MANUAL);
  }

I always had the Manual and Automatic codes in the 2 different "if functions". I have tried the following variants (unsuccessfully):

-When PeltierMode = 0 (off) --> Manual and Output_Peltier = 0. For PeltierMode = 1 (on), normal variable reading "analogWrite(PeltierOutputPin, Output_Peltier)";
-When PeltierMode = 0 (off) --> Kp, Ki, Kd =0. Normal values when PeltierMode = 1 (on)
-When PeltierMode = 0 (off) --> SetOutputLimits(0, 0). Normal values when PeltierMode = 1 (on) (0, 255)
-PID_Peltier.Compute() only in "if function" when PeltierMode = 1 (on)

None of these worked and I ran out of ideas. Could you please help me?

  if (PeltierMode = 1) {

Assigning a value to a variable in an if statement is relatively unusual.

Why do you unconditionally call Compute(), if you want to disable PID?

PaulS:
if (PeltierMode = 1)
Assigning a value to a variable in an if statement is relatively unusual.

I did it like this because I easily created a menu where I can change the value of the variable very easily with a joystick.

PaulS:
Why do you unconditionally call Compute(), if you want to disable PID?

In the fourth test I did, I enclosed Compute() only in the PeltierMode = 1 (on) "if function". It did not work. The arduino would still turn on the peltier to cool down, which I do not fully understand.

I could change the output pin to a "ghost/fake one" while PeltierMode = 0, but I do not think it is a great solution. Maybe someone has dealt with this before.

russelcrowe_:
I did it like this because I easily created a menu where I can change the value of the variable very easily with a joystick.

And that one line very easily changes it back to 1. Every time.

MorganS:
And that one line very easily changes it back to 1. Every time.

No it does not. I can see it in the serial that PeltierMode stays = 0. As I said, the rest of the code works flawless.

russelcrowe_:
No it does not. I can see it in the serial that PeltierMode stays = 0. As I said, the rest of the code works flawless.

That might have something to do with the next if block setting it to just that.

= != ==

russelcrowe_:
No it does not. I can see it in the serial that PeltierMode stays = 0. As I said, the rest of the code works flawless.

Seriously?

If you know what the problem is, you wouldn't be here asking for help. When you come here asking for help, and somebody tries to guide you to the problem (without putting it in flashing, colored lights), don't act like you know better. Actually read what you are being told.

  if (PeltierMode = 1)

should be

  if (PeltierMode == 1)

= assigns the value on the right to the variable on the left
== compares the value on the right with the variable on the left

Your code would evaluate to

 if (1)

which will always execute the code in the body of the if statement.

Sorry for the late reply. I appreciate your reply. You pointed out my coding error (typing = instead of == in the if function):

if (PeltierMode == 1) {
  PID_Peltier.SetMode(AUTOMATIC);
  analogWrite(PeltierOutputPin, Output_Peltier);
  }

  if (PeltierMode == 0) {
  PID_Peltier.SetMode(MANUAL);
  Output_Peltier = 0;
  }

I changed the subject to "Solved"

evanmars:
Seriously?

If you know what the problem is, you wouldn't be here asking for help. When you come here asking for help, and somebody tries to guide you to the problem (without putting it in flashing, colored lights), don't act like you know better. Actually read what you are being told.

I am sorry my answer to MorganS got missinterpreted. All I tried to point out as precise as possible where my code error should be (as I try to solve the issues myself for hours before posting). I do this to try to save the people that kindly help as much time as possible.

I carefully read what I am being told as you suggests. Unfortunately, the previously mentioned user did not suggest anything, only criticised the code.

Once again, thank you very much for your help! :slight_smile: