Skip to main content
2 of 5
corrected spelling
jsotola
  • 1.6k
  • 2
  • 13
  • 22

Running code inside void loop() vs inside a separate function with an infinite loop in it

I'm writing a program for running a stepper motor at a high RPM using accelStepper on a NodeMCU esp8266. Super New to the accelStepper but reading, experimenting and learning as I go

When the code to cycle the stepper one way and then the other is inside the void loop() it works as expected, but there's a bunch of other inputs I need to check in there for setting up the system and when they are all inside the void loop() they slow down the cycle noticeably as it does various digital and analog reads.

While the stepper is cycling I only really need to watch one e-stop input instead of everything so I figured to create a separate function with an infinite loop in it, call the stepper code there and just check the one input and return out of the function when done

Testing this and it partially works but the stepper only makes it part way to its end point and then abruptly stops (instead of decelerating) and then reverse and same thing.

Probably not even the right way of trying to solve my issue but it's what I came up with tonight. Any help would be much appreciated.

Here's the stripped down test code. Endpoint is 50000

 void loop()
{
  /*  if this code runs it works perfectly
  stepper.run(); 
  if (stepper.currentPosition()==EndPoint)
    {
    NewEndPoint=0;
    stepper.moveTo(NewEndPoint);
    }
  else if (stepper.currentPosition()==0)
    {
    NewEndPoint=EndPoint;
    stepper.moveTo(NewEndPoint);
    }
    */

  go_to();
  
}


void go_to()
{
  for(;;) // have tried while(true) also, same results
  {
  stepper.run();
  if (stepper.currentPosition()==EndPoint)
    {
    NewEndPoint=0;
    stepper.moveTo(NewEndPoint);
    }
  else if (stepper.currentPosition()==0)
    {
    NewEndPoint=EndPoint;
    stepper.moveTo(NewEndPoint);
    }
  }
}
Bart
  • 1
  • 1