Hi there,
I'm new to Arduino. This is the first time I'm working with it. I've learned a lot of this forum but now I'm stuck in my code
I've got 2 NEMA17 steppermotors with 2 drivers and 2 switches on a Arduino Mega. First thing they have to do is "homing" apart of each other. This works fine.
Then they need to move at the same time to 2 different steps. One at 415 and the other one at 830. When the first one has moved to 415 steps, he will wait until the other one is at step 830. This is what is want and they do this correctly.
After that they have to move at the same time to the "homingswitches". They do this, but both the motors will keep on moving until both switches are activated. This is not what i want. I want them to move at the same time to the switches and if one motor activates a switch, I want that motor to wait until the other motor activates the other switch. After that they can move at the same time again.
I have used this code so far:
#include "AccelStepper.h"
// AccelStepper Setup
// MOTOR A
AccelStepper stepperA(1, 2, 3); // 1 = Type Driver interface
// Pin 2 connected to STEP pin
// Pin 3 connected to DIR pin
// MOTOR B
AccelStepper stepperB(1, 12, 13); // 1 = Type Driver interface
// Pin 12 connected to STEP pin
// Pin 13 connected to DIR pin
// Define the Pins used
#define home_switchA 30 // Pin 30 connected to Home Switch (MicroSwitch)
#define home_switchB 35 // Pin 35 connected to Home Switch (MicroSwitch)
// Stepper Travel Variables
long initial_homing=-1; // Used to Home Stepper at startup
// --------------------------------------------------------------------------------------------------------
void setup() {
// Start Homing procedure of Stepper Motor "A" at startup
pinMode(home_switchA, INPUT_PULLUP);
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepperA.setMaxSpeed(160.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperA.setAcceleration(1000.0); // Set Acceleration of Stepper
while (digitalRead(home_switchA)) { // Make the Stepper move CCW until the switch is activated
stepperA.moveTo(initial_homing); // Set the position to move to
stepperA.moveTo(initial_homing++); // Decrease by 1 for next move if needed
stepperA.run(); // Start moving the stepper
}
stepperA.setCurrentPosition(0); // Set the current position as zero for now
stepperA.setMaxSpeed(160.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperA.setAcceleration(1000.0); // Set Acceleration of Stepper
stepperA.moveTo(initial_homing=1);
//--------------------------------------------------------------------------------------------------------
// Start Homing procedure of Stepper Motor "B" at startup
pinMode(home_switchB, INPUT_PULLUP);
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepperB.setMaxSpeed(160.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperB.setAcceleration(1000.0); // Set Acceleration of Stepper
while (digitalRead(home_switchB)) { // Make the Stepper move CCW until the switch is activated
stepperB.moveTo(initial_homing); // Set the position to move to
stepperB.moveTo(initial_homing++); // Decrease by 1 for next move if needed
stepperB.run(); // Start moving the stepper
}
stepperB.setCurrentPosition(0); // Set the current position as zero for now
stepperB.setMaxSpeed(160.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperB.setAcceleration(1000.0); // Set Acceleration of Stepper
stepperB.moveTo(initial_homing=1);
}
//--------------------------------------------------------------------------------------------------------
void loop() {
// (1)
stepperA.moveTo(-830);
stepperB.moveTo(-415);
while
(
stepperA.currentPosition() != -830 ||
stepperB.currentPosition() != -415
)
{
stepperA.run();
stepperB.run();
}
// (2) (HOMING)
while
(
digitalRead(home_switchA) ||
digitalRead(home_switchB)
)
{
stepperA.moveTo(initial_homing);
stepperA.moveTo(initial_homing++);
stepperA.run();
stepperB.moveTo(initial_homing);
stepperB.moveTo(initial_homing++);
stepperB.run();
}
stepperA.setCurrentPosition(0);
stepperA.moveTo(initial_homing=1);
stepperB.setCurrentPosition(0);
stepperB.moveTo(initial_homing=1);
}
I hope you'll understand what I'm meaning and I hope if you can help me.
I saw another topic on this forum that looks to have the same issue as mine: https://forum.arduino.cc/t/homing-two-stepper-motors-simultaneously-with-accelstepper/588107
There they assumed to change the code like this:
bool homingComplete = false;
bool xHome = false;
bool yHome = falsel
while(homingComplete == false) {
if (digitalRead(home_switch_stepperX) {
stepperX.moveTo(initial_homing_stepperX); // Set the position to move to
initial_homing_stepperX--;
stepperX.run();
}
else {
xHome = true;
}
// likewise for stepper Y
if (xHome == true and yHome == true) {
homingComplete = true;
}
}
I tried this, but I think I'm doing something wrong because it didn't work..