Hi all,
I have a system where I am controlling two NEMA 17 motors using an Arduino UNO. One motor controls a linear actuator via an UP button and a DOWN button. The other rotates constantly in the same direction when a switch is on. Here's the code:
#include <AccelStepper.h>
// Defines analog-in pin for UP button
#define BUTTON_A_PIN 8
// Defines analog-in pin for DOWN button
#define BUTTON_B_PIN 9
// Defines analog-in pin for spindle ON/OFF switch
#define switchPin 10
// Defines microstep pins for A4988 stepper motor driver
#define MS1_PIN 7
#define MS2_PIN 6
#define MS3_PIN 5
// Defines Linear Actuator stepper
AccelStepper upDownStepper(1,1,2);
// Defines Spindle rotation stepper
AccelStepper spindleStepper(1,3,4);
// Variable to track switch state
int prevSwitchState = HIGH;
// Variables to track button states
int prevButtonAState = HIGH;
int prevButtonBState = HIGH;
void setup() {
pinMode(MS1_PIN, OUTPUT);
pinMode(MS2_PIN, OUTPUT);
pinMode(MS3_PIN, OUTPUT);
// Set microstepping resolution to 1/16 step (MS1=HIGH, MS2=HIGH, MS3=HIGH)
digitalWrite(MS1_PIN, HIGH);
digitalWrite(MS2_PIN, HIGH);
digitalWrite(MS3_PIN, HIGH);
// Set maximum speed and acceleration values for the stepper
spindleStepper.setMaxSpeed(1000);
spindleStepper.setAcceleration(500);
// Set the initial position of the stepper motor
spindleStepper.setCurrentPosition(0);
// Set up the button pin
pinMode(switchPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
// Set up the stepper motor
upDownStepper.setMaxSpeed(1000); // Set the maximum speed of the stepper motor in steps per second
upDownStepper.setAcceleration(1000); // Set the acceleration of the stepper motor in steps per second squared
}
void loop() {
// UP/DOWN ******************************************************************************
// Read the state of button A
int buttonAState = digitalRead(BUTTON_A_PIN);
if(buttonAState == LOW) {
upDownStepper.setSpeed(100);
// Step the motor with a constant speed previously set by setSpeed();
upDownStepper.runSpeed();
}
else {
upDownStepper.stop();
upDownStepper.setCurrentPosition(0);
}
// Read the state of button B
int buttonBState = digitalRead(BUTTON_B_PIN);
if(buttonBState == LOW) {
upDownStepper.setSpeed(-100);
// Step the motor with a constant speed previously set by setSpeed();
upDownStepper.runSpeed();
}
else {
upDownStepper.stop();
upDownStepper.setCurrentPosition(0);
}
//SPINDLE ROTATION ******************************************************************************
// Read the state of the button
int switchState = digitalRead(switchPin);
// Check if the button is pressed and the previous state was not pressed
if (switchState == LOW) {
spindleStepper.setSpeed(500);
// Step the motor with a constant speed previously set by setSpeed();
spindleStepper.runSpeed();
}
else {
spindleStepper.stop();
spindleStepper.setCurrentPosition(0);
}
}
The UP/DOWN motor rotates randomly in several small steps in one direction the several in the other. Completely random. I think it may be a noisy connection with the driver board. Or it may be that my code is buggy. Any help is appreciated.
Cheers,