DRV8825 Stepper Motor Driver & Nema17 Stepper Motor with Arduino | KY-023 Dual Axis Joystick

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 พ.ย. 2024

ความคิดเห็น • 1

  • @doit.20
    @doit.20  9 หลายเดือนก่อน

    #include
    // Define the stepper motor connections
    #define STEP_PIN 2
    #define DIR_PIN 3
    #define ENABLE_PIN 4
    #define JOYSTICK_X A0
    #define JOYSTICK_THRESHOLD 50 // Adjust this threshold based on your joystick sensitivity
    // Create an instance of the AccelStepper class
    AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
    void setup() {
    // Set up the motor pins as outputs
    pinMode(STEP_PIN, OUTPUT);
    pinMode(DIR_PIN, OUTPUT);
    pinMode(ENABLE_PIN, OUTPUT);
    // Enable the motor driver
    digitalWrite(ENABLE_PIN, LOW);
    // Set the maximum speed and acceleration
    stepper.setMaxSpeed(1000);
    stepper.setAcceleration(500);
    }
    void loop() {
    // Read the joystick position
    int joystickValue = analogRead(JOYSTICK_X);
    // Determine the direction based on the joystick position
    if (joystickValue < 512 - JOYSTICK_THRESHOLD) {
    stepper.setSpeed(500); // Set speed for one direction
    } else if (joystickValue > 512 + JOYSTICK_THRESHOLD) {
    stepper.setSpeed(-500); // Set speed for the opposite direction
    } else {
    stepper.setSpeed(0); // Stop the motor if joystick is centered
    }
    // Step the motor one step
    stepper.runSpeed();
    // You can add a delay here to control the speed of the motor
    delay(5);
    }