#include <Stepper.h>
#include <Servo.h>
const int stepPin1 = 2;
const int dirPin1 = 5;
const int stepPin2 = 3;
const int dirPin2 = 6;
const int stepPin3 = 4;
const int dirPin3 = 7;
Stepper stepper1(200, stepPin1, dirPin1);
Stepper stepper2(200, stepPin2, dirPin2);
Stepper stepper3(200, stepPin3, dirPin3);
const int buttonPinGreen = 13;
const int buttonPinRed = 12;
int operationRunning = 0;
int speed1 = 20000000000000;
int speed2 = 20000000000000;
int speed3 = 20000000000000;
Servo servoMotor;
int servoPosition = 0;
int servoIncrement = 10;
void setup() {
pinMode(buttonPinGreen, INPUT);
pinMode(buttonPinRed, INPUT);
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(stepPin3, OUTPUT);
pinMode(dirPin3, OUTPUT);
servoMotor.attach(9);
}
void loop() {
int buttonStateGreen = digitalRead(buttonPinGreen);
int buttonStateRed = digitalRead(buttonPinRed);
if (buttonStateRed == LOW) {
stopOperation();
}
if (buttonStateGreen == HIGH) {
if (!operationRunning) {
startOperation();
}
}
if (operationRunning) {
servoPosition += servoIncrement;
if (servoPosition >= 160 || servoPosition <= 0) {
servoIncrement = -servoIncrement;
}
servoMotor.write(servoPosition);
delay(1);
}
}
void startOperation() {
operationRunning = 1;
servoMotor.write(0);
// Stepper 1 maju 20 langkah
digitalWrite(dirPin1, HIGH);
for (int i = 0; i < 20; i++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(speed1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(speed1);
}
delay(100);
servoMotor.write(90);
// Stepper 1 mundur 20 langkah
digitalWrite(dirPin1, LOW);
for (int i = 0; i < 20; i++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(speed1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(speed1);
}
// Stepper 2 maju 10 langkah
digitalWrite(dirPin2, HIGH);
for (int i = 0; i < 10; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(speed2);
digitalWrite(stepPin2, LOW);
delayMicroseconds(speed2);
}
// Stepper 3 maju 30 langkah
digitalWrite(dirPin3, HIGH);
for (int i = 0; i < 30; i++) {
digitalWrite(stepPin3, HIGH);
delayMicroseconds(speed3);
digitalWrite(stepPin3, LOW);
delayMicroseconds(speed3);
}
// Stepper 3 mundur 30 langkah
digitalWrite(dirPin3, LOW);
for (int i = 0; i < 30; i++) {
digitalWrite(stepPin3, HIGH);
delayMicroseconds(speed3);
digitalWrite(stepPin3, LOW);
delayMicroseconds(speed3);
}
// Stepper 2 mundur 10 langkah
digitalWrite(dirPin2, LOW);
for (int i = 0; i < 10; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(speed2);
digitalWrite(stepPin2, LOW);
delayMicroseconds(speed2);
}
servoMotor.write(0);
}
void stopOperation() {
if (operationRunning) {
operationRunning = 0;
servoMotor.write(0);
}
}