#include <Stepper.h>
#include <Wire.h> // Library untuk I2C
#include <LiquidCrystal_I2C.h> // Library untuk LCD I2C
const int stepsPerRevolution = 200; // Sesuaikan dengan jumlah langkah per putaran motor Anda
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const int buzzerPin = 7; // Pin untuk buzzer
const int ledPin = 13; // Pin untuk LED built-in pada Arduino
const int buttonPin = 6; // Pin untuk tombol push-button
const int potentiometerPin = A0; // Pin untuk potensiometer
// Inisialisasi objek LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat LCD I2C dan jumlah kolom dan baris
bool clockwiseRotation = true; // Variabel untuk melacak arah putaran motor
bool isRunning = false; // Variabel untuk melacak apakah program berjalan
void setup() {
myStepper.setSpeed(60);
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Aktifkan pull-up resistor untuk tombol push-button
// Inisialisasi LCD I2C
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Keadaan : ");
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
// Tombol ditekan, ubah status isRunning
isRunning = !isRunning;
delay(100); // Debouncing delay
}
if (isRunning) {
// Baca nilai potensiometer untuk mengatur kecepatan stepper motor
int potValue = analogRead(potentiometerPin);
int motorSpeed = map(potValue, 0, 1023, 10, 100); // Potensiometer mengatur kecepatan antara 10 dan 100
myStepper.setSpeed(motorSpeed);
// Jika program sedang berjalan, teruskan putaran motor
if (clockwiseRotation) {
Serial.println("Presisi");
myStepper.step(stepsPerRevolution);
tone(buzzerPin, 5000, 500); // Mengaktifkan buzzer dengan frekuensi 5000 Hz selama 500 ms
digitalWrite(ledPin, HIGH); // Mengaktifkan LED
lcd.setCursor(10, 0);
lcd.print("Hidup");
} else {
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
noTone(buzzerPin); // Mematikan buzzer
digitalWrite(ledPin, LOW); // Matikan LED
lcd.setCursor(10, 0);
lcd.print("CCW");
}
} else {
// Jika program tidak berjalan, matikan stepper motor, buzzer, dan LED
myStepper.step(0);
noTone(buzzerPin); // Mematikan buzzer
digitalWrite(ledPin, LOW); // Matikan LED
lcd.setCursor(10, 0);
lcd.println("Mati");
}
delay(500);
}