#include <AccelStepper.h>
AccelStepper stepper[] = {
{1, 3,2},
{1, 5,4},
{1, 7,6},
{1, 9,8},
};
constexpr byte stepperCount = sizeof(stepper)/sizeof(stepper[0]);
constexpr byte joystickPin[] = {A0, A1, A2, A3};
void setup() {
Serial.begin(9600);
for ( byte i = 0; i < stepperCount; i++ ) {
stepper[i].setMaxSpeed(500);
stepper[i].setAcceleration(250);
}
}
void loop() {
for ( byte i = 0; i < stepperCount; i++ ) {
int joystickValue = analogRead(joystickPin[i]);
int deviation = joystickValue - 512;
if (abs(deviation) > 10) {
int speed = map(deviation, -512, 512, -500, 500);
Serial.print(speed);
stepper[i].setSpeed(speed);
} else {
stepper[i].setSpeed(0);
}
stepper[i].runSpeed();
}
}