// defines pins #define stepPin 2 #define dirPin 5 const int stepsPerRevolution = 200; // Número de pasos por revolución (ajusta según tu motor) const float degreesPerStep = 360.0 / stepsPerRevolution; // Grados por paso int currentStep = 0; // Posición actual del motor en pasos int targetStep = 0; // Posición objetivo del motor en pasos void setup() { // Sets the two pins as Outputs pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); Serial.begin(9600); // Para depuración } void loop() { // Read the potentiometer value int potValue = analogRead(A0); // Map the potentiometer value to an angle between 0 and 180 degrees int targetAngle = map(potValue, 0, 1023, 0, 180); // Calculate the target step position targetStep = targetAngle / degreesPerStep; // Ensure the targetStep is within bounds (0 to 180 degrees) targetStep = constrain(targetStep, 0, 180 / degreesPerStep); // Move the motor to the target position moveToPosition(targetStep); // Print debug information Serial.print("Potentiometer Value: "); Serial.print(potValue); Serial.print(" | Target Angle: "); Serial.print(targetAngle); Serial.print(" | Current Step: "); Serial.print(currentStep); Serial.print(" | Target Step: "); Serial.println(targetStep); } void moveToPosition(int targetStep) { if (targetStep > currentStep) { // Move forward digitalWrite(dirPin, HIGH); while (currentStep < targetStep) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); // Ajusta el delay según la velocidad deseada digitalWrite(stepPin, LOW); delayMicroseconds(1000); // Ajusta el delay según la velocidad deseada currentStep++; } } else if (targetStep < currentStep) { // Move backward digitalWrite(dirPin, LOW); while (currentStep > targetStep) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); // Ajusta el delay según la velocidad deseada digitalWrite(stepPin, LOW); delayMicroseconds(1000); // Ajusta el delay según la velocidad deseada currentStep--; } } }