#include <Bounce2.h>
#include <AccelStepper.h>
// Define pin connections for the first stepper motor
#define DIR_PIN1 23
#define STEP_PIN1 22
#define BUTTON1_FW_PIN 5 // Forward button for motor 1
#define BUTTON1_BW_PIN 6 // Backward button for motor 1
// Define pin connections for the second stepper motor
#define DIR_PIN2 25
#define STEP_PIN2 24
#define BUTTON2_FW_PIN 4 // Forward button for motor 2
#define BUTTON2_BW_PIN 3 // Backward button for motor 2
// Define pins for controlling both motors
#define BUTTON_BOTH_FW_PIN 7 // Forward button for both motors
#define BUTTON_BOTH_BW_PIN 8 // Backward button for both motors
// Create button objects for motor 1
Bounce button1FW = Bounce();
Bounce button1BW = Bounce();
// Create button objects for motor 2
Bounce button2FW = Bounce();
Bounce button2BW = Bounce();
// Create button objects for controlling both motors
Bounce buttonBothFW = Bounce();
Bounce buttonBothBW = Bounce();
// Create stepper motor objects
AccelStepper stepper1(AccelStepper::DRIVER, STEP_PIN1, DIR_PIN1);
AccelStepper stepper2(AccelStepper::DRIVER, STEP_PIN2, DIR_PIN2);
void setup() {
// Set pin modes for motor 1 buttons
pinMode(BUTTON1_FW_PIN, INPUT_PULLUP);
pinMode(BUTTON1_BW_PIN, INPUT_PULLUP);
// Attach motor 1 buttons to Bounce2 library
button1FW.attach(BUTTON1_FW_PIN);
button1FW.interval(5); // Debounce interval
button1BW.attach(BUTTON1_BW_PIN);
button1BW.interval(5); // Debounce interval
// Set pin modes for motor 2 buttons
pinMode(BUTTON2_FW_PIN, INPUT_PULLUP);
pinMode(BUTTON2_BW_PIN, INPUT_PULLUP);
// Attach motor 2 buttons to Bounce2 library
button2FW.attach(BUTTON2_FW_PIN);
button2FW.interval(5); // Debounce interval
button2BW.attach(BUTTON2_BW_PIN);
button2BW.interval(5); // Debounce interval
// Set pin modes for both motors buttons
pinMode(BUTTON_BOTH_FW_PIN, INPUT_PULLUP);
pinMode(BUTTON_BOTH_BW_PIN, INPUT_PULLUP);
// Attach both motors buttons to Bounce2 library
buttonBothFW.attach(BUTTON_BOTH_FW_PIN);
buttonBothFW.interval(5); // Debounce interval
buttonBothBW.attach(BUTTON_BOTH_BW_PIN);
buttonBothBW.interval(5); // Debounce interval
// Set maximum speed and acceleration for the stepper motors
stepper1.setMaxSpeed(1000); // Steps per second
stepper1.setAcceleration(500); // Steps per second^2
stepper2.setMaxSpeed(1000); // Steps per second
stepper2.setAcceleration(500); // Steps per second^2
}
void loop() {
// Update button states for motor 1
button1FW.update();
button1BW.update();
// Update button states for motor 2
button2FW.update();
button2BW.update();
// Update button states for both motors
buttonBothFW.update();
buttonBothBW.update();
// Check if both motors forward button is pressed
if (buttonBothFW.read() == LOW) {
stepper1.setSpeed(500); // Set speed for forward rotation
stepper2.setSpeed(500); // Set speed for forward rotation
stepper1.runSpeed(); // Start rotation for motor 1
stepper2.runSpeed(); // Start rotation for motor 2
}
// Check if both motors backward button is pressed
else if (buttonBothBW.read() == LOW) {
stepper1.setSpeed(-500); // Set speed for backward rotation
stepper2.setSpeed(-500); // Set speed for backward rotation
stepper1.runSpeed(); // Start rotation for motor 1
stepper2.runSpeed(); // Start rotation for motor 2
}
// Check if forward button for motor 1 is pressed
else if (button1FW.read() == LOW) {
stepper1.setSpeed(500); // Set speed for forward rotation
stepper1.runSpeed(); // Start rotation
}
// Check if backward button for motor 1 is pressed
else if (button1BW.read() == LOW) {
stepper1.setSpeed(-500); // Set speed for backward rotation
stepper1.runSpeed(); // Start rotation
}
// Check if forward button for motor 2 is pressed
else if (button2FW.read() == LOW) {
stepper2.setSpeed(500); // Set speed for forward rotation
stepper2.runSpeed(); // Start rotation
}
// Check if backward button for motor 2 is pressed
else if (button2BW.read() == LOW) {
stepper2.setSpeed(-500); // Set speed for backward rotation
stepper2.runSpeed(); // Start rotation
}
// If no button is pressed, stop both motors
else {
stepper1.setSpeed(0); // Stop motor 1
stepper2.setSpeed(0); // Stop motor 2
stepper1.runSpeed(); // Apply the speed change for motor 1
stepper2.runSpeed(); // Apply the speed change for motor 2
}
}