Controlling stepper motor with sensor

I have a stepper motor and a pressure sensor. Right now I have the motor running, and then with a separate code I can read values from the pressure sensor.

I now want to control the motor so that it will stop when a certain reading from the sensor is reached. I am new to arduino/coding so haven't figured it out yet.

Any help would be great.
Thanks!

This is the code that drives the motor:

/*************************************************************
Motor Shield Stepper Demo
by Randy Sarafan

For more information see:
http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

*************************************************************/

int delaylegnth = 30;

void setup() {
 
 //establish motor direction toggle pins
 pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
 pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
 
 //establish motor brake pins
 pinMode(9, OUTPUT); //brake (disable) CH A
 pinMode(8, OUTPUT); //brake (disable) CH B


 
 
}

void loop(){

 digitalWrite(9, LOW);  //ENABLE CH A
 digitalWrite(8, HIGH); //DISABLE CH B

 digitalWrite(12, HIGH);   //Sets direction of CH A
 analogWrite(3, 255);   //Moves CH A
 
 delay(delaylegnth);
 
 digitalWrite(9, HIGH);  //DISABLE CH A
 digitalWrite(8, LOW); //ENABLE CH B

 digitalWrite(13, LOW);   //Sets direction of CH B
 analogWrite(11, 255);   //Moves CH B
 
 delay(delaylegnth);
 
 digitalWrite(9, LOW);  //ENABLE CH A
 digitalWrite(8, HIGH); //DISABLE CH B

 digitalWrite(12, LOW);   //Sets direction of CH A
 analogWrite(3, 255);   //Moves CH A
 
 delay(delaylegnth);
   
 digitalWrite(9, HIGH);  //DISABLE CH A
 digitalWrite(8, LOW); //ENABLE CH B

 digitalWrite(13, HIGH);   //Sets direction of CH B
 analogWrite(11, 255);   //Moves CH B
 
 delay(delaylegnth);
 

}

And this is the code that reads the sensor:

int analogPin = A0;     // potentiometer wiper (middle terminal) connected to analog pin 3

                      // outside leads to ground and +5V

int val = 0;           // variable to store the value read



void setup()


{

 Serial.begin(9600);          //  setup serial

}



void loop()

{

 val = analogRead(analogPin);    // read the input pin

 Serial.println(val);             // debug value

}

What type of stepper are you using? Looks to be a bipolar from your code.

Are you using a stepper driver? You are toggling a direction / brake pin in setup().

Also, I'm not sure why you are using a PWM to control the stepper. PWM isn't of use with stepper motors. Read this over. Stepper Motors – code, circuits, & construction

If your motor is in fact bipolar, it should be a simple matter of locking the motor into position so that it does not move. This is done by setting all of the phases HIGH (or channels as you are calling them).

So your code would look like "IF the pressure sensor reading is a value X, THEN enable channels A and B"