Skip to content

Instantly share code, notes, and snippets.

@chris-schmitz
Created July 10, 2020 22:05
Show Gist options
  • Save chris-schmitz/52903f54ba1e73eee116741b76adcdf1 to your computer and use it in GitHub Desktop.
Save chris-schmitz/52903f54ba1e73eee116741b76adcdf1 to your computer and use it in GitHub Desktop.
// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
// Requires the Adafruit_Motorshield v2 library
// https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// And AccelStepper with AFMotor support
// https://github.com/adafruit/AccelStepper
// This tutorial is for Adafruit Motorshield v2 only!
// Will not work with v1 shields
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>
#include <Wire.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 2);
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep1()
{
myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1()
{
myStepper1->onestep(BACKWARD, SINGLE);
}
AccelStepper Astepper1(forwardstep1, backwardstep1); // use functions to step
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
Astepper1.moveTo(5000);
Astepper1.setSpeed(200);
}
void loop()
{
Astepper1.runSpeed();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment