//parameter 
const int putaran = 10; //banyaknya putaran
const int KANAN = -60; //putaran ke kanan
const int KIRI = 20; //putaran ke kiri


const int AtoA = 250; //delay sudut ke sudut //min 50
const int kecPutaran = 700; //kecepatan pergerakan ke sudut //min 400
//untuk kalibrasi atur pada 2 parameter ini, semakin rendah nilainya maka akan semakin kurang akurat

const int buzzerStart = 100; //delay buzzer start 
const int buzzerFinish = 500; //delay buzzer finish

//untuk Pin
const int stepPin = 2; //ke steps pin
const int dirPin = 3; //ke direction pin
const int enablePin = 4; //ke enable pin
const int buttonPin = 5; //ke tombol start       
const int buzzerPin = 6; //ke buzzer finnish

const float degreePerStep = 0.9;  
bool buttonPressed = false;
int calculateSteps(float targetDegree) {
  return (int)(targetDegree / degreePerStep);
}

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buzzerPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW); 
  moveToAngle(0);
}

void loop() {
    if (digitalRead(buttonPin) == LOW) { 
    digitalWrite(buzzerPin, HIGH);
    delay(buzzerStart);
    noTone(buzzerPin);
    if (!buttonPressed) {  
      buttonPressed = true;
        for (int i = 0; i < putaran; i++) {
        moveToAngle(KANAN);
        delay(AtoA);
        moveToAngle(KIRI);
        delay(AtoA);
      }
      moveToAngle(0);
      digitalWrite(buzzerPin, HIGH); 
      delay(buzzerFinish);             
      noTone(buzzerPin);     
    }
  } else {
    buttonPressed = false;
  }
}

void moveToAngle(float targetAngle) {
  static float currentAngle = 0; 
  int stepsToMove = calculateSteps(targetAngle - currentAngle);
  if (stepsToMove > 0) {
    digitalWrite(dirPin, HIGH);  
  } else {
    digitalWrite(dirPin, LOW);   
    stepsToMove = -stepsToMove; 
  }
  for (int i = 0; i < stepsToMove; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(kecPutaran);  
    digitalWrite(stepPin, LOW);
    delayMicroseconds(kecPutaran);
  }
  currentAngle = targetAngle;  
}
A4988