Code not responding to IF statement

Hi, I’m trying to create an accelerating motion in my code using the if(stepper1.distanceToGo()<****), I’ve tested it on it’s own without the full code and it works fine, an example would be, if the distance to go is <2000 it increases speed.

When I try to implement it into my code it doesn’t work, the if statement is completely ignored, any help appreciated, here’s a bit off the code.

stepper1.setMaxSpeed(2275);
  digitalWrite(startScan, HIGH);
  delay(100);
  digitalWrite(startScan, LOW);
  digitalWrite(fanPin, HIGH);
  stepper1.moveTo(16000);
if (stepper1.distanceToGo() <8000)
{
  stepper1.setMaxSpeed(6000);
  digitalWrite(startScan, HIGH);
  delay(100);
  digitalWrite(startScan, LOW);
  digitalWrite(fanPin, LOW);
  }

entire code is here

#include <AccelStepper.h>
#include <SM.h>
const int buttonPin = A0; 
const int pulseopto1 = 9;
const int startScan = 11;
const int stopButton = A1;
const int fanPin = 5;

int ledState = LOW; 
int buttonState = 0; 
long previousMicros = 0;
long interval = 700;
SM Controller(Parked);
AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 3);
void setup()
{ 
  pinMode(stopButton, INPUT);
  pinMode(startScan, OUTPUT);
  pinMode(pulseopto1, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(fanPin, OUTPUT);
  stepper1.setMaxSpeed(2000);
  stepper1.setAcceleration(14000.0);
}
void loop(){ 
  buttonState = digitalRead(buttonPin);
  EXEC(Controller);
  stepper1.run();
      unsigned long currentMicros = micros();

  if(currentMicros - previousMicros > interval) {
    // save the last time you blinked the LED
    previousMicros = currentMicros;  
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(pulseopto1, ledState);
    }
}//loop()
State Parked(){//wait for sensor to be activated


  if(analogRead(A2)>600){
  stepper1.setCurrentPosition(0);
  stepper1.stop(); }
  digitalWrite(fanPin, LOW);
  if (buttonState == LOW) 
  { 
    Controller.Set(SlowH, SlowB);
  }
  else if (analogRead(A2)<600){
    stepper1.moveTo(-87500);
  digitalWrite(fanPin, LOW);}
}//Parked()
State SlowH(){// start moving forward at slow speed

  stepper1.setMaxSpeed(2275);
  digitalWrite(startScan, HIGH);
  delay(100);
  digitalWrite(startScan, LOW);
  digitalWrite(fanPin, HIGH);
  stepper1.moveTo(16000);
if (stepper1.distanceToGo() <8000)
{
  stepper1.setMaxSpeed(6000);
  digitalWrite(startScan, HIGH);
  delay(100);
  digitalWrite(startScan, LOW);
  digitalWrite(fanPin, LOW);
  }
}//SlowH()
State SlowB(){//check remainng distance while moving
  if(stepper1.distanceToGo() == 0) Controller.Set(ReverseC);
}//SlowB()


State ReverseC(){

if (analogRead(A2)>600)
{
      stepper1.setCurrentPosition(0);
      Controller.Set(SlowH, SlowB);
}
else
{ stepper1.moveTo(-8750);
  stepper1.setMaxSpeed(6000);
}
}

the if statement is completely ignored

Your proof seems to be non-existent.

if (stepper1.distanceToGo() <8000)
{
  stepper1.setMaxSpeed(6000);

If it’s getting close, speed up. How does that make sense?

I’d suggest that http://snippets-r-us.com might be a more useful place to go with your poorly indented snippets.

if (stepper1.distanceToGo() <8000)

If the if is not being executed then it’s because this is false.
To debug try to do a Serial.println(stepper1.distanceToGo()); before the if statment

Going to try that, thanks hugoPT, also posted entire code.