/*NOTES:
Items named "VCC" and GND are the external power suitable for the stepper requirements, + connected to VMOT
Connect a 100uf across Vmot and GND.2 for peak power spike control.
The A4988 driver "VDD" pin is connected to the arduino/attiny 5v
When applying in real life, ensure current limiting pot is set to approx
0.6-0.8v (measured across pot wiper and any ground)
The coil pair wiring can differ between makers, generally it is red/blue and black/green
Easy test: Motor disconnected from circuit, join any 2 wires and manually turnt he stepper shaft.
If it resists and feels lumpy, it is a coil pair (back EMF causing the resistance to turning, and why you should
never force the shaft roation when it is connected)
ALL grounds must be connected, even from the external PSU
*/
#include <AccelStepper.h>
const byte motStep = 11;
const byte motDir = 12;
int mm = 0;
float mmSteps = 200; // Arbitrary number of steps per mm of axis travel - Change to suit
AccelStepper stepper(AccelStepper::DRIVER, motStep, motDir);
void setup() {
pinMode(motStep, OUTPUT);
pinMode(motDir, OUTPUT);
stepper.setMaxSpeed(400.0);
stepper.setAcceleration(200.0);
}
// Single function to move the motor x number of mm (steps per mm * mm requested)
void motMove(int mm) {
if(stepper.distanceToGo() == 0){
delay(50); // Short delay between moves
stepper.moveTo(mm * mmSteps);
}
stepper.run();
}
void loop() {
motMove(2); // Go to 2mm (based on steps/mm defined above)
motMove(0); // Go to 0mm
}