Stepper Motor Will not run when AccelStpper run(); method is called

I am using the AccelStepper.h library to run a stepper motor using an Arduino Mega. I am trying to make the stepper motor go to the center of its range (11000 steps), and then oscillate back and forth 8 times. The issue is that the motor does not run at all when the Run(); command is called outside of the loop() function. I have posted the code below.

//accelstepper constant speed example has been altered and is being used
// not using default 4 wires setup, but instead using step, direction, and enable pins
// using TB6600 4A 9-42V stepper driver at 6400 pulses/rev (32 microsteps)

#include <AccelStepper.h>

// defines pins numbers
const int stepPin1 = 32;
const int directionPin1 = 34;
const int enablePin1 = 36;

const int stepPin2 = 22;
const int directionPin2 = 24;
const int enablePin2 = 26;


const int limitSwitch = 46;

// Define a stepper and the pins it will use
// 1 or AccelStepper::DRIVER means a stepper driver (with Step and Direction pins)

AccelStepper stepper2(AccelStepper::DRIVER, stepPin2, directionPin2);
void setup()
{
  Serial.begin(19200);

  pinMode(limitSwitch, INPUT);



  stepper2.setEnablePin(enablePin2);
  stepper2.setPinsInverted(false, false, true);
  stepper2.enableOutputs();

  stepper2.setCurrentPosition(0);
  friction(750, 18000);
}

void loop()
{

  
}


void friction(int sampleWidthSteps, int speed){

  stepper2.setMaxSpeed(speed);
  stepper2.setAcceleration(10000);
  stepper2.moveTo(11000);
  stepper2.run();
  int left = 11000 - sampleWidthSteps/2;
  int right = 11000 - sampleWidthSteps/2;
  


  for(int i = 0; i < 5; i++){
    stepper2.moveTo(left);
    stepper2.run();
    stepper2.moveTo(right);
    stepper2.run();
  }


}

However the following code does run, and I don't know why.

//accelstepper constant speed example has been altered and is being used
// not using default 4 wires setup, but instead using step, direction, and enable pins
// using TB6600 4A 9-42V stepper driver at 6400 pulses/rev (32 microsteps)

#include <AccelStepper.h>

// defines pins numbers
const int stepPin1 = 32;
const int directionPin1 = 34;
const int enablePin1 = 36;

const int stepPin2 = 22;
const int directionPin2 = 24;
const int enablePin2 = 26;


const int limitSwitch = 46;

// Define a stepper and the pins it will use
// 1 or AccelStepper::DRIVER means a stepper driver (with Step and Direction pins)
AccelStepper stepper1(AccelStepper::DRIVER, stepPin1, directionPin1);

AccelStepper stepper2(AccelStepper::DRIVER, stepPin2, directionPin2);
void setup()
{
  Serial.begin(19200);

  pinMode(limitSwitch, INPUT);


  // stepper1.setEnablePin(enablePin1);
  // stepper1.setPinsInverted(false, false, true);
  // stepper1.enableOutputs();
  // stepper1.setMaxSpeed(2150.0);
  // stepper1.setAcceleration(10000.0);
  // stepper1.setCurrentPosition(0);
  // stepper1.moveTo(5000);
    
  stepper2.setEnablePin(enablePin2);
  stepper2.setPinsInverted(false, false, true);
  stepper2.enableOutputs();
  stepper2.setMaxSpeed(18000.0);
  stepper2.setAcceleration(6000.0);
  stepper2.setCurrentPosition(0);
  stepper2.moveTo(11000);
}

void loop()
{

  

  stepper2.run();



}

It runs because You use AccelStepper the correct way.

Perhaps you expect stepper2.run() (run() reference) to work like Stepper2.runToPosition() (runToPosition() reference)?

This is it! Did not realize that the run() function needed to be called in a loop while runToPosition() function was blocking. Thanks!