#include <Stepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int stepsPerRevolution = 200; // Jumlah langkah per revolusi stepper motor
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // Konfigurasi pin stepper motor
LiquidCrystal_I2C lcd(0x27, 16, 2); // Inisialisasi LCD I2C dengan alamat 0x27
const int buttonPin = 2; // Pin pushbutton
int buttonState = 0; // Variabel untuk menyimpan status pushbutton
int lastButtonState = 0; // Variabel untuk menyimpan status pushbutton sebelumnya
void setup() {
lcd.begin(16,2); // Inisialisasi LCD
lcd.backlight();
pinMode(buttonPin, INPUT); // Set pin pushbutton sebagai input
}
void loop() {
buttonState = digitalRead(buttonPin); // Baca status pushbutton
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
rotateMotor();
}
delay(50); // Debouncing
}
lastButtonState = buttonState;
}
void rotateMotor() {
static int direction = 1; // 1 untuk maju, -1 untuk mundur
myStepper.setSpeed(100); // Set kecepatan motor
lcd.clear(); // Hapus tampilan sebelumnya
lcd.setCursor(0,0);
if (direction == 1) {
lcd.setCursor(6, 0);
lcd.print("Maju");
} else {
lcd.setCursor(6,0);
lcd.print("Mundur");
}
myStepper.step(stepsPerRevolution * direction); // Langkahkan motor
direction *= -1; // Ubah arah setiap kali tombol ditekan
}