Clamping output of a variable

I have a pressure sensor 0-30 PSI , and want to clamp the MAX value to 31 psi ( it has over range to about 35psi ) to protect my gauge , there is a stepper motor running a Pressure regulator and I use the feedback from the sensor in a closed loop , It will not clamp with the code I have ??? any ideas or did I do something stupid again lol ?
I am trying to control "psi" seek psi

#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "DRV8825.h"
#include <MovingAverage.h>
MovingAverage average(0.01f);

#define MOTOR_STEPS 200
#define RPM 80
#define MICROSTEPS 1
#define DIR 8
#define STEP 9
#define ENABLE 10

A4988 stepper(MOTOR_STEPS, DIR, STEP, ENABLE);

const int ANALOG_PIN0 = A2;  // sensor
const int ANALOG_PIN1 = A3;//pot
int sensorValue0 = analogRead(A2); // sensor
int sensorValue1 = analogRead(A3); // pot
int val0 = 0;
int val1 = 0;
float deadBand;
double error;
double psi0;

void setup() {
  Serial.begin(115200);
  average.reset( analogRead(A2) );  // sensor

  stepper.begin(RPM, MICROSTEPS);

  deadBand = 15;  //stop moving when error < deadBand
}
void measurePressure() {
  int raReading = analogRead(A2);  // sensor

  float psi0 = ((30 - 0) / (675.0 - 127.3)) * (raReading - 0);
  Serial.println(psi0);
  Serial.println("psi");
  

}
void loop() {
    ( average.update( analogRead(0) ) );

  measurePressure();

  val0 = analogRead(A2);     // read the input pin
  Serial.println(val0);             // sensor
  //Serial.println("sensor");
  val1 = analogRead(A3);     // read the input pin
  Serial.println(val1);             // pot
  //Serial.println("pot");

  error = val0 - val1;
  Serial.println(error);
  if (abs(error) < deadBand ) error = 0;
  
  stepper.enable();
  stepper.move(-error);
  stepper.disable();

  if (abs(psi0) > 31.0 ) stepper.disable();// MAX output clamp
  
  delay(5);
}

I have a pressure sensor 0-30 PSI , and want to clamp the MAX value to 31 psi

You can't do that with software. You CAN constrain() the value that you read to be in some range.

int sensorValue0 = analogRead(A2); // sensor
int sensorValue1 = analogRead(A3); // pot

It is useless to read the pins AT COMPILE TIME.

The compiler doesn't know that anything is attached to some other computer.

    ( average.update( analogRead(0) ) );

(What's) (with) (the) (useless) (parentheses) (?)

  measurePressure();

That's a useless function, since it doesn't alter a global variable or return a value.

  if (abs(psi0) > 31.0 ) stepper.disable();// MAX output clamp

It's pointless to disable the stepper AFTER you've crashed the needle into the physical stop.

Next time around, you are just going to enable it again, and crash it again.

PaulS:
You can't do that with software. You CAN constrain() the value that you read to be in some range.

Ok good to know !

int sensorValue0 = analogRead(A2); // sensor

int sensorValue1 = analogRead(A3); // pot



It is useless to read the pins AT COMPILE TIME.

The compiler doesn't know that anything is attached to some other computer.
**This came from code from another person ? I wondered about that because they are being called just after this** 




( average.update( analogRead(0) ) );



(What's) (with) (the) (useless) (parentheses) (?)

**code from moving avg.h a library online so ??** 



measurePressure();



That's a useless function, since it doesn't alter a global variable or return a value.
**Also from other coder ?** 



if (abs(psi0) > 31.0 ) stepper.disable();// MAX output clamp




It's pointless to disable the stepper AFTER you've crashed the needle into the physical stop.
**the stop is about 35 psi so stopping at 31 will not crash it** 

Next time around, you are just going to enable it again, and crash it again.

Again thanks for the reply , I will look up constrain

Where would I put the Constrain ? loop or setup I tried it both places with no luck , new to Arduino FYI

void setup() {
Serial.begin(115200);
average.reset( analogRead(A2) ); // sensor

stepper.begin(RPM, MICROSTEPS);

deadBand = 15; //stop moving when error < deadBand
}
void measurePressure() {
int raReading = analogRead(A2); // sensor

float psi0 = ((30 - 0) / (675.0 - 127.3)) * (raReading - 0);
Serial.println(psi0);
Serial.println("psi");
constrain(psi0,0,30);
}

I would use something like this:

if (psi0 < 0.0) {
  psi0 = 0.0;
}
if (psi0 > 30.0) {
  psi0 = 30.0;
}

I will look up constrain

Well, do it again. constrain() returns a value. If you don't care what the returned value is, it is pointless to call it.

I tried several things , Im going to just switch the power to the stepper driver , it never seems to disable ? weird , so when it is at position power down the driver.

buzzkill:
I tried several things , Im going to just switch the power to the stepper driver , it never seems to disable ? weird , so when it is at position power down the driver.

But, you don't seem to have tried posting the COMPLETE code where you called constrain AND used the (constrained) value that it returns. Go ahead. Toss in the towel. I don't care. It's not my hardware that will be damaged.