Stepper Motors wont change direction while already turning

I am trying to make something where I need to change the direction of a stepper motor with an IR Remote. The problem is when the motors spin, they can't change directions. Would someone please tell me what is wrong with my code?

// AccelStepper - Version: Latest 
#include <AccelStepper.h>
#include <MultiStepper.h>

#include <Stepper.h>
#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
#define T2 16718055 //2 Button
#define T4 16716015 //4 Button 
#define T6 16734885 //6 Button
#define T8 16730805 //8 Button 
#define T10 16726215 //5 Button 
const int stepsPerRevolution = 64;  
                                         

// initialize the stepper library for both steppers:

Stepper small_stepper2(stepsPerRevolution, 8,10,9,11); 
Stepper small_stepper1(stepsPerRevolution, 4,5,6,7);

 
void setup() {


  

  small_stepper2.setSpeed(400);    // speed of 1. motor

  small_stepper1.setSpeed(400);   // speed of 2. motor
   
   Serial.begin(9600);

 irrecv.enableIRIn(); 

}

void loop() {
   if (irrecv.decode(&results)) 
  {
  
   while (results.value == T2)
    { small_stepper1.step(1);
      small_stepper2.step(1);
      } 

   while (results.value == T4) 
    { small_stepper1.step(-1);
      small_stepper2.step(1);
      } 
      
   while (results.value == T6) 
    { small_stepper1.step(1);
      small_stepper2.step(-1);
      } 
      
   while (results.value == T8) 
    { small_stepper1.step(-1);
      small_stepper2.step(-1);
      } 
      
    while (results.value == T10) 
    { small_stepper1.step(0);
      small_stepper2.step(0);
      } 
      

 }

 
 }

How do you expect to break out of your while loops?

I thought the different inputs in the remote would stop the loop.

But you don't read the new inputs in your while loops.

So how would I do that?

Here's a hint:

Do not use "while" loops in code. :worried:

Do not use "delay()" either. :roll_eyes:

Could you tell me what I should use instead as opposed to eyeroll emojis?

Have you thought if swapping the "while"s for "if"s, and rearranging the code a little, so that loop() does the looping?

OK, AWOL got to it first.

It's a matter of code design. You need to understand the overall loop() concept. The loop must keep rolling.

On each pass through the loop(). you need to see if you have received a new instruction. If not, you select - based on the most recent instruction - what single step you need to perform and then pass through to the end of the loop. Nothing you do must require waiting for anything to happen.

I just tried changing the "while"s to "if"s, but nothing changed. Where would you suggest moving the "loop()" to?

We can't see your code.

Its the same code as originally posted, but with the "while"s changed to "if"s

But that will only step once, won't it?

That's why

Actually means rearranging the code completely. :rofl:

Unfortunate but true.

No, it wont. The results haven't changed.

Have you missed something with your IR handling,?

No, each button works whenever I reset the code. Its definitely an issue with breaking the loops.

You can explain how instead of sending laughing emojis and being vague.

Not vague at all. Read #9.

1 Like

In addition to all the tips from the other posters:

It doesn't make sense to include a lot of library .h files if you don't use them. I think for your sketch only Stepper.h and IRremote.h is needed.

And you forgot the

        IrReceiver.resume();

after receiving and evaluating a code, to enable receiving the next code.