PID control of proportional solenoid

Morning All,

This is my first post on here and I am pretty new to the Arduino.

My problem is I cannot seem to get my code to work to control a proportional relief valve by measuring a pressure sensor.

I have written seperate programs to control the solenoid and read the sensor so I know they are both working but I cannot seem to get the PID control to work, It just outputs the max to my solenoid and maintains.

sensor outputs 0-5v and solenoid is controlled with 0-5v

see code below.

#include <PID_v1.h>

// code to increase pressure to 140 bar in 35 bar increments with 10 second delay inbetween increases before unloading to zero, waiting 20 seconds and repeating.


double Setpoint, Input, Output;

//tuning parameters P,I,D = 2,5,1, 

PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, DIRECT);


void setup() {

  Input = analogRead(A0);

  myPID.SetMode(AUTOMATIC);


  
}

void loop() {


                             //reduce P part of controller to slow down pressure increase, fine tune with the I and D parts

  Setpoint = 90;             //ten bit number for 35 bar assuming full scale 0-400 bar = 0-1023
  Input = analogRead(A0);    //read input from pressure sensor 
  myPID.Compute();           //compute relavent output from tuning parameters and sensor reading
  analogWrite(5, Output);    //send output to propertional valve 

  delay(10000);              //10 second delay 

  Setpoint = 179;            //ten bit number for 70 bar assuming full scale 0-400 bar = 0-1023
  Input = analogRead(A0);
  myPID.Compute();
  analogWrite(5, Output);

  delay(10000);

  Setpoint = 256;           //ten bit number for 100 bar assuming full scale 0-400 bar = 0-1023
  Input = analogRead(A0);
  myPID.Compute();
  analogWrite(5, Output);

  delay(10000);

  Setpoint = 358;           //ten bit number for 140 bar assuming full scale 0-400 bar = 0-1023
  Input = analogRead(A0);
  myPID.Compute();
  analogWrite(5, Output);

  delay(10000);

 
  analogWrite(5, 0);       //unload to minimum pressure

  delay(20000);           //20 second delay before restarting 
  
}

I don’t think you understand how PID works. PID is an iterative software feedback control loop. Note that it is an “iterative” algorithm. Because of this, you need to call myPID.compute() at a constant frequency (with such frequency higher than 1Hz like you have now).

Bottom line: if you want more resolution from the PID library, call myPID.compute() more times before you change the setpoint.

You should do some quick googling on how PID works in general (not necessarily in software).

ahhh okay, thank you!

I understood it was an iterative algorithm but had assumed it repeated itself until I called the next set point
(not sure where I got that idea from).

is there a function I can use that will repeat until the next set point (to create like a mini loop) or do I need to just copy and paste the code multiple times with a smaller delay?

I guess the other question is:

is PID control the best solution for this type of problem or is there a better way to gradually increase the output until the sensor set point is reached?