Only 3 things at same time in loop

Hello,
I got a code to run 14 StepperMotors via AccelStepper libary.
But I want just to run 3 StepperMotors at the same time.
If 4 (ore more) StepperMotor should be used, then the 4th should wait until one of the other 3 has finished.
Means: Start a StepperMotor when a “free slot” is available.
How can i do this?

Here is my Code:

#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>
#include <AccelStepper.h>

#define VALUE_OFF 0x0
#define VALUE_ON 0x1
#define COMMAND_DRINK 0x2
#define COMMAND_LED 0x3

AndroidAccessory acc("Manufacturer",
                     "Model",
                     "Description",
                     "1.0",
                     "http://nichtsda.com",
                     "0000000012345678");


//pin definitions.  must be PWM-capable pins!
const int redPin = 2;
const int greenPin = 3;
const int bluePin = 4;


AccelStepper stepper1   (AccelStepper::DRIVER, 23, 12);
AccelStepper stepper2   (AccelStepper::DRIVER, 27, 11);
AccelStepper stepper3   (AccelStepper::DRIVER, 31, 11);
AccelStepper stepper4   (AccelStepper::DRIVER, 35, 11);
AccelStepper stepper5   (AccelStepper::DRIVER, 39, 11);
AccelStepper stepper6   (AccelStepper::DRIVER, 43, 11);
AccelStepper stepper7   (AccelStepper::DRIVER, 47, 11);
AccelStepper stepper8   (AccelStepper::DRIVER, 51, 11);
AccelStepper stepper9   (AccelStepper::DRIVER, 24, 11);
AccelStepper stepper10  (AccelStepper::DRIVER, 28, 11);
AccelStepper stepper11  (AccelStepper::DRIVER, 32, 11);
AccelStepper stepper12  (AccelStepper::DRIVER, 36, 11);
AccelStepper stepper13  (AccelStepper::DRIVER, 40, 11);
AccelStepper stepper14  (AccelStepper::DRIVER, 44, 11);



//int stepPin[16]={23,27,31,35,39,43,47,51,24,28,32,36,40,44,48,52};
int multiplikator = 60000;
int MengeZutat[16];

//maximum duty cycle to be used on each led for color balancing.
//if "white" (R=255, G=255, B=255) doesn't look white, reduce the red, green, or blue max value.
const int max_red = 255;
const int max_green = 255;
const int max_blue = 200;

byte colors[3] = {0, 0, 0}; //array to store led brightness values
byte rcvmsg[16];








void setup()
{

  //set all three of our led pins to output
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(115200);
  acc.powerOn();


  stepper1.setMaxSpeed(1800.0);
  stepper1.setAcceleration(1000.0);

  stepper2.setMaxSpeed(1100.0);
  stepper2.setAcceleration(1000.0);

  stepper3.setMaxSpeed(1800.0);
  stepper3.setAcceleration(1000.0);


  stepper4.setMaxSpeed(1800.0);
  stepper4.setAcceleration(1000.0);

  stepper5.setMaxSpeed(1800.0);
  stepper5.setAcceleration(1000.0);

  stepper6.setMaxSpeed(1800.0);
  stepper6.setAcceleration(1000.0);

  stepper7.setMaxSpeed(1800.0);
  stepper7.setAcceleration(1000.0);

  stepper8.setMaxSpeed(1800.0);
  stepper8.setAcceleration(1000.0);

  stepper9.setMaxSpeed(1800.0);
  stepper9.setAcceleration(1000.0);

  stepper10.setMaxSpeed(1800.0);
  stepper10.setAcceleration(1000.0);

  stepper11.setMaxSpeed(1800.0);
  stepper11.setAcceleration(1000.0);

  stepper12.setMaxSpeed(1800.0);
  stepper12.setAcceleration(1000.0);

  stepper13.setMaxSpeed(1800.0);
  stepper13.setAcceleration(1000.0);

  stepper14.setMaxSpeed(1800.0);
  stepper14.setAcceleration(1000.0);

}



void loop()
{

  if (acc.isConnected()) {
    acc.read(rcvmsg, sizeof(rcvmsg), 1);






    if (rcvmsg[14] == COMMAND_LED ) {
      byte value = rcvmsg[15];
      if (value == VALUE_ON) {
        colors[0] = rcvmsg[0] + 128;
        colors[1] = rcvmsg[1] + 128;
        colors[2] = rcvmsg[2] + 128;
        analogWrite(redPin, map(colors[0], 0, 255, 0, max_red));
        analogWrite(greenPin, map(colors[1], 0, 255, 0, max_green));
        analogWrite(bluePin, map(colors[2], 0, 255, 0, max_blue));
      }
      else if (value = VALUE_OFF) {
        colors[0] = 0;
        colors[1] = 0;
        colors[2] = 0;
        analogWrite(redPin, map(colors[0], 0, 255, 0, max_red));
        analogWrite(greenPin, map(colors[1], 0, 255, 0, max_green));
        analogWrite(bluePin, map(colors[2], 0, 255, 0, max_blue));
      }
    }



    if (rcvmsg[14] == COMMAND_DRINK ) {
      byte value = rcvmsg[15];
      if (value == VALUE_ON) {

        //int sleepPin[16]={25,29,33,37,41,45,49,53,22,26,30,34,38,42,46,50};


        stepper1.move(rcvmsg[0]*multiplikator);
        stepper1.setEnablePin(25);

        stepper2.move(rcvmsg[1]*multiplikator);
        stepper2.setEnablePin(29);

        stepper3.move(rcvmsg[2]*multiplikator);
        stepper3.setEnablePin(33);

        stepper4.move(rcvmsg[3]*multiplikator);
        stepper4.setEnablePin(25);

        stepper5.move(rcvmsg[4]*multiplikator);
        stepper5.setEnablePin(37);

        stepper6.move(rcvmsg[5]*multiplikator);
        stepper6.setEnablePin(41);

        stepper7.move(rcvmsg[6]*multiplikator);
        stepper7.setEnablePin(45);

        stepper8.move(rcvmsg[7]*multiplikator);
        stepper8.setEnablePin(49);

        stepper9.move(rcvmsg[8]*multiplikator);
        stepper9.setEnablePin(53);

        stepper10.move(rcvmsg[9]*multiplikator);
        stepper10.setEnablePin(22);

        stepper11.move(rcvmsg[10]*multiplikator);
        stepper11.setEnablePin(26);

        stepper12.move(rcvmsg[11]*multiplikator);
        stepper12.setEnablePin(30);

        stepper13.move(rcvmsg[12]*multiplikator);
        stepper13.setEnablePin(34);

        stepper14.move(rcvmsg[13]*multiplikator);
        stepper14.setEnablePin(38);

      }
      else if (value == VALUE_OFF ) {



        stepper1.run();
        if (stepper1.isRunning() == false) {
          stepper1.disableOutputs();
        }

        stepper2.run();
        if (stepper2.isRunning() == false) {
          stepper2.disableOutputs();
        }


        stepper3.run();
        if (stepper3.isRunning() == false) {
          stepper3.disableOutputs();
        }

        stepper4.run();
        if (stepper4.isRunning() == false) {
          stepper4.disableOutputs();
        }

        stepper5.run();
        if (stepper5.isRunning() == false) {
          stepper5.disableOutputs();
        }

        stepper6.run();
        if (stepper6.isRunning() == false) {
          stepper6.disableOutputs();
        }

        stepper7.run();
        if (stepper7.isRunning() == false) {
          stepper7.disableOutputs();
        }

        stepper8.run();
        if (stepper8.isRunning() == false) {
          stepper8.disableOutputs();
        }


        stepper9.run();
        if (stepper9.isRunning() == false) {
          stepper9.disableOutputs();
        }

        stepper10.run();
        if (stepper10.isRunning() == false) {
          stepper10.disableOutputs();
        }

        stepper11.run();
        if (stepper11.isRunning() == false) {
          stepper11.disableOutputs();
        }

        stepper12.run();
        if (stepper12.isRunning() == false) {
          stepper12.disableOutputs();
        }


        stepper13.run();
        if (stepper13.isRunning() == false) {
          stepper13.disableOutputs();
        }

        stepper14.run();
        if (stepper14.isRunning() == false) {
          stepper14.disableOutputs();
        }




      }
    }



  }
}

Life would be very much easier if you create an array of stepper objects because then you could control which ones are working by changing the array indices. You could then have code like

for (byte n = 0; n < 14; n++) {
  stepper[n].setMaxSpeed(1800.0);
  stepper[n].setAcceleration(1000.0);
}

Assuming you are using the non-blocking stepper.run() command your code will need to keep checking if the running motors have reached their destination. When a motor is found to have reached its destination you can decrement the count of running motors. When the count is below 3 you can cause another motor to run and increment the count of running motors.

You have not said how you propose to choose which of the 12 non-running motors should be called next.

…R

Replacing by Arrays I like at the end, when the code is finish.

My Problem is where to creat this count?
My trys always increase ulimited in the void loop().
How can I tell them to only increase once?

DtotheZ:
Replacing by Arrays I like at the end, when the code is finish.

That sort of thing is very much easier to do at the start of the programming process.

My Problem is where to creat this count?

When you instruct a motor to move you should increment the count.
if the count is already at 3 you should not instruct another motor to move.

If that does not answer your question it is because I did not understand what you want advice about.

I am assuming that you instruct a motor to move by calling the moveTo() function for that motor.

...R

My problem is that I can not find a place where excatly to count.

I have now created a counter and set this to 0;
When I let the Code run, my counter rises immediately to maximum.
The function “counter++” is only in the stepper1 part.

I hope you see now my problem

Image from Reply #4 so we don’t have to download it. See Image Guide

190ed93dce181c59e799a0dad213b755879e22bf.png

…R

I am not going to look at pictures of program code or program output.

Post it as text.

...R

My problem is that I can not find a place where excatly to count.

I have now created a counter and set this to 0;
When I let the Code run, my counter rises immediately to maximum.
The function “counter++” is only in the stepper1 part.

The Code is changed to:

else if (value == VALUE_OFF ) {


if( counter<4) {
   stepper1.run();
   if(stepper1.isRunning()==true){
   counter++;
   }
else if (stepper1.isRunning() == false) {
    stepper1..disableOutputs();
    counter--;
   }
...
...
...

Still won’t work…

DtotheZ:
The Code is changed to:

Please post a complete program that compiles correctly.

…R