Hallo zusammen,
Ich habe ein Arduino CNC Shield mit a4988 Schrittmotortreiber und 28byj-48 Motor, möchte diesen auf einen Endstop fahren und dann je nach Eingangssignal 2 verschiedene Positionen anfahren. Aber der Stepper fährt nur in eine Richtung, mit der korrekten Geschwindigkeit. Sobald er die Richtung wechselt fährt er nur noch extrem langsam. Ich denke es liegt am Code oder an der Library da der Stepper mit GRBL oder auch mit accelstepper, wenn ich die Pin Reihenfolge ändere die korrekte Geschwindigkeit hat. Kann mir jemand helfen?
#include <AccelStepper.h>
#define MOTOR_X_ENABLE_PIN 8
#define MOTOR_X_STEP_PIN 3
#define MOTOR_X_DIR_PIN 6
AccelStepper motor_X(1, MOTOR_X_STEP_PIN, MOTOR_X_DIR_PIN);
#define home_switch 11 // Pin 9 connected to Home Switch (MicroSwitch)
long initial_homing = -1; // Used to Home Stepper at startup
// Stepper Travel Variables
long TravelX; // Used to store the X value entered in the Serial Monitor
int move_finished = 1; // Used to check if move is completed
int set_pin; //mount or dismount spindle
int spindle_mounted = 12; //check if spindle mounted
int homed = 0; //use home
void setup()
{
Serial.begin(115200); // Start the Serial monitor with speed of 9600 Bauds
pinMode(set_pin, INPUT);
pinMode(spindle_mounted, INPUT);
pinMode(home_switch, INPUT_PULLUP);
motor_X.setEnablePin(MOTOR_X_ENABLE_PIN);
motor_X.setPinsInverted(false, false, true);
motor_X.setAcceleration(300);
motor_X.setMaxSpeed(800);
motor_X.enableOutputs();
if (homed == 0) {
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(home_switch)) { // Do this until the switch is activated
motor_X.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
motor_X.run();
Serial.print("Speed: ");
Serial.println(motor_X.speed());
}
motor_X.setCurrentPosition(0);
motor_X.setAcceleration(300);
motor_X.setMaxSpeed(800);
initial_homing = 1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
motor_X.moveTo(initial_homing);
motor_X.run();
initial_homing++;
delay(5);
Serial.print("Speed: ");
Serial.println(motor_X.speed());
}
motor_X.setCurrentPosition(0);
motor_X.setAcceleration(300);
motor_X.setMaxSpeed(800);
Serial.println("Homing Completed");
Serial.println("");
} else if (homed == 1) {
if (digitalRead(spindle_mounted) == LOW) {
motor_X.setCurrentPosition(9500);
Serial.println("Homing not needed: Set Positon to 9500");
Serial.println("");
} else {
motor_X.setCurrentPosition(43000);
Serial.println("Homing not needed: Set Positon to 43000");
Serial.println("");
}
}
}
void loop() {
if (digitalRead(set_pin) == LOW) {
TravelX = 9500;
//Serial.println("Move to position 9500");
} else {
TravelX = 43000;
//Serial.println("Move to position 43000");
}
move_finished = 0; // Set variable for checking move of the Stepper
motor_X.moveTo(TravelX); // Set new moveto position of Stepper
motor_X.setAcceleration(300);
motor_X.setMaxSpeed(800);
delay(1000); // Wait 1 seconds before moving the Stepper
if (TravelX >= 0 && TravelX <= 43500) {
// Check if the Stepper has reached desired position
if ((motor_X.distanceToGo() != 0)) {
Serial.print("Move to position: ");
Serial.println(TravelX);
Serial.print("Stepper position: ");
Serial.println(motor_X.currentPosition ());
Serial.print("Stepper distance to go: ");
Serial.println(motor_X.distanceToGo());
Serial.print("Speed: ");
Serial.println(motor_X.speed());
motor_X.run(); // Move Stepper into position
}
if ((move_finished == 0) && (motor_X.distanceToGo() == 0)) {
Serial.println("COMPLETED!");
move_finished = 1; // Reset move variable
}
}
}