/* Example sketch to control a stepper motor with TB6600 stepper motor driver,
AccelStepper library and Arduino: acceleration and deceleration.
More info: https://www.makerguides.com */
// Include the AccelStepper library:
#include "AccelStepper.h"
// Define stepper motor connections and motor interface type.
// Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
int pinX = A0;
int valorXjoystick;
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
Serial.begin(9600);
pinMode(pinX, INPUT);
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(8000);
stepper.setAcceleration(1000);
}
void loop() {
valorXjoystick = analogRead(pinX);
Serial.println(valorXjoystick);
if (valorXjoystick>600){
stepper.moveTo(1000);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
}
else if (valorXjoystick<450){
// Move back to zero:
stepper.moveTo(-1000);
stepper.runToPosition();
}
else {
// Move back to zero:
stepper.moveTo(0);
stepper.runToPosition();
}
}