#include <Stepper.h>
// Inisialisasi objek stepper motor dengan 200 langkah per putaran
Stepper motor(200, 2, 3, 4, 5);
// Pin untuk tombol naik/turun
const int buttonPin = 7;
int buttonState = 0;
int lastButtonState = 0;
int motorState = 0; // Variabel untuk melacak status motor stepper (0: berhenti, 1: naik, -1: turun)
void setup() {
// Set kecepatan motor (sesuaikan dengan kebutuhan)
motor.setSpeed(100);
// Inisialisasi pin untuk tombol sebagai input dan aktifkan pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Serial Monitor untuk debugging
Serial.begin(9600);
}
void loop() {
// Baca status tombol
buttonState = digitalRead(buttonPin);
// Handle debouncing
if (buttonState != lastButtonState) {
delay(50); // Tambahkan delay kecil untuk debouncing
buttonState = digitalRead(buttonPin); // Baca ulang status tombol setelah delay
}
// Jika tombol ditekan dan tombol belum pernah ditekan sebelumnya
if (buttonState == LOW && lastButtonState == HIGH) {
// Putar motor stepper sesuai arah yang sesuai (tergantung kondisi)
if (motorState == 0) {
motorState = 1; // Motor mulai bergerak naik
Serial.println("Eskalator bergerak ke atas");
} else {
motorState = 0; // Motor berhenti
Serial.println("Eskalator berhenti");
}
}
// Gerakan motor stepper
if (motorState != 0) {
motor.step(motorState); // 1 langkah naik atau berhenti
}
// Simpan status tombol
lastButtonState = buttonState;
}