/*NOTES:
Items named "VCC" and GND are the external power suitable for the stepper requirements, + connected to VMOT
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)
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
}