#include <AccelStepper.h>
// Pin definitions
const int DIR_PIN = 14;
const int STEP_PIN = 12;
const int POT_PIN = 34;
// FIXED: Exactly 360° = 1 full turn (no microstep multiplication)
const int STEPS_PER_REV = 200; // Your stepper's steps per revolution
const long TOTAL_STEPS_360 = STEPS_PER_REV; // EXACTLY 360° = 200 steps
// Speed control
const float MAX_SPEED = 500.0; // Slower for precision
const float MAX_ACCEL = 800.0;
// Create stepper
AccelStepper stepper(1, STEP_PIN, DIR_PIN);
long targetPosition = 0;
int potValue = 0;
int lastPotValue = 0;
void setup() {
Serial.begin(115200);
pinMode(POT_PIN, INPUT);
// Configure stepper
stepper.setMaxSpeed(MAX_SPEED);
stepper.setAcceleration(MAX_ACCEL);
stepper.setCurrentPosition(0);
Serial.println("=== 0-360° EXACT 1 TURN CONTROLLER ===");
Serial.println("Pot 0V = 0° | Pot 3.3V = 360° (1 full turn)");
Serial.print("Steps per 360°: "); Serial.println(TOTAL_STEPS_360);
}
void loop() {
potValue = analogRead(POT_PIN);
// Map EXACTLY 0-4095 → 0-TOTAL_STEPS_360 (1 full turn)
targetPosition = map(potValue, 0, 4095, 0, TOTAL_STEPS_360);
// Anti-jitter: only move on significant change
if (abs(targetPosition - stepper.currentPosition()) > 2 ||
abs(potValue - lastPotValue) > 50) {
stepper.moveTo(targetPosition);
lastPotValue = potValue;
}
stepper.run();
// Status every 300ms
static unsigned long lastPrint = 0;
if (millis() - lastPrint > 300) {
float currentDeg = (stepper.currentPosition() * 360.0) / TOTAL_STEPS_360;
float targetDeg = (targetPosition * 360.0) / TOTAL_STEPS_360;
Serial.print("Pot:");
Serial.print(potValue);
Serial.print(" | ");
Serial.print(currentDeg, 0);
Serial.print("° → ");
Serial.print(targetDeg, 0);
Serial.print("° (");
Serial.print(stepper.currentPosition());
Serial.print("/");
Serial.print(targetPosition);
Serial.println(" steps)");
lastPrint = millis();
}
delay(5);
}Microservo (gripper) D23
D5
D18
D19
D21
D22
MS3=D4
MS2=D17
MS1=D16
Step_Pin=D12