Skip to content

Instantly share code, notes, and snippets.

@haashimrehan
Created October 27, 2023 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haashimrehan/4218e6da1480972896f1795613dc8b9d to your computer and use it in GitHub Desktop.
Save haashimrehan/4218e6da1480972896f1795613dc8b9d to your computer and use it in GitHub Desktop.
Stepper motor testing (with buttons)
#include "FastAccelStepper.h"
 
// As in StepperDemo for Motor 1 on AVR
#define dirPinStepper    2
#define enablePinStepper 6
#define stepPinStepper   9  // OC1A in case of AVR
 
FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper = NULL;
 
void setup() {
  Serial.begin(115200);
  engine.init();
  stepper = engine.stepperConnectToPin(stepPinStepper);
  if (stepper) {
    stepper->setDirectionPin(dirPinStepper);
    stepper->setEnablePin(enablePinStepper);
    stepper->setAutoEnable(true);
 
    // If auto enable/disable need delays, just add (one or both):
    // stepper->setDelayToEnable(50);
    // stepper->setDelayToDisable(1000);
 
    stepper->setSpeedInUs(200);  // the parameter is us/step !!!
    stepper->setAcceleration(3000);
 
  }
 
}
 
void loop() {
 
  if (digitalRead(7)) {
    stepper->moveTo(100000);
  } else if (digitalRead(8)) {
    stepper->moveTo(-100000);
  } else {
    stepper->stopMove();
  }
}
@haashimrehan
Copy link
Author

Requires FastAccelStepper library
https://github.com/gin66/FastAccelStepper

(Use the above link instead of installing from Arduino’s built in library manager, otherwise it won’t compile)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment