Skip to content

Instantly share code, notes, and snippets.

@chris-pws
Created June 23, 2018 20:00
Show Gist options
  • Save chris-pws/0cd1b025fbe76cc32ef6b3dccde92145 to your computer and use it in GitHub Desktop.
Save chris-pws/0cd1b025fbe76cc32ef6b3dccde92145 to your computer and use it in GitHub Desktop.
Reads an 8 bit value (0 - 254) from the analog to digital converter and moves two stepper motors to a relative position within the motor's range (0 - 63). You could also use the Arduino map() function to translate the different ranges.
#include <AccelStepper.h>
// Define a stepper and the pins it will use
//AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper xAxis(1, 4, 3); // pin 3 = step, pin 6 = direction
AccelStepper yAxis(1, 6, 5); // pin 3 = step, pin 6 = direction
#define Xpin A0
#define Ypin A3
void setup()
{
// Change these to suit your stepper if you want
xAxis.setMaxSpeed(30);
yAxis.setMaxSpeed(30);
}
void loop()
{
// Read new position
int Xpos = analogRead(Xpin) /4;
int Ypos = analogRead(Ypin) /4;
xAxis.moveTo(Xpos);
yAxis.moveTo(Ypos);
yAxis.runSpeedToPosition();
xAxis.runSpeedToPosition();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment