Running 2 steppers for robot

Hello,
I am just starting a maybe over ambition project using Arduino and steppers motors to create a robot for some cleaning tasks. The setup below is to push objects using a rotating hand on a slide. I named it "god's hand" with all modesty. I am struggling for weeks now trying to get it working as expected. Chat-GPT was pretty useless to be frank. Is there here a kind person to help me, please ?

Here the setup (test bench for now)


Here my project description:
WRITE FULL OPTIMIZED CODE FOR FOLLOWING INSTRUCTIONS.

A CNC Shield is mounted on a Arduino One.

a stepper motor named M1 is connected to the CNC Shield to a DRV8825 driver on module C1 using STEP d2 and DIR 5.

a stepper motor named M2 is connected to the CNC Shield to a DRV8825 driver on module C2 using STEP d3 and DIR 6.

A limit switch name L1 is connected to the CNC Shield on pin 9.

A limit switch name L2 is connected to the CNC Shield on pin 10.

A limit switch name L3 is connected to the CNC Shield on pin 11. L3 is used to count that 1 revolution of M2 is done.

Stepper Power : 24V , 2A

M2 can only turn CLOCKWISE.

ACCELSTEPPER library for all rotations.

Initialization:

If L1 is LOW, run M1 for - 50 steps. Set Home Position

if L2 is LOW, initiate HOMING function

IF L1 and L2 are LOW, initiate HOMING function.

HOMING:

Run M1 and M2.

If L1 is HIGH then STOP M1 and Run M1 for -50 Steps. Set M1 Home Position.

If L3 is HIGH then STOP M2 and set M2 Home Position.

MAIN SEQUENCE:

  1. Run M1.

  2. When M1 DISTANCE = 800 then run M1 reverse direction to Home Position and run M2 for 100 steps.

  3. Run M2 until L3 is LOW
    When M1 is at M1 Home Position. Loop to 1.

If L1 or L2 are LOW during MAIN SEQUENCE. STOP M1 and M2.

Standard advice:

Ask the robot again until he/she/it has provided the correct result expected by you.

It could be 42.

1 Like

Hi Newbie welcome to the community

Kindly go through it
Guidelines

Kindly provide a code and proper circuit diagram it will be helpful

2 Likes

This (above) will be Main Sequence Step 1, before "Run M1."

looks like I forgot the code:

// GOD OF MOUTH

#include <AccelStepper.h>

// Define stepper motors and limit switches
AccelStepper stepperM1(1, 2, 5); // (driver, STEP, DIR)
AccelStepper stepperM2(1, 3, 6);
const int L1 = 9;
const int L2 = 10;
const int L3 = 11;

void setup() {
  // Set initial speed and acceleration for both motors
  stepperM1.setMaxSpeed(1000);
  stepperM1.setAcceleration(100);
  stepperM2.setMaxSpeed(1000);
  stepperM2.setAcceleration(100);

  // Check limit switches for initialization
  if (digitalRead(L1) == HIGH) {
    stepperM1.moveTo(-50);
    while (stepperM1.distanceToGo() != 0) {
      stepperM1.run();
    }
    stepperM1.setCurrentPosition(0);

  } else if (digitalRead(L2) == HIGH || (digitalRead(L1) == LOW && digitalRead(L2) == LOW)) {
    // Homing function

    stepperM1.moveTo(-2000); // Move M1 towards L1 and L2
    stepperM2.moveTo(-2000);

    while (digitalRead(L1) == LOW || digitalRead(L2) == LOW) {
      stepperM1.run();
      stepperM2.run();
    }
    if (digitalRead(L1) == HIGH) {
      stepperM1.moveTo(-50);
      while (stepperM1.distanceToGo() != 0) {
        stepperM1.run();
      }
      stepperM1.setCurrentPosition(0);
    }
    if (digitalRead(L3) == HIGH) {
      stepperM2.setCurrentPosition(0);
    }
    stepperM1.setCurrentPosition(0);
    stepperM2.setCurrentPosition(0);
  }
}

void loop() {
  // Main sequence
  stepperM1.run();

  if (stepperM1.currentPosition() == 800) {
    stepperM1.moveTo(-800);
    stepperM2.moveTo(100);
  }

  if (stepperM2.distanceToGo() != 0) {
    stepperM2.run();
  } else {
    if (digitalRead(L3) == LOW) {
      stepperM2.moveTo(-50);
    } else {
      stepperM2.setCurrentPosition(0);
    }
  }

  // Check for limit switch during main sequence
  if (digitalRead(L1) == HIGH || digitalRead(L2) == HIGH) {
    stepperM1.stop();
    stepperM2.stop();
  }
}

Very generalised question. Very generalised answer: "yes"

Sure the main purpose of this forum is to help.
Though you should ask a specific question.
I am sure that you can ask specific questions. But you can not expect that somebody reads your mind or will post a "cover 1000 possabilities-tutorial" for you.

So what are your questions?

best regards Stefan

I put your sketch into a crude simulation. I have not added any hardware logic gates, but maybe only software is needed.

Thanks a lot xfpd !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.