Multisteps loop help needed

Hello everyone!
I'm trying to create two stepper motors program using Accelstepper.

I have:
Arduino R3, two ULN2003 drivers, two 28byj-48 , motors + 5V power supply. IDE 2.3.2

Goal: to program both motors to turn at the same time , executing 10 consecutive steps, to a different value each time, returning to 0 after each step. 2 seconds delay between steps. Loop the whole program.
Where I am right now: Motors do execute first step only. Every attempt to use delay between steps or add more steps blocks the code. Not sure what me and my very basic knowledge do wrong but if you can lead me to a topic explaining consecutive steps and loops would be great. I already read one about loop vs millis and watched many examples but most of them show one step or one motor only and when I start adding steps/motors it gets confusing and doesn't work.

  Stepper Motor Demonstration 2
  Stepper-Demo2.ino
  Demonstrates Two 28YBJ-48 Unipolar Steppers with ULN2003 Driver
  Uses Accelstepper Library

  
*/

// Include the AccelStepper Library
#include <AccelStepper.h>

// Define step constants
#define FULLSTEP 4
#define FULLSTEP 8

// Define Motor Pins (2 Motors used)

#define motorPin1 8   // Blue   - 28BYJ48 pin 1
#define motorPin2 9   // Pink   - 28BYJ48 pin 2
#define motorPin3 10  // Yellow - 28BYJ48 pin 3
#define motorPin4 11  // Orange - 28BYJ48 pin 4


#define motorPin5 4  // Blue   - 28BYJ48 pin 1
#define motorPin6 5  // Pink   - 28BYJ48 pin 2
#define motorPin7 6  // Yellow - 28BYJ48 pin 3
#define motorPin8 7  // Orange - 28BYJ48 pin 4

AccelStepper stepper1(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(FULLSTEP, motorPin5, motorPin7, motorPin6, motorPin8);

void setup() {
  // 1 step Motor 1 
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(50.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(1000);

  // 1 step Motor 2 
  stepper2.setMaxSpeed(1000.0);
  stepper2.setAcceleration(50.0);
  stepper2.setSpeed(200);
  stepper2.moveTo(1000);
  

    }

    void loop() {
      //Change direction at the limits
      if (stepper1.distanceToGo() == 0)
        stepper1.moveTo(-stepper1.currentPosition());
      if (stepper2.distanceToGo() == 0)
        stepper2.moveTo(-stepper2.currentPosition());
      

      stepper1.run();
      stepper2.run();
    }

what is "value" ? two identical motors rotating 10 steps in identical way. or you mean by "steps" something different as one single move of stepper motor?

#include <AccelStepper.h>

// Define Motor Pins (2 Motors used)
#define motorPin1 8   // Blue   - 28BYJ48 pin 1
#define motorPin2 9   // Pink   - 28BYJ48 pin 2
#define motorPin3 10  // Yellow - 28BYJ48 pin 3
#define motorPin4 11  // Orange - 28BYJ48 pin 4

#define motorPin5 4  // Blue   - 28BYJ48 pin 1
#define motorPin6 5  // Pink   - 28BYJ48 pin 2
#define motorPin7 6  // Yellow - 28BYJ48 pin 3
#define motorPin8 7  // Orange - 28BYJ48 pin 4

AccelStepper M1(4, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper M2(8, motorPin5, motorPin7, motorPin6, motorPin8);

void setup() {
  // 1 step Motor 1
  M1.setMaxSpeed(1000.0);
  M1.setAcceleration(50.0);
  M1.setSpeed(200);

  // 1 step Motor 2
  M2.setMaxSpeed(1000.0);
  M2.setAcceleration(50.0);
  M2.setSpeed(200);

  M1.setCurrentPosition(-500);
  M2.setCurrentPosition(-500);
}

void loop() {
  //Change direction at the limits
  if (M1.distanceToGo() == 0)M1.moveTo(-M1.currentPosition() * 2);
  if (M2.distanceToGo() == 0)M2.moveTo(-M2.currentPosition() * 2);

  M1.run();
  M2.run();
}
2 Likes

Yes, I agree. Here is an example you can experiment with using an "array" of motors. See if you can study this and understand how each separate motor is addressed and moved. threewipermotors - Wokwi ESP32, STM32, Arduino Simulator

And here is an example of keeping track of how much the motor moved, then playing it backwards... stepperRandomRotateReturn - Wokwi ESP32, STM32, Arduino Simulator

1 Like