Instantly share code, notes, and snippets.

Embed
What would you like to do?
Control a Stepper with Arduino and Potentiometer Speed Control
//Arduino stepper library
#include <Stepper.h>
// different steppers have different
// amount of steps to do a full
// turn of the wheel.
#define STEPS 32
// connections need to be done carefully
// In1-In4 are added in the sequence 1-3-2-4
Stepper stepper(STEPS, 3, 5, 4, 6);
// the number of steps to take
int numSteps;
// Only serial to setup here
void setup() {
Serial.begin(115200); // start serial
}
// loop runs constantly
void loop() {
// full revolution
numSteps = (STEPS * 64) /100;
// max power is 1024
stepper.setSpeed( getMotorSpeed() );
// take the steps
// you can reverse direction with -numSteps
stepper.step(numSteps);
}
int getMotorSpeed() {
// read the sensor value:
int sensorReading = analogRead(A0);
Serial.println(sensorReading);
// map it to a range from 0 to 1024:
int motorSpeed = map(sensorReading, 0, 769, 0, 1024);
// set the motor speed:
if (motorSpeed > 0) {
return motorSpeed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment