Hi, looking for Arduino coding help for a stepper motor operating system. I’m a complete newbie (Old Mech engineer) to the world of Arduino and ‘C’ programming so please bear with me.
System- NEMA17 /100:1 ratio stepper connected to a Leonardo board via an Easy Driver shield. It’s controlled via 2 pushbutton momentary switches (FWD & REV) and 2 limit switches on the stepper motor/gear box assy (REV & STOP/PARK). The REV switches are connected in parallel, one for automated operation, the other for Manual intervention/override.
Operation- Push the FWD(GO) button and the stepper motor runs CW until it hits the REV limit switch (or the manual REV button is pushed) and then returns CCW to the STOP/PARK limit switch and stops - total movement 20deg. Additionally, the FWD/CW (GO) movement is speed variable via a 10K pot (Slowly rotate at a manually set / adjusted speed). While the REV/CCW movement runs at single high speed only. (Returns quickly to start position and stops). It also has R/G/B LED indicators for each action/mode.
My difficulties – I found during Adruino research Brian Schmalz excellent EasyDriver examples, one of which I’ve used and modified (butchered??). Sadly it doesn’t work so I’m probably doing a huge number of incorrect things within the code??? I’ve read complex stepper codes talking of push button ‘Debounce’ etc, but struggle to understand what I require?
//Based on 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
#define GO_PIN 4
#define STOP_PIN 3
#define RETURN_PIN 2
#define ENABLE 6
#define RELAY1 7
#define LED_BLUE 11
#define LED_GREEN 12
#define LED_RED 10
#define SPEED_PIN 0
#define MAX_SPEED 5500
#define MIN_SPEED 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(GO_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RETURN_PIN, INPUT_PULLUP);
pinMode(RELAY1, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(ENABLE,OUTPUT);
}
void loop() {
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 3000; // 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 (digitalRead(GO_PIN) == 0) {
sign = -1;
digitalWrite(RELAY1,LOW);
digitalWrite(LED_BLUE,LOW);
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_RED,HIGH);
if(analog_read_counter > 0)
analog_read_counter--;
analog_read_counter = 3000;
analog_value = analogRead(SPEED_PIN);
stepper1.runSpeed();
current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(current_speed);
stepper1.runSpeed();
}
else if (digitalRead(RETURN_PIN) == 0) {
sign = 1;
digitalWrite(RELAY1,HIGH);
digitalWrite(LED_BLUE,LOW);
digitalWrite(LED_GREEN,HIGH);
digitalWrite(LED_RED,LOW);
stepper1.setSpeed(MAX_SPEED);
stepper1.runSpeed();
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
digitalWrite(RELAY1,LOW);
digitalWrite(LED_BLUE,HIGH);
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_RED,LOW);
digitalWrite(ENABLE, HIGH);
}
}
Any help coding this thing to work would be HIGHLY appreciated.
Wiring Diagram attached.

