#include <Stepper.h>
// Define the number of steps per revolution for your stepper motors
const int stepsPerRevolution = 200;
#define PotVal A0
// Define the pins for the stepper motors
const int motor1StepPin = 12;
const int motor1DirPin = 11;
const int motor2StepPin = 5;
const int motor2DirPin = 4;
const int spd = 100; // CHANGE OVERALL SPEED HERE
// Define the pins for the buttons
const int motor1ClockwiseButton = 6;
const int motor1CounterclockwiseButton = 7;
const int motor2ClockwiseButton = 8;
const int motor2CounterclockwiseButton = 9;
// Create instances of the stepper motors
Stepper motor1(stepsPerRevolution, motor1StepPin, motor1DirPin);
Stepper motor2(stepsPerRevolution, motor2StepPin, motor2DirPin);
void setup() {
Serial.begin(9600);
// Set the button pins as inputs
pinMode(motor1ClockwiseButton, INPUT_PULLUP);
pinMode(motor1CounterclockwiseButton, INPUT_PULLUP);
pinMode(motor2ClockwiseButton, INPUT_PULLUP);
pinMode(motor2CounterclockwiseButton, INPUT_PULLUP);
}
void loop() {
int PotVal = map(analogRead(A0), 0, 1023, 0, 100);
Serial.println(PotVal);
// Check the state of the buttons and move the motors accordingly
if (digitalRead(motor1ClockwiseButton) == LOW) {
motor1.setSpeed(PotVal);
motor1.step(1); // Step one step clockwise
}
if (digitalRead(motor1CounterclockwiseButton) == LOW) {
motor1.setSpeed(PotVal);
motor1.step(-1); // Step one step counterclockwise
}
if (digitalRead(motor2ClockwiseButton) == LOW) {
motor2.setSpeed(PotVal);
motor2.step(1); // Step one step clockwise
}
if (digitalRead(motor2CounterclockwiseButton) == LOW) {
motor2.setSpeed(PotVal);
motor2.step(-1); // Step one step counterclockwise
}
}