// =============================================================
// Mesin jahit Shuttlecock Otomatis
// Maju Teknik art
// Versi 1.0a - fitur : Tinggal Start mulai, lcd tidak dipakai
// Dibuat oleh [Eko Andriyanto] - [09 April 2025 / V 1.0a]
// =============================================================
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
// === Inisialisasi LCD ===
LiquidCrystal_I2C lcd(0x27, 16, 2);
// === Pin Stepper Motor ===
#define DIR_A 11
#define STEP_A 12
#define DIR_B 8
#define STEP_B 9
AccelStepper motorA(AccelStepper::DRIVER, STEP_A, DIR_A);
AccelStepper motorB(AccelStepper::DRIVER, STEP_B, DIR_B);
// === Pin Tombol, Potensiometer, Relay, dan Buzzer ===
#define START_BUTTON A1
#define RESET_BUTTON A2
#define POT_PIN A0
#define RELAY_PIN 10
#define BUZZER_PIN 7
// === Pin Microstepping A4988 ===
#define MS1 5
#define MS2 6
#define MS3 4
#define STEPS_PER_REV 1600 // Satu putaran penuh stepper motor A
// === Variabel Global ===
bool running = false;
unsigned long lastStartDebounce = 0;
unsigned long lastResetDebounce = 0;
int kokCount = 0;
void setup() {
// Setup pin
pinMode(START_BUTTON, INPUT_PULLUP);
pinMode(RESET_BUTTON, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Setup microstepping driver A4988
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(MS3, OUTPUT);
digitalWrite(MS1, LOW);
digitalWrite(MS2, HIGH);
digitalWrite(MS3, LOW);
// Setup motor
motorA.setMaxSpeed(1000);
motorA.setAcceleration(500);
motorB.setMaxSpeed(12000);
motorB.setAcceleration(6000);
// Setup LCD
lcd.init();
lcd.backlight();
showInitial();
}
void loop() {
// Tombol START ditekan
if (digitalRead(START_BUTTON) == LOW && millis() - lastStartDebounce > 300) {
lastStartDebounce = millis();
running = true;
runSequence();
}
// Tombol RESET ditekan
if (digitalRead(RESET_BUTTON) == LOW && millis() - lastResetDebounce > 300) {
lastResetDebounce = millis();
kokCount = 0;
showStatus("Reset", kokCount);
delay(1000); // Tampilkan sebentar status reset
showInitial();
}
}
// === Prosedur utama menjalankan urutan tancap bulu ===
void runSequence() {
motorA.setCurrentPosition(0);
motorB.setCurrentPosition(0);
showStatus("Running", kokCount);
digitalWrite(RELAY_PIN, HIGH);
delay(2000);
struct Step {
float angle;
int stepB;
};
Step steps[] = {
{ 0, 1600 }, { 22.5, 3200 }, { 45, 3200 }, { 67.5, 3200 },
{ 90, 3200 }, { 112.5, 3200 }, { 135, 3200 }, { 157.5, 3200 },
{ 180, 3200 }, { 202.5, 3200 }, { 225, 3200 }, { 247.5, 3200 },
{ 270, 3200 }, { 292.5, 3200 }, { 315, 3200 }, { 337.5, 3200 },
{ 360, 1600 }
};
for (int i = 0; i < sizeof(steps) / sizeof(steps[0]); i++) {
moveMotorA(steps[i].angle);
moveMotorB(steps[i].stepB);
}
digitalWrite(RELAY_PIN, LOW);
beep();
kokCount++;
showStatus("Done", kokCount);
}
// === Fungsi Gerakan Motor A Sesuai Sudut ===
void moveMotorA(float angle) {
int steps = (angle / 360.0) * STEPS_PER_REV;
motorA.moveTo(steps);
while (motorA.distanceToGo() != 0) {
updateSpeed();
motorA.runSpeed();
}
}
// === Fungsi Gerakan Motor B ===
void moveMotorB(int steps) {
motorB.moveTo(motorB.currentPosition() + steps);
while (motorB.distanceToGo() != 0) {
updateSpeed();
motorB.runSpeed();
}
}
// === Update Kecepatan dari Potensiometer ===
void updateSpeed() {
int potValue = analogRead(POT_PIN);
int speed = map(potValue, 0, 1023, 1000, 8000);
motorA.setSpeed(speed);
motorB.setSpeed(speed);
}
// === Bunyi Buzzer ===
void beep() {
for (int i = 0; i < 2; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
delay(200);
}
}
// === Tampilkan Status ke LCD ===
void showStatus(String status, int count) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(status);
lcd.setCursor(0, 1);
lcd.print("Kok: ");
lcd.print(count);
}
// === Tampilkan Status Awal ===
void showInitial() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ready");
lcd.setCursor(0, 1);
lcd.print("Kok: ");
lcd.print(kokCount);
}