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);
}
}
}
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.