I am controlling a nema17 stepper motor with a drv8825 driver. I would like to be able to use 2 push buttons and 2 limit switches to control it. A click of button 1 should enable the motor and make it rotate clockwise until limit 1 is pressed. When limit 1 is pressed it should stop and disable the motor. Button 2 and switch 2 should do the same thing but counter clockwise.
I have the buttons and switches connected between 5v and a digital pin each.
Any pointers would be greatly appreciated. This is my first time using c++ or an arduino so apologies if i miss something out or am just generally stupid. My setup code is below:
#include <AccelStepper.h>
// Define pin numbers for stepper motor control
#define dirPin 2
#define stepPin 3
#define enPin 10
#define fltPin 11
// Define pin numbers for buttons
#define closePin 22
#define openPin 23
#define cstopPin 24
#define ostopPin 25
// Create an instance of the AccelStepper class
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup() {
// Begin serial communication
Serial.begin(9600);
// Set up pins for stepper motor control
pinMode(enPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(fltPin, INPUT_PULLUP);
// Set up pins for buttons
pinMode (closePin, INPUT);
pinMode (openPin, INPUT);
pinMode (cstopPin, INPUT);
pinMode (ostopPin, INPUT);
// Disable the stepper motor driver initially
digitalWrite(enPin, HIGH);
// Set stepper motor parameters
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(500.0);
}