Led control dont work right after activate an usermod

hi, i wrote an usermod with the v2 template. but afte i flash the file, the leds working interupted and i get every second/third click at the color change at the UI an error to connect.

can somebody see in my code why?

its an simple code to drive an stepper motor. the motor works correct…only the leds not.

thank you


#pragma once

#include "wled.h"

#include <AccelStepper.h>

const int stepsPerRevolution = 64;  // change this to fit the number of steps per revolution

// ULN2003 Motor Driver Pins

#define IN1 19

#define IN2 18

#define IN3 5

#define IN4 17

// initialize the stepper library

AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);

//This is an empty v2 usermod template. Please see the file usermod_v2_example.h in the EXAMPLE_v2 usermod folder for documentation on the functions you can use!

class speedstep : public Usermod {

  private:

   

  public:

    void setup()

 {

 // Change these to suit your stepper if you want

 stepper.setMaxSpeed(150);

  stepper.setAcceleration(100);

}

    void loop() {

      stepper.moveTo(500);

  while (stepper.currentPosition() != 300) // Full speed up to 300

    stepper.run();

  stepper.stop(); // Stop as fast as possible: sets new target

  stepper.runToPosition();

  // Now stopped after quickstop

  // Now go backwards

  stepper.moveTo(-500);

  while (stepper.currentPosition() != 0) // Full speed basck to 0

    stepper.run();

  stepper.stop(); // Stop as fast as possible: sets new target

  stepper.runToPosition();

  // Now stopped after quickstop

    }

};

I have extremely limited experience creating user mods, so take all this with a grain of salt… but is it possible that this is due to the “while” loops? My thinking is that, while in the while loop, the controller isn’t able to think about anything other than resolving the while loop. It might be worth trying to achieve this with “if” statements instead of “while” i.e.
if (stepper.currentPosition() = 0) // Full speed back to 0

Just a thought, eager to hear everyone else’s! Good luck, keep us posted!

Yep. You need to yield so wled can run.

Exactly what @tonyno says!
yield() in every loop.