# Stepper Code question

I just finished setting up and testing my Arduino, A4988 and NEMA 11 3.6V stepper motor with test code from decrDude. All works and now I’m adapting some code found at: http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.htmlExample #5, three buttons and a pot to adjust speed.
The Code:

[code]#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
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).
}
else {
// Now read the pot (from 0 to 1023)
// 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();
}[/code]

First question that comes to mind is in the easy driver code there is no sign of defining an enable pin as well as no sign of a connection to the enable from Arduino to the easy driver on the image.
Is it as simple as just adding code to #define enablePin 5 ?

Hello.

Yes, you could just `#define` that pin and use it like any other digital pin to control the enable pin on the A4988.

-Jon

Hi Jonathan,
How familiar are you with AccelStepper? Anyhow I added #define enablePin 5 and in the void setup: digitalWrite(enablePin, LOW);

Don’t know if it is that easy but I put acceleration in as well: stepper1.setAcceleration(100);
Any obvious syntax errors?

[code]// Example5 code for Brian Schmalz’s Easy Driver Example page
// http://www.schmalzhaus.com/EasyDriver/EasyDriverExamples.html

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8);

// Define our three input button pins that is now four with the enable pin added
#define enablePin 5
#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 2500
#define MIN_SPEED 250

void setup() {
// The only AccelStepper value we have to set here is the max speeed, which is higher than we’ll ever go
// NOTE added acceleration to code
stepper1.setMaxSpeed(10000.0);
stepper1.setAcceleration(100);
//Enable A4899 driver board
digitalWrite(enablePin, LOW);
// 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
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).
}
else {
// Now read the pot (from 0 to 1023)
// 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();
}[/code]

We do not use AccelStepper, so we are not very familiar with that code. Have you tried compiling your code? If it compiles, your syntax is probably okay.

-Jon