connect 2 stepp motor code in one part , helpp

hello, each code work fine separately but when i connect codes topgether they do not work as they should. did somebody have any suggestion how to connect together?
here is code:

#include <AccelStepper.h>

// Definicija motora i pinovi
AccelStepper stepper1(AccelStepper::FULL4WIRE, 8, 9, 10, 11);


#define home_switch 2 //pin 2 spojen na multiprekidač

// Stepper Travel Variables
long TravelX; 
long initial_homing=-1;  // Used to Home Stepper at startup


void setup()
{

           Serial.begin(11520);  // serial monitor
           pinMode(home_switch, INPUT_PULLUP);
           delay(5);  // cekanje da se modul probudi
           stepper1.setMaxSpeed(100.0);      // maximalna brzina motor1
           stepper1.setAcceleration(100.0);  // akceleracija motor1
           while (digitalRead(home_switch))
          {   
            stepper1.moveTo(initial_homing);  // Set the position to move to
            initial_homing--;  // Decrease by 1 for next move if needed
            stepper1.run();  // Start moving the stepper
            delay(5);
                     }
                       stepper1.setCurrentPosition(0);  // postavi trenutno poziciju na 0
                       stepper1.setMaxSpeed(100.0);     // max brzina
                       stepper1.setAcceleration(100.0);  // akceleracija
                       initial_homing=-1;
                       while (!digitalRead(home_switch)) // Make the Stepper move CW until the switch is deactivated
                     { 
            stepper1.moveTo(initial_homing);  
            stepper1.run();
            initial_homing++;
            delay(5);
         }
          stepper1.setCurrentPosition(0);
          stepper1.setMaxSpeed(180);     
          stepper1.setAcceleration(180);
          delay(1000);
 
 
}

void loop(){
    static int repeatCount = 0; // note static so the value is remembered
    static int stepperDestination = -153;
     
     
    if (repeatCount <= 5) {
        if (stepper1.distanceToGo() == 0) {
            delay(1000);
            
            if (stepperDestination == 0) {
            stepperDestination = -153;
            }
            else {
                stepperDestination = 0;
                
                
            }
            stepper1.moveTo(stepperDestination);
            repeatCount++;
        }
    }
    stepper1.run();
   
}

—>this is niidle arm connecting with steep motor 1

#include <AccelStepper.h>

// Definicija motora i pinovi
AccelStepper stepper2(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
#define home_switch 2  //pin 2 spojen na multiprekidač
#define senzor 3  //pin 3 spojen na senzor

long initial_homing = -1;
long trazi_senzor = -1;
int stepamount = -41;
int steps = stepamount;



void setup()
{

  Serial.begin(11520);  // serial monitor
  pinMode(senzor, INPUT_PULLUP);
  delay(5);  // cekanje da se modul probudi
  stepper2.setMaxSpeed(200.0);      // maximalna brzina motor1
  stepper2.setAcceleration(200.0);  // akceleracija motor1
  while (digitalRead(senzor))
  {
    stepper2.moveTo(trazi_senzor);  // Set the position to move to
    trazi_senzor--;  // Decrease by 1 for next move if needed
    stepper2.run();  // Start moving the stepper
    delay(5);
  }
  stepper2.setCurrentPosition(0);  // postavi trenutno poziciju na 0
  stepper2.setMaxSpeed(200.0);     // max brzina
  stepper2.setAcceleration(200.0);  // akceleracija
  trazi_senzor = -1;
  while (!digitalRead(senzor)) // Make the Stepper move CW until the switch is deactivated
  {
    stepper2.moveTo(trazi_senzor);
    stepper2.run();
    trazi_senzor++;
    delay(5);
  }
}


void loop() {
static int repeatCount = 0;
if (repeatCount <= 5) {
  stepper2.moveTo(stepamount);
  stepper2.run();

  if (stepper2.distanceToGo() == 0) {
    stepamount = stepamount + steps;
    repeatCount++;
    delay(1000);
    loop();

  }
}
}

—>belt driven by stepp motor 2

tnx

when i connect codes topgether they do not work as they should.

What should it do ?
What does it do ?

My guess is that at least part of the problem is the 1 second delays in the loops and the RECURSIVE CALL TO loop().

Change "loop();" to "return;" if you want to exit the current iteration of loop() and go on to the next iteration.

The homing would be simpler if you use the relative motion function (.move()) of the AccelStepper library.

void setup()
{
  Serial.begin(11520);  // serial monitor
  pinMode(home_switch, INPUT_PULLUP);
  pinMode(senzor, INPUT_PULLUP);
  HomeStepper1();
  stepper1.setCurrentPosition(0);
  stepper1.setMaxSpeed(180);
  stepper1.setAcceleration(180);

  HomeStepper2();
  stepper2.setCurrentPosition(0);  // postavi trenutno poziciju na 0
  stepper2.setMaxSpeed(200.0);     // max brzina
  stepper2.setAcceleration(200.0);  // akceleracija
}

void HomeStepper1() {
  stepper1.setMaxSpeed(100.0);      // maximalna brzina motor1
  stepper1.setAcceleration(100.0);  // akceleracija motor1

  while (digitalRead(home_switch)) {
    stepper1.move(-1);
    stepper1.runToPosition();
  }

  while (!digitalRead(home_switch)) {
    stepper1.move(1);
    stepper1.runToPosition();
  }

  while (digitalRead(home_switch)) {
    stepper1.move(-1);
    stepper1.runToPosition();
  }
}

void HomeStepper2() {
  stepper2.setMaxSpeed(100.0);      // maximalna brzina motor1
  stepper2.setAcceleration(100.0);  // akceleracija motor1

  while (digitalRead(trazi_senzor)) {
    stepper2.move(-1);
    stepper2.runToPosition();
  }

  while (!digitalRead(trazi_senzor)) {
    stepper2.move(1);
    stepper2.runToPosition();
  }

  while (digitalRead(trazi_senzor)) {
    stepper2.move(-1);
    stepper2.runToPosition();
  }
}

first code is used for arm with needle witch is conected with stepp motor. 1.step is initialzation home position, after needle pres microconttroler- home is completed- after this arm moving left and right 5 time( program in loop.

Second code is used for belt conected with stepp motor for transfer container to de phototransistor-laser sensor - after lase sensor detected container , belt need to move 5 time forward and stop after this..

here is 3d model of my project so you can easier understand what i do

24282253 10210873873547740 2130162572 n — ImgBB 3d model

marinac94:
hello, each code work fine separately

I have just had a quick look at the code in the second program in your Original Post and it has lots wrong with it.

You should not be using delay() when you are using stepper.run(). That function must be called as often as possible.

Outside of the setup() functon stepper.run() should be called from loop() and not from inside an IF statement.

You should never call the loop() function. That will eventually cause your program to crash. loop() gets called automatically - which is why it is called loop().

And if you want advice about why a combined program does not work you need to post the combined program code.

...R
Stepper Motor Basics
Simple Stepper Code

hello. this is a connected code together.

#include <AccelStepper.h>

// Definicija motora i pinovi
AccelStepper stepper1(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 8, 9, 10, 11);

#define home_switch 2     //pin 2 spojen na multiprekidač
#define senzor 3          //pin 3 spojen na senzor

long initial_homing = -1;
long trazi_senzor = -1;

void setup()
{

  Serial.begin(11520);  // serial monitor
  pinMode(home_switch, INPUT_PULLUP);
  pinMode(senzor, INPUT_PULLUP);

  HomeStepper1();
  stepper1.setCurrentPosition(0);
  stepper1.setMaxSpeed(160);// maximalna brzina motor1
  stepper1.setAcceleration(160); // akceleracija motor1

  HomeStepper2();
  stepper2.setCurrentPosition(0);  // postavi trenutno poziciju na 0
  stepper2.setMaxSpeed(110.0);     // max brzina
  stepper2.setAcceleration(110.0);  // akceleracija
  
}
void HomeStepper1() {
  stepper1.setMaxSpeed(4000);      // maximalna brzina motor1
  stepper1.setAcceleration(4000);  // akceleracija motor1

  while (digitalRead(home_switch)) {
    stepper1.move(-1);
    stepper1.runToPosition();
  }
  while (!digitalRead(home_switch)) {
    stepper1.move(1);
    stepper1.runToPosition();
  }
}
void HomeStepper2() {
  stepper2.setMaxSpeed(4000);      // maximalna brzina motor1
  stepper2.setAcceleration(4000);  // akceleracija motor1

  while (!digitalRead(senzor)) {
    stepper2.move(-1);
    stepper2.runToPosition();
  }
  while (digitalRead(senzor)) {
    stepper2.move(-1);
    stepper2.runToPosition();
  }
}

//work great

void loop() {
  static int repeatCount = 0;
  static int repeatCount1 = 0;
  static int stepperDestination = -153;
  static int stepamount = -40;
  static int steps = stepamount;

  if (repeatCount1 <= 20) {
    if (stepper1.distanceToGo() == 0)
    {
      if (stepperDestination == 0)
      {
        stepperDestination = -153;
      }
      else {
        stepperDestination = 0;
      }
      stepper1.moveTo(stepperDestination);
      repeatCount1++;
    }
  }
  stepper1.run();


  if (repeatCount <= 20)
  {
    stepper2.moveTo(stepamount);
    if (stepper2.distanceToGo() == 0)
    
    {
      stepamount = stepamount + steps;
      repeatCount++;
      
    }
  }
  stepper2.run();
}

first part - setup-----> works super.
second part ------> loop works ok but i need delay after each move- fe. 1. move needle arm left- delay (1000); move needle arm right delay(1000)
2. move belt 1 step forward delay (1000);

when i put delay in loop program after stepper2 motor repeat count needle arm and doing half steps+half step+half step (half+half-full step) belt work okay -doing 1 step forward and pause 1 second …
any suggestion

marinac94:
when i put delay in loop program after stepper2 motor repeat count

You need to post the code with the delay() in it so we can see exactly what you are doing.

And if you want a pause for one motor while the other is running then delay() is almost certainly wrong as it blocks everything. Have a look at how millis() is used to manage timing without blocking in Several things at a time.

...R

#include <AccelStepper.h>

// Definicija motora i pinovi
AccelStepper stepper1(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 8, 9, 10, 11);

#define home_switch 2     //pin 2 spojen na multiprekidač
#define senzor 3          //pin 3 spojen na senzor

long initial_homing = -1;
long trazi_senzor = -1;

void setup()
{

  Serial.begin(11520);  // serial monitor
  pinMode(home_switch, INPUT_PULLUP);
  pinMode(senzor, INPUT_PULLUP);

  HomeStepper1();
  stepper1.setCurrentPosition(0);
  stepper1.setMaxSpeed(160);// maximalna brzina motor1
  stepper1.setAcceleration(160); // akceleracija motor1

  HomeStepper2();
  stepper2.setCurrentPosition(0);  // postavi trenutno poziciju na 0
  stepper2.setMaxSpeed(110.0);     // max brzina
  stepper2.setAcceleration(110.0);  // akceleracija

}
void HomeStepper1() {
  stepper1.setMaxSpeed(4000);      // maximalna brzina motor1
  stepper1.setAcceleration(4000);  // akceleracija motor1

  while (digitalRead(home_switch)) {
    stepper1.move(-1);
    stepper1.runToPosition();
  }
  while (!digitalRead(home_switch)) {
    stepper1.move(1);
    stepper1.runToPosition();
  }
}
void HomeStepper2() {
  stepper2.setMaxSpeed(4000);      // maximalna brzina motor1
  stepper2.setAcceleration(4000);  // akceleracija motor1

  while (!digitalRead(senzor)) {
    stepper2.move(-1);
    stepper2.runToPosition();
  }
  while (digitalRead(senzor)) {
    stepper2.move(-1);
    stepper2.runToPosition();
  }
}

//work great

void loop()
{
  static int repeatCount = 0;
  static int repeatCount1 = 0;
  static int stepperDestination = -153;
  static int stepamount = -40;
  static int steps = stepamount;

  if (repeatCount1 <= 20) {
    if (stepper1.distanceToGo() == 0)
    
    {
      if (stepperDestination == 0)
      {
        stepperDestination = -153;
      }
      else {
        stepperDestination = 0;
      }
      stepper1.moveTo(stepperDestination);
      repeatCount1++;
      delay(1500);
    }
    stepper1.run();
  }


  if (repeatCount <= 20)
  {
    stepper2.moveTo(stepamount);
    if (stepper2.distanceToGo() == 0)

    {
      stepamount = stepamount + steps;
      repeatCount++;
      delay(1500);
    }
  }
  stepper2.run();
}

here is code. i want to both motor have delay after wach step, so i can then
synchronize to do pause in same time. because needle arm grab seed with air when she go left, and drop seed when she go right, when she drop seed container need to be in dalay , if you undarstend … here is youtube video - i create same project with arduino. —> diy needle seeder - YouTube

marinac94:
here is code. i want to both motor have delay after wach step, so i can then
synchronize to do pause in same time. because needle arm grab seed with air when she go left, and drop seed when she go right, when she drop seed container need to be in dalay , if you undarstend .. here is youtube video - i create same project with arduino. ---> diy needle seeder - YouTube

Do you ever read back your text after you write it and ask yourself if someone reading it will understand it easily, or if it might be improved to make the meaning clearer?

I don't know where you live but in Britain it is Winter and I don't expect gardeners to be "grabbing seed" until Spring. Even then I don't know what "grab seed with air" means.

If you are using two separate delay()s how do you hope to synchronize the motors?

...R

robin, im so sory for my bad english, i will try describe my problem in more detail.

I'm working on automatic planter for Styrofoam containers. Automatic planter have 3 part.

  1. Automatic belt wich i control with 1 steep motor-nema 17. Automatic belt moves to the sensor, after sensor detect styrofoam containers, the belt must stop- this is first position for belt, after this position belt has 20 steps ahead with a break between each step... container have 19x11 holls for the seeds.

2.Automatic arm with needles- was created by closed tube on one side and on the another side
opened for air connection-vacum . tube have 11 needles glued to the length which are used to capture the seeds by air vacum. This arm is controled by steep motor. This arm have 3 task..

  1. found home position- when arm touch microcontroler, thet's for her home position (0)
    2 Arm must go left to the pot with the seed, after she reaches the position of the pot, arm must have a break 1.5 second because seeds can be received od needle by air (vacum)
  2. arm must go right to home position or 0 and drop this seedes in container -also needs to pause for 1.5 second that needles can release the seeds.

you can watch the video in last post for appearance and work of this Automatic planter maschine.

my problem is how to paused 1.5 second automatic arm and belt after each step?
when i try codes separetely, codes works super, problem is when i connected code together.
the code should be work following stepps.

  1. belt 1 step forward.
  2. belt delay 1.5 second
  3. arm go left do the pot.
  4. arm delay1.5 second
  5. arm go right to the home (0)
  6. arm delay 1.5 second

but the code works next.

  1. belt 1 step forward.
  2. belt delay 1.5 second
  3. arm go left- stop in the midle delay 1.5 second
  4. belt go 1 step forward
  5. belt delay 1.5 second
  6. arm go left to the end- pot- delay 1.5 second
  7. belt go 1 step forward
  8. belt delay 1.5 second
  9. arm go right to the homo- stop in the middle- delay 1.5 second
  10. belt go 1 step forward
  11. belt delay 1.5 second
  12. arm go right to the homo- finnish step to the end. delay 1.5 second

Im hope you undarstand now much clearer, and thank you for your patience.

Thanks. That's a big help. I had not realized you were actually working with plants and seeds. I thought "grab seed with air" was a typo.

Anyway, it looks like a clever device.

The sort of process you are trying to control is easiest with what is rather grandly referred to as a State Machine. It's really just a fancy name for having some variables that keep track of what is happening.

Using your short list as a guide you might have a variable (let's call it systemState) that can have the values 'B' (belt moving); 'L' (arm Left); 'H' (arm Home).

As well as that have a separate function for each action. Those functions should include the timing (using millis() and NOT delay()) for each action. When each function is complete it will change the value of systemState for the next action.

The code in loop() might be like this

void loop() {
  if (systemState == 'B') {
    moveBelt();
  }
  else if (systemState == 'L') {
    moveArmLeft();
  }
  else if (systemState == 'H') {
    moveArmHome();
  }
}

and the code in the moveArmLeft() function might be something like (pseudo code)

void moveArmleft() {
  if I have waited long enough {
    move the arm
  }
  if the arm has moved and I have waited another little bit {
    systemState = 'H'
 }
}

...R

thank you for answer , so do i have to change full code ? or only loop because, code in setup is fine and work fine, how to create variable systemstate ?

if I get it well ,for every movement i need give a time at the beginning of the program ?*

and how to solve repeat count ? same like now or have another catch ?

marinac94:
thank you for answer , so do i have to change full code ? or only loop because, code in setup is fine and work fine, how to create variable systemstate ?

if I get it well ,for every movement i need give a time at the beginning of the program ?*

and how to solve repeat count ? same like now or have another catch ?

I think you need to absorb the implications of the approach that I suggested. I suspect it is very different from the method that was in your mind when you wrote your code.

Maybe write a short learning program using that style.

When you grasp the State Machine concept you will know what changes you need to make. They will probably be large. And I think you will also see that the questions in your last paragraph just goes away.

how to create variable systemstate ?

The usual way

char systemstate;

f I get it well ,for every movement i need give a time at the beginning of the program ?*

You will need to establish the time (the value of millis() ) at the start of every section where you need timing. Or it might be easier to save the time as the last thing in the preceding section. I don't know what you had in mind when you thought there would be a need to give times at the beginning of the program. Think about it this way ... if you are going to cook a chicken for dinner you don't need to write down the time at 8am. You just make a note of the time when you put the chicken in the oven - perhaps at 5pm. Then you can work out when it has been in the oven long enough.

...R