I'm running into some problems with my AccelStepper library use. I am attempting to achieve the same outcome as this video:
To control a stepper's direction and speed using only 1 potentiometer
This is the code that I have been attempting to modify in its proven raw form from
with no success....
#include <AccelStepper.h>
// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8);
// Define our three input button pins
#define LEFT_PIN 4
#define STOP_PIN 3
#define RIGHT_PIN 2
// Define our analog pot input pin
#define SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 500
#define MIN_SPEED 0.1
void setup() {
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
}
void loop() {
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
// This will run the stepper at a constant speed
stepper1.runSpeed();
}
The code that I posted utilizes three switches; one to set a direction CW, one to set a direction CCW, and one to stop the stepper. The part of the code that interested me the most is that it uses a potentiometer to control the speed. I like this code because it works with my driver that my board controls. I want the code to allow a potentiometer to control the speed and direction of the stepper, to eliminate the switches. Thank you for your time.
Your potentiomenter should cause the result from analogRead() to range from 0-1023. If you subtract 512 from that you change the range to -512 to + 511. It should give you 0 when the pot is in the middle.
You can use the sign of the value to signify direction and the size of the value (0-511 or 512) to control the speed.
The code that I posted utilizes three switches; one to set a direction CW, one to set a direction CCW, and one to stop the stepper.
With that combination of switches, you can step clockwise, counter-clockwise, both clockwise and counter-clockwise, or neither clockwise or counter-clockwise. It makes NO sense to have TWO switches for direction.
You want to step ONE direction - no more and no less. A single switch is either pressed or it isn't. That is, it has ONE state, which corresponds to selecting ONE direction.
There are three switches so there would be eight states except that the if...else statements in the program reduce to four states in this priority order: LEFT, RIGHT, STOP, or continue (implicit if no button is pressed).
I would have prioritized STOP over LEFT or RIGHT, but at least a choice was made. Besides, STOP doesn't really stop the motor, it just runs it at MIN_SPEED speed in the LEFT direction. That is one of the bad things that I wrote about. Maybe there is a missing pair of parentheses?