Gorgeous Orange Replicator
Controls
Properties
Availability & Pricing
Distributor | Qty 1 | |
---|---|---|
Digi-Key | $48.40 | |
Mouser | $48.40 |
Assets
Simulation
´
) en lugar de comillas dobles ('
o "
).stepper_X
y stepper_x
).Cambios realizados:Cpp
#include <AccelStepper.h> char SerialData; long motorx = 0; long motory = 0; long motorz = 0; int M = 0; AccelStepper stepper_x(1, 12, 14); void setup() { Serial.begin(9600); pinMode(8, OUTPUT); SerialData = '0'; stepper_x.setSpeed(1600); stepper_x.setAcceleration(2000); stepper_x.setMaxSpeed(2600); digitalWrite(8, HIGH); } void loop() { if (Serial.available()) { SerialData = Serial.read(); if (SerialData == 'p') { Serial.println('0'); digitalWrite(8, LOW); motorx = Serial.parseFloat(); stepper_x.moveTo($$motorx$$); M = 1; } } stepper_x.run(); if ($$stepper_x.distanceToGo()$$ == 0 && M == 1) { digitalWrite(8, HIGH); Serial.println('1'); M = 0; SerialData = 'n'; } }
AccelStepper
(de stepper_X
a stepper_x
).pinMode(8, OUTPUT);
(había un punto en lugar de una coma y ajustes en la sintaxis de SerialData
).if (Serial.available())
para asegurar la apertura y cierre correctos del paréntesis.SerialData = Serial.read();
(quitar coma errónea).if (SerialData == 'p')
para comparar el valor de SerialData
.Cambios realizados:Cpp
#include <AccelStepper.h> char serialData; long motorx = 0; int M = 0; AccelStepper stepper_X(1, 12, 14); long movementHistory[10]; int historyIndex = 0; void setup() { Serial.begin(9600); pinMode(8, OUTPUT); serialData = '0'; stepper_X.setSpeed(1600); stepper_X.setAcceleration(2000); stepper_X.setMaxSpeed(2600); digitalWrite(8, HIGH); } void loop() { if (Serial.available()) { serialData = Serial.read(); if (serialData == 'p') { Serial.println('0'); digitalWrite(8, LOW); motorx = Serial.parseFloat(); stepper_X.moveTo(motorxToSteps(motorx)); addMovementToHistory(motorx); M = 1; } if (serialData == 'h') { printMovementHistory(); } } stepper_X.run(); if (stepper_X.distanceToGo() == 0 && M == 1) { digitalWrite(8, HIGH); Serial.println('1'); M = 0; serialData = 'n'; } } long motorxToSteps(long motorx) { // Convert degrees to steps (Assuming 1 degree = 10 steps here, change as needed) return motorx * 10; } void addMovementToHistory(long degrees) { if (historyIndex < 10) { movementHistory[historyIndex] = degrees; historyIndex++; } else { // Shift history if array is full for (int i = 1; i < 10; i++) { movementHistory[i - 1] = movementHistory[i]; } movementHistory[9] = degrees; } } void printMovementHistory() { Serial.println("Movement History (last 10 moves):"); for (int i = 0; i < historyIndex; i++) { Serial.print(movementHistory[i]); Serial.print(" degrees"); if (i < historyIndex - 1) { Serial.print(", "); } } Serial.println(); }
#include <AccelStepper.h>
.serialData
para ser consistente.' '
y "
.pinMode
y digitalWrite
.AccelStepper
a stepper_X
.motorxToSteps(long motorx)
para convertir grados a pasos.movementHistory
para almacenar los últimos 10 movimientos en grados.addMovementToHistory(long degrees)
para gestionar el historial.printMovementHistory()
para imprimir el historial cuando se recibe el carácter 'h'
.'p'
seguido por el valor en grados. Además, puedes enviar 'h'
para imprimir el historial de los últimos 10 movimientos.AccelStepper.h
. En su lugar, usaremos la función de retardo (delay()
) de Arduino para crear una rutina simple para el control del motor. A continuación te proporcionaré un código básico que hará que el motor se mueva a una posición específica en grados y mantendrá un historial de movimientos realizados.AccelStepper.h
:
Explicación del código:Cpp
// Definición de pines const int dirPin = 12; const int stepPin = 14; const int enablePin = 8; char serialData; long motorx = 0; int M = 0; long movementHistory[10]; int historyIndex = 0; const int stepsPerRevolution = 200; // Ajusta según tu motor const float degreesPerStep = 360.0 / stepsPerRevolution; // Grados por paso void setup() { Serial.begin(9600); pinMode(dirPin, OUTPUT); pinMode(stepPin, OUTPUT); pinMode(enablePin, OUTPUT); digitalWrite(enablePin, LOW); // Activa el controlador de motor serialData = '0'; } void loop() { if (Serial.available()) { serialData = Serial.read(); if (serialData == 'p') { Serial.println('0'); digitalWrite(enablePin, HIGH); motorx = Serial.parseFloat(); moveToDegree(motorx); addMovementToHistory(motorx); M = 1; } if (serialData == 'h') { printMovementHistory(); } } } void moveToDegree(long degree) { long steps = degreeToSteps(degree); digitalWrite(dirPin, steps > 0 ? HIGH : LOW); steps = abs(steps); for (long i = 0; i < steps; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(500); // Ajusta para controlar la velocidad digitalWrite(stepPin, LOW); delayMicroseconds(500); // Ajusta para controlar la velocidad } } long degreeToSteps(long degree) { return degree / degreesPerStep; } void addMovementToHistory(long degrees) { if (historyIndex < 10) { movementHistory[historyIndex] = degrees; historyIndex++; } else { // Desplaza el historial si el arreglo está lleno for (int i = 1; i < 10; i++) { movementHistory[i - 1] = movementHistory[i]; } movementHistory[9] = degrees; } } void printMovementHistory() { Serial.println("Historial de movimientos (últimos 10 movimientos):"); for (int i = 0; i < historyIndex; i++) { Serial.print(movementHistory[i]); Serial.print(" grados"); if (i < historyIndex - 1) { Serial.print(", "); } } Serial.println(); }
dirPin
para la dirección del motor.stepPin
para los pulsos de paso.enablePin
para habilitar/deshabilitar el controlador del motor.stepsPerRevolution
ajustado según el motor que estés usando.degreesPerStep
calculado para convertir grados a pasos.moveToDegree()
para mover el motor a una posición específica en grados.degreeToSteps()
para convertir grados a pasos.addMovementToHistory(long degrees)
y printMovementHistory()
, como en el código anterior, para gestionar y mostrar el historial.AccelStepper.h
y proporcionarte la funcionalidad básica para mover el motor a posiciones específicas en grados y mantener un historial de movimientos realizados.DIR
a 12
(dirPin)STEP
a 14
(stepPin)EN
a 8
(enablePin)GND
a GND
VDD
a 5V
1A
, 1B
, 2A
, 2B
).VMOT
y GND
del A4988.Notas importantes:Cpp
// Definición de pines const int dirPin = 12; const int stepPin = 14; const int enablePin = 8; char serialData; long motorx = 0; int M = 0; long movementHistory[10]; int historyIndex = 0; const int stepsPerRevolution = 200; // Ajusta según tu motor y configuración de microstepping const float degreesPerStep = 360.0 / stepsPerRevolution; // Grados por paso void setup() { Serial.begin(9600); pinMode(dirPin, OUTPUT); pinMode(stepPin, OUTPUT); pinMode(enablePin, OUTPUT); digitalWrite(enablePin, LOW); // Activa el controlador de motor A4988 (por defecto LOW es activo) serialData = '0'; } void loop() { if (Serial.available()) { serialData = Serial.read(); if (serialData == 'p') { Serial.println('0'); digitalWrite(enablePin, LOW); // Activa el A4988 antes de mover el motor motorx = Serial.parseFloat(); moveToDegree(motorx); addMovementToHistory(motorx); M = 1; } if (serialData == 'h') { printMovementHistory(); } } } void moveToDegree(long degree) { long steps = degreeToSteps(degree); digitalWrite(dirPin, steps > 0 ? HIGH : LOW); steps = abs(steps); for (long i = 0; i < steps; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(500); // Ajusta para controlar la velocidad digitalWrite(stepPin, LOW); delayMicroseconds(500); // Ajusta para controlar la velocidad } delay(10); // Espera para asegurar que el último paso se ejecute } long degreeToSteps(long degree) { return degree / degreesPerStep; } void addMovementToHistory(long degrees) { if (historyIndex < 10) { movementHistory[historyIndex] = degrees; historyIndex++; } else { // Desplaza el historial si el arreglo está lleno for (int i = 1; i < 10; i++) { movementHistory[i - 1] = movementHistory[i]; } movementHistory[9] = degrees; } } void printMovementHistory() { Serial.println("Historial de movimientos (últimos 10 movimientos):"); for (int i = 0; i < historyIndex; i++) { Serial.print(movementHistory[i]); Serial.print(" grados"); if (i < historyIndex - 1) { Serial.print(", "); } } Serial.println(); }
stepsPerRevolution
acorde a la configuración de microstepping.AccelStepper.h
, debemos convertir los grados deseados en pasos para el motor paso a paso. Considerando que deseas trabajar completamente en grados, el código proporcionado anteriormente ya hace esta conversión internamente, aunque la función principal controla los pasos del motor.stepsPerRevolution
) tendrán que ajustarse en consecuencia.degreesPerStep
sea correcto para tu configuración de motor y microstepping.Explicación:Cpp
// Definición de pines const int dirPin = 12; // Pin DIR del A4988 const int stepPin = 14; // Pin STEP del A4988 const int enablePin = 8; // Pin ENABLE del A4988 char serialData; long motorDegrees = 0; // Grados solicitados long currentDegrees = 0; // Grados actuales del motor int M = 0; long movementHistory[10]; // Historial de movimientos en grados int historyIndex = 0; // Configuración del A4988 y motor const int stepsPerRevolution = 200; // Ajusta según tu motor y configuración de microstepping const float degreesPerStep = 360.0 / stepsPerRevolution; // Grados por paso void setup() { Serial.begin(9600); pinMode(dirPin, OUTPUT); pinMode(stepPin, OUTPUT); pinMode(enablePin, OUTPUT); digitalWrite(enablePin, LOW); // Activa el controlador de motor A4988 (default LOW es activo) serialData = '0'; } void loop() { if (Serial.available()) { serialData = Serial.read(); if (serialData == 'p') { Serial.println('0'); digitalWrite(enablePin, LOW); // Activa el A4988 antes de mover el motor motorDegrees = Serial.parseFloat(); moveToDegree(motorDegrees); addMovementToHistory(motorDegrees); M = 1; } if (serialData == 'h') { printMovementHistory(); } } } void moveToDegree(long targetDegrees) { long degreeDifference = targetDegrees - currentDegrees; long steps = degreesToSteps(degreeDifference); // Ajuste la dirección del motor if (degreeDifference > 0) { digitalWrite(dirPin, HIGH); } else { digitalWrite(dirPin, LOW); } // Mover el motor la cantidad de pasos necesaria steps = abs(steps); for (long i = 0; i < steps; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(500); // Ajustar según la velocidad deseada digitalWrite(stepPin, LOW); delayMicroseconds(500); // Ajustar según la velocidad deseada } // Actualizar la posición actual del motor en grados currentDegrees = targetDegrees; } long degreesToSteps(long degree) { return degree / degreesPerStep; } void addMovementToHistory(long degrees) { if (historyIndex < 10) { movementHistory[historyIndex] = degrees; historyIndex++; } else { // Desplazar el historial si el arreglo está lleno for (int i = 1; i < 10; i++) { movementHistory[i - 1] = movementHistory[i]; } movementHistory[9] = degrees; } } void printMovementHistory() { Serial.println("Historial de movimientos (últimos 10 movimientos):"); for (int i = 0; i < historyIndex; i++) { Serial.print(movementHistory[i]); Serial.print(" grados"); if (i < historyIndex - 1) { Serial.print(", "); } } Serial.println(); }
dirPin
, stepPin
, enablePin
son los pines conectados al A4988.motorDegrees
guarda el ángulo al que queremos mover.currentDegrees
guarda el ángulo actual del motor.movementHistory
mantiene el historial de movimientos.setup()
.loop()
):'p'
para mover a una posición en grados y 'h'
para imprimir el historial de movimientos.moveToDegree(long targetDegrees)
convierte los grados deseados a pasos y mueve el motor.addMovementToHistory(long degrees)
y printMovementHistory()
manejan y muestran el historial de movimientos en grados.Cpp
// Definición de pines const int dirPin = 12; // Pin DIR del A4988 const int stepPin = 14; // Pin STEP del A4988 const int enablePin = 8; // Pin ENABLE del A4988 char serialData; long motorDegrees = 0; // Grados solicitados long currentDegrees = 0; // Grados actuales del motor int M = 0; long movementHistory[10]; // Historial de movimientos en grados int historyIndex = 0; // Configuración del A4988 y motor const int stepsPerRevolution = 200; // Ajusta según tu motor y configuración de microstepping const float degreesPerStep = 360.0 / stepsPerRevolution; // Grados por paso void setup() { Serial.begin(9600); pinMode(dirPin, OUTPUT); pinMode(stepPin, OUTPUT); pinMode(enablePin, OUTPUT); digitalWrite(enablePin, LOW); // Activa el controlador de motor A4988 (por defecto LOW es activo) serialData = '0'; } void loop() { if (Serial.available()) { serialData = Serial.read(); if (serialData == 'p') { Serial.println('0'); digitalWrite(enablePin, LOW); // Activa el A4988 antes de mover el motor motorDegrees = Serial.parseFloat(); moveToDegree(motorDegrees); addMovementToHistory(motorDegrees); M = 1; } if (serialData == 'h') { printMovementHistory(); } } } void moveToDegree(long targetDegrees) { long degreeDifference = targetDegrees - currentDegrees; long steps = degreesToSteps(degreeDifference); // Ajuste la dirección del motor if (degreeDifference > 0) { digitalWrite(dirPin, HIGH); } else { digitalWrite(dirPin, LOW); } // Mover el motor la cantidad de pasos necesaria steps = abs(steps); for (long i = 0; i < steps; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(500); // Ajustar según la velocidad deseada digitalWrite(stepPin, LOW); delayMicroseconds(500); // Ajustar según la velocidad deseada } // Actualizar la posición actual del motor en grados currentDegrees = targetDegrees; } long degreesToSteps(long degree) { return degree / degreesPerStep; } void addMovementToHistory(long degrees) { if (historyIndex < 10) { movementHistory[historyIndex] = degrees; historyIndex++; } else { // Desplazar el historial si el arreglo está lleno for (int i = 1; i < 10; i++) { movementHistory[i - 1] = movementHistory[i]; } movementHistory[9] = degrees; } } void printMovementHistory() { Serial.println("Historial de movimientos (últimos 10 movimientos):"); for (int i = 0; i < historyIndex; i++) { Serial.print(movementHistory[i]); Serial.print(" grados"); if (i < historyIndex - 1) { Serial.print(", "); } } Serial.println(); }
Cpp
#include <AccelStepper.h> char serialData; long motorx = 0; int M = 0; AccelStepper stepper_X(1, 14, 12); // STEP pin 14, DIR pin 12 (adjusted for A4988) long movementHistory[10]; int historyIndex = 0; // Constants const float stepsPerRevolution = 200; // Ajusta según tu motor y configuración de microstepping const float degreesPerStep = 360.0 / stepsPerRevolution; // Grados por paso void setup() { Serial.begin(9600); pinMode(8, OUTPUT); // Pin ENABLE serialData = '0'; stepper_X.setMaxSpeed(1000); stepper_X.setAcceleration(50); // Enable the motor driver digitalWrite(8, LOW); // Activating the motor driver (LOW is active) digitalWrite(8, HIGH); // Keep the motor driver enabled initially } void loop() { if (Serial.available()) { serialData = Serial.read(); if (serialData == 'p') { Serial.println("0"); digitalWrite(8, LOW); // Activa el A4988 antes de mover el motor motorx = Serial.parseFloat(); stepper_X.moveTo(motorxToSteps(motorx)); addMovementToHistory(motorx); M = 1; } if (serialData == 'h') { printMovementHistory(); } } stepper_X.run(); if (stepper_X.distanceToGo() == 0 && M == 1) { digitalWrite(8, HIGH); // Desactiva el A4988 después de mover el motor Serial.println("1"); M = 0; serialData = 'n'; } } long motorxToSteps(long motorx) { // Convert degrees to steps return motorx / degreesPerStep; } void addMovementToHistory(long degrees) { if (historyIndex < 10) { movementHistory[historyIndex] = degrees; historyIndex++; } else { // Shift history if array is full for (int i = 1; i < 10; i++) { movementHistory[i - 1] = movementHistory[i]; } movementHistory[9] = degrees; } } void printMovementHistory() { Serial.println("Movement History (last 10 moves):"); for (int i = 0; i < historyIndex; i++) { Serial.print(movementHistory[i]); Serial.print(" degrees"); if (i < historyIndex - 1) { Serial.print(", "); } } Serial.println(); }
Cpp
// Define los pines de control const int dirPin = 2; const int stepPin = 3; void setup() { pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); digitalWrite(dirPin, HIGH); // Definir la dirección: HIGH o LOW } void loop() { // Paso adelante digitalWrite(stepPin, HIGH); delayMicroseconds(1000); // Ajusta la velocidad cambiando la duración del delay digitalWrite(stepPin, LOW); delayMicroseconds(1000); }
Welcome 👋
Flux helps you build PCBs faster with an AI teammate!
To collaborate on this project, get the latest updates,
or fork your own version, sign up now.