Hello there!
I'm currently using a setup of 2 stepper motors with DM556 drivers, 24V 10A power supply, and arduino Uno.
In my project I was using GRBL to control those steppers in a machine to make bowstrings for archery.
I'm using macros to make them spin for a long distance sincronized in the same direction. And with another macro to spin for 10 turns each in oposite directions. It was enough for me to do the job, but I decided to try to run it standalone, without a PC and the GRBL, but the speeds using regular libraries like Stepper.h, AccelStepper, or SpeedyStepper does not gave me the same speeds as with the GRBL.
I'm not a programmer and I'm actually using ChatGPT to generate the sketches.
I was trying to edit the GRBL's source code to include just the buttons I need to control the steppers without the need of a PC, but it's very hard for me as a non programer guy.
Is there any faster library to use with?
Here is the best I found with the SpeedyStepper.h library but now reaching just half of the speed I was expecting and with very noisy results.
#include <SpeedyStepper.h>
// Definições dos pinos
const int LED_NORMAL_PIN = 0; // LED para indicar rotação normal
const int LED_INVERT_PIN = 1; // LED para indicar rotação invertida
const int STEP_PIN1 = 2; // Pulso motor 1
const int DIR_PIN1 = 5; // Direcao motor 1
const int STEP_PIN2 = 3; // Pulso motor 2
const int DIR_PIN2 = 6; // Direcao motor 2
const int BUTTON_PIN = 8; // Botão para iniciar/parar os motores
const int NEW_BUTTON_PIN_1 = 9; // Botão para iniciar/parar os motores com 50% da velocidade
const int NEW_BUTTON_PIN_2 = 10; // Botão para ligar/desligar os motores com 25% da velocidade
const int NEW_BUTTON_PIN_3 = 11; // Botão para ligar/desligar os motores com 10% da velocidade
const int TEN_ROTATE_BUTTON_PIN = 12; // Botão para girar os motores por 10 voltas em sentidos opostos
const int ONE_ROTATE_BUTTON_PIN = 7; // Botão para girar os motores por 1 volta em sentidos opostos
const int ROTATE_BUTTON_PIN = A0; // Botão para inverter o sentido de rotação dos motores
const int EMERGENCY_STOP_PIN = A1; // Botão de emergência
SpeedyStepper stepper1;
SpeedyStepper stepper2;
bool motorRunning = false;
bool invertDirection = false; // Controla a inversão de sentido de ambos os motores
bool lastButtonState = HIGH;
bool lastNewButtonState1 = HIGH;
bool lastNewButtonState2 = HIGH;
bool lastNewButtonState3 = HIGH;
bool lastRotateButtonState = HIGH;
bool lastOneRotateButtonState = HIGH;
bool lastTenRotateButtonState = HIGH;
bool lastEmergencyStopState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 500;
const int MAX_SPEED = 6000;
const int QUARTER_MAX_SPEED = MAX_SPEED / 5;
const int ACCELERATION = 10000;
const int QUICK_DECELERATION = 16000;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(NEW_BUTTON_PIN_1, INPUT_PULLUP);
pinMode(NEW_BUTTON_PIN_2, INPUT_PULLUP);
pinMode(NEW_BUTTON_PIN_3, INPUT_PULLUP);
pinMode(ROTATE_BUTTON_PIN, INPUT_PULLUP);
pinMode(EMERGENCY_STOP_PIN, INPUT_PULLUP);
pinMode(ONE_ROTATE_BUTTON_PIN, INPUT_PULLUP);
pinMode(TEN_ROTATE_BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_NORMAL_PIN, OUTPUT);
pinMode(LED_INVERT_PIN, OUTPUT);
stepper1.connectToPins(STEP_PIN1, DIR_PIN1);
stepper2.connectToPins(STEP_PIN2, DIR_PIN2);
stepper1.setSpeedInStepsPerSecond(MAX_SPEED);
stepper1.setAccelerationInStepsPerSecondPerSecond(ACCELERATION);
stepper2.setSpeedInStepsPerSecond(MAX_SPEED);
stepper2.setAccelerationInStepsPerSecondPerSecond(ACCELERATION);
}
void toggleMotors(bool run, int speed, bool invert) {
if (run) {
// Define a velocidade para ambos os motores
stepper1.setSpeedInStepsPerSecond(speed);
stepper2.setSpeedInStepsPerSecond(speed);
// Movimento relativo com base na direção atual dos motores
stepper1.setupRelativeMoveInSteps(invert ? -200000 : 200000);
stepper2.setupRelativeMoveInSteps(invert ? 200000 : -200000);
} else {
// Para o movimento dos motores com desaceleração rápida
stepper1.setAccelerationInStepsPerSecondPerSecond(QUICK_DECELERATION);
stepper2.setAccelerationInStepsPerSecondPerSecond(QUICK_DECELERATION);
stepper1.setupStop();
stepper2.setupStop();
// Volta à aceleração normal
stepper1.setAccelerationInStepsPerSecondPerSecond(ACCELERATION);
stepper2.setAccelerationInStepsPerSecondPerSecond(ACCELERATION);
}
}
void rotateMotors(int numVoltas) {
// Configura uma nova velocidade para girar os motores
stepper1.setSpeedInStepsPerSecond(QUARTER_MAX_SPEED);
stepper2.setSpeedInStepsPerSecond(QUARTER_MAX_SPEED);
// Calcula o número de passos necessário para as voltas desejadas
long numPassos = numVoltas * 200; // 200 passos por volta (exemplo)
// Move os motores relativamente e simultaneamente
stepper1.setupRelativeMoveInSteps(invertDirection ? -numPassos : numPassos);
stepper2.setupRelativeMoveInSteps(invertDirection ? -numPassos : numPassos); // Movimento no sentido oposto se invertDirection for true
// Inicia o movimento simultaneamente
while ((!stepper1.motionComplete()) || (!stepper2.motionComplete())) {
stepper1.processMovement();
stepper2.processMovement();
}
}
void emergencyStop() {
stepper1.setAccelerationInStepsPerSecondPerSecond(QUICK_DECELERATION);
stepper2.setAccelerationInStepsPerSecondPerSecond(QUICK_DECELERATION);
stepper1.setupStop();
stepper2.setupStop();
stepper1.setAccelerationInStepsPerSecondPerSecond(ACCELERATION);
stepper2.setAccelerationInStepsPerSecondPerSecond(ACCELERATION);
motorRunning = false;
}
void updateLEDs() {
digitalWrite(LED_NORMAL_PIN, invertDirection ? LOW : HIGH);
digitalWrite(LED_INVERT_PIN, invertDirection ? HIGH : LOW);
}
void loop() {
bool buttonState = digitalRead(BUTTON_PIN);
bool newButtonState1 = digitalRead(NEW_BUTTON_PIN_1);
bool newButtonState2 = digitalRead(NEW_BUTTON_PIN_2);
bool newButtonState3 = digitalRead(NEW_BUTTON_PIN_3);
bool rotateButtonState = digitalRead(ROTATE_BUTTON_PIN);
bool oneRotateButtonState = digitalRead(ONE_ROTATE_BUTTON_PIN);
bool tenRotateButtonState = digitalRead(TEN_ROTATE_BUTTON_PIN);
bool emergencyStopState = digitalRead(EMERGENCY_STOP_PIN);
unsigned long currentMillis = millis();
if (emergencyStopState == LOW && lastEmergencyStopState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
emergencyStop();
lastDebounceTime = currentMillis;
}
if (rotateButtonState == LOW && lastRotateButtonState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
invertDirection = !invertDirection; // Inverte o sentido de rotação armazenado
updateLEDs(); // Atualiza os LEDs
lastDebounceTime = currentMillis;
}
if (buttonState == LOW && lastButtonState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
motorRunning = !motorRunning;
toggleMotors(motorRunning, MAX_SPEED, invertDirection);
lastDebounceTime = currentMillis;
}
if (newButtonState1 == LOW && lastNewButtonState1 == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
motorRunning = !motorRunning;
toggleMotors(motorRunning, MAX_SPEED / 2, invertDirection);
lastDebounceTime = currentMillis;
}
if (newButtonState2 == LOW && lastNewButtonState2 == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
motorRunning = !motorRunning;
toggleMotors(motorRunning, MAX_SPEED / 4, invertDirection);
lastDebounceTime = currentMillis;
}
if (newButtonState3 == LOW && lastNewButtonState3 == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
motorRunning = !motorRunning;
toggleMotors(motorRunning, MAX_SPEED / 10, invertDirection);
lastDebounceTime = currentMillis;
}
if (oneRotateButtonState == LOW && lastOneRotateButtonState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
rotateMotors(1); // Exemplo: Girar 1 volta
lastDebounceTime = currentMillis;
}
if (tenRotateButtonState == LOW && lastTenRotateButtonState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
rotateMotors(10); // Exemplo: Girar 10 voltas
lastDebounceTime = currentMillis;
}
lastButtonState = buttonState;
lastNewButtonState1 = newButtonState1;
lastNewButtonState2 = newButtonState2;
lastNewButtonState3 = newButtonState3;
lastRotateButtonState = rotateButtonState;
lastOneRotateButtonState = oneRotateButtonState;
lastTenRotateButtonState = tenRotateButtonState;
lastEmergencyStopState = emergencyStopState;
stepper1.processMovement();
stepper2.processMovement();
}