On this week we changed the way we wanted to rotate the stepper motor, goin from full step mode to 1/16 mode. By attaching a wood pannel with scotch and glue we figured out that we didn't need to rotate the full lenght of the motor. So, after we rewrote the code,this was it's newer version:
/*
Example sketch to control a stepper motor in 1/16-step mode using the A4988 driver,
AccelStepper library, and Arduino. The motor alternates directions quickly.
*/
// Include the AccelStepper library:
#include "AccelStepper.h"
// Define stepper motor connections and motor interface type:
#define dirPin 2
#define stepPin 3
#define enablePin 4
#define ms1Pin 5 // Microstepping pin MS1
#define ms2Pin 6 // Microstepping pin MS2
#define ms3Pin 7 // Microstepping pin MS3
#define motorInterfaceType 1 // Set to 1 for step/direction control
// Create a new instance of the AccelStepper class:
AccelStepper stepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// Set the Enable pin and microstepping pins as outputs:
pinMode(enablePin, OUTPUT);
pinMode(ms1Pin, OUTPUT);
pinMode(ms2Pin, OUTPUT);
pinMode(ms3Pin, OUTPUT);
// Enable the motor driver (active LOW):
digitalWrite(enablePin, LOW);
// Set the A4988 to 1/16-step mode:
digitalWrite(ms1Pin, HIGH); // MS1 = HIGH
digitalWrite(ms2Pin, HIGH); // MS2 = LOW
digitalWrite(ms3Pin, HIGH); // MS3 = LOW
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(30000); // Maximum steps per second
stepper.setAcceleration(30000); // High acceleration for quick changes
}
void loop() {
static bool movingForward = true;
// Move motor back and forth between 0 and 20 steps:
if (movingForward) {
stepper.moveTo(20); // Move 20 steps forward
} else {
stepper.moveTo(0); // Move back to starting position
}
// Run the motor to the target position:
stepper.run();
// Check if the motor reached the target position:
if (stepper.distanceToGo() == 0) {
movingForward = !movingForward; // Reverse direction immediately
}
}
We also did test upon the motor duration to see after 5-10min of functioning, if it wasn't to hot. We found no problems on this side. But we had another problem, vibrations. This problem changed our view upon the structure of the whole assembly and we decided to go for a big box full of sand, to add weight. With this need design came the need of a support for the motor itself.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.