// Definisikan pin
const int dirPin = 8; // Pin untuk arah
const int stepPin = 9; // Pin untuk langkah
// Jumlah langkah per putaran motor
const int stepsPerRevolution = 200;
void setup() {
// Atur pin sebagai output
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// Putar motor 90 derajat ke kanan
digitalWrite(dirPin, HIGH); // Set arah ke kanan
for(int x = 0; x < stepsPerRevolution / 4; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000); // Atur kecepatan motor
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Jeda 1 detik
// Ulangi putaran 90 derajat ke kanan
for(int x = 0; x < stepsPerRevolution / 4; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Jeda 1 detik
// Putar motor 180 derajat ke kiri
digitalWrite(dirPin, LOW); // Set arah ke kiri
for(int x = 0; x < stepsPerRevolution / 2; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Jeda 1 detik
// Ulangi putaran 180 derajat ke kiri
for(int x = 0; x < stepsPerRevolution / 2; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Jeda 1 detik
}