Ping sensor and Step motor

Hello.

I am trying to add PING sensor switch to trigger steppers movement based on ping distance.
Below code makes the steppers stuttering and not moving naturally… I don’t know what’s the problem. If I add delay() for ping reading in the loop, it also makes the steppers not moving as those should be.

Thanks.

#include <AccelStepper.h>
#include <NewPing.h>

const int step1cues[] = {0, 3000};
const int step2cues[] = {0, 3000};

// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);

byte movingStepper;

int blueLED = 11;
int redLED = 12;


// Distance from position when the motor should start
#define THRESHOLD 2000



#define TRIGGER_PIN  4  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     3  // Arduino pin tied to echo pin on the ultrasonic sensor.

#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.



NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.



void setup()
{
  Serial.begin(9600);

  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  pinMode(redLED, OUTPUT);
  pinMode(blueLED, OUTPUT);

  // A0 should be disconnected. If it is used
  // change this to an unused analog pin
  randomSeed(analogRead(0));

  stepper1.setMaxSpeed(10000);
  stepper1.setAcceleration(3000); // higher = faster
  stepper2.setMaxSpeed(10000);
  stepper2.setAcceleration(3000);

  // Make sure that steppers are at zero
  stepper1.setCurrentPosition(0);
  stepper2.setCurrentPosition(0);

  // Pretend that we were moving motor 2
  movingStepper = 2;
}


void loop()
{
  int ping = sonar.ping_cm();

  for (ping = 0; ping >= 300; ping++) {
    int zero = 0;

    digitalWrite(blueLED, LOW);
    digitalWrite(redLED, LOW);
    Serial.print("Not Walking" );
    stepper1.moveTo(zero);
    stepper2.moveTo(zero);

    movingStepper = 1;
    movingStepper = 2;

  }

  for (ping = 0; ping > 200 && ping < 300; ping++) {
    int zero = 0;
    int nextpos;

    nextpos = step2cues[random(sizeof(step2cues) / sizeof(int))];
    Serial.print("moving Stepper2 to = " );
    Serial.println(abs(nextpos - stepper2.targetPosition())) ;
    stepper2.moveTo(nextpos);
    movingStepper = 2;

    stepper1.moveTo(zero);
    digitalWrite(blueLED, HIGH);

  }


  for (ping = 0; ping <= 200; ping++) {

    if (movingStepper == 1)
    {
      if (abs(stepper1.distanceToGo()) < THRESHOLD)
      {
        int nextpos;

        {
          nextpos = step2cues[random(sizeof(step2cues) / sizeof(int))];
          Serial.print("moving Stepper2 to = " );
          Serial.println(abs(nextpos - stepper2.targetPosition())) ;
        } while (abs(nextpos - stepper2.targetPosition()) < THRESHOLD);
        stepper2.moveTo(nextpos);
        movingStepper = 2;

        digitalWrite(blueLED, HIGH);

      }

    }
    else
    {
      if (abs(stepper2.distanceToGo()) < THRESHOLD)
      {
        int nextpos;

        {
          nextpos = step1cues[random(sizeof(step1cues) / sizeof(int))];
          Serial.print("moving Stepper1 to = " );
          Serial.println(abs(nextpos - stepper1.targetPosition())) ;
        } while (abs(nextpos - stepper1.targetPosition()) < THRESHOLD);
        stepper1.moveTo(nextpos);
        movingStepper = 1;

        digitalWrite(redLED, HIGH);

      }
    }
    //
  }


  stepper1.run();
  stepper2.run();


}
    pinMode(switch1, OUTPUT);
    pinMode(switch2, OUTPUT);

Switches are input devices.

  while (sonar.ping_cm() <= 100){

You seem to have forgotten that loop() loops.

The inconsistent placement of curly braces makes your code hard to read. Put ALL { on lines by themselves. Use Tools + Auto Format to improve the indenting.

You seem far to hung up on while loops to do something that loop() looping should handle. Why should the stepper move more than one step between readings of the ping sensor?

        if ( nextpos = stepper2.targetPosition() ){

Why use an if statement to assign a value to a variable? = is NOT the comparison operator.

Thank you for the advice, those are not switches, LEDs. I have fixed and formatted the code I uploaded.
I assume the problem is caused by the sonar reading delay() which is not assigned, but with delay() my motors get affected.. Could you help me to fix this issue without modifying the existing code for motors?

Best,