Skip to content

Instantly share code, notes, and snippets.

@TURBULENTE
Created March 5, 2017 19:42
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 TURBULENTE/40d4a54901a11353918a512166fceeeb to your computer and use it in GitHub Desktop.
Save TURBULENTE/40d4a54901a11353918a512166fceeeb to your computer and use it in GitHub Desktop.
Stepper + Potentiometer Arduino
// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
// Requires the AFMotor library (https://github.com/adafruit/Adafruit-Motor-Shield-library)
// And AccelStepper with AFMotor support (https://github.com/adafruit/AccelStepper)
// Public domain!
#include <AccelStepper.h>
#include <AFMotor.h>
AF_Stepper motor1(200, 1);
//POTEN
// Variables to store current, previous and move position
int val = 0;
int previous = 0;
int long newval = 0;
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {
motor1.onestep(FORWARD, SINGLE);
}
void backwardstep() {
motor1.onestep(BACKWARD, SINGLE);
}
AccelStepper stepper(forwardstep, backwardstep); // use functions to step
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
stepper.setMaxSpeed(4800); // Set speed fast enough to follow pot rotation
stepper.setAcceleration(4800); // High Acceleration to follow pot rotation
/* stepper.setSpeed(550); */
}
void loop()
{
/* stepper.runSpeed();*/
//poten
val = analogRead(A0); // Read Potentiometer current value
if ((val > previous+6) || (val < previous-6)) { // Check that a move of the pot is at least > or < than 6
newval = map(val, 0, 1023, 0, 200); // Map value (1600 = 1 stepper shaft rotation)
stepper.runToNewPosition(newval); // Move stepper to new position
previous = val; // save current value into variable previous
}
// Serial.println(val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment