Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Definisikan pin yang digunakan
- const int stepPin = 2;
- const int dirPin = 3;
- const int buttonPin = 7;
- const int limitSwitchStartPin = 8;
- const int limitSwitchEndPin = 9;
- // Variable untuk menyimpan status motor dan arah gerakan
- bool motorActivated = false;
- bool moveDirection = true; // true = maju, false = mundur
- bool atStartPosition = false;
- bool atEndPosition = false;
- void setup() {
- // Set pin sebagai OUTPUT untuk step dan direction
- pinMode(stepPin, OUTPUT);
- pinMode(dirPin, OUTPUT);
- // Set pin sebagai INPUT untuk push button dan limit switch dengan pull-up resistor
- pinMode(buttonPin, INPUT_PULLUP);
- pinMode(limitSwitchStartPin, INPUT_PULLUP);
- pinMode(limitSwitchEndPin, INPUT_PULLUP);
- // Mulai dengan arah motor maju
- digitalWrite(dirPin, HIGH);
- }
- void loop() {
- // Baca status limit switch untuk posisi awal
- bool switchStartState = digitalRead(limitSwitchStartPin);
- // Jika limit switch posisi awal aktif dan motor tidak bergerak
- if (switchStartState == LOW && !motorActivated && !atStartPosition) {
- atStartPosition = true; // Motor berada di posisi awal
- }
- // Baca status limit switch untuk posisi akhir
- bool switchEndState = digitalRead(limitSwitchEndPin);
- // Jika limit switch posisi akhir aktif
- if (switchEndState == LOW && atStartPosition && !atEndPosition) {
- atEndPosition = true; // Motor mencapai posisi akhir
- motorActivated = false; // Matikan motor
- moveDirection = false; // Setel arah motor ke mundur
- delay(1000); // Tunda 1 detik untuk stabilisasi
- }
- // Baca status push button
- bool buttonState = digitalRead(buttonPin);
- // Jika push button ditekan dan motor berada di posisi awal
- if (buttonState == LOW && atStartPosition && !atEndPosition) {
- motorActivated = true; // Aktifkan motor
- moveDirection = true; // Setel arah motor ke maju
- }
- // Jika motor telah diaktifkan dan berada di posisi awal atau akhir
- if (motorActivated && (atStartPosition || atEndPosition)) {
- // Lanjutkan gerakan motor ke arah yang ditentukan
- digitalWrite(stepPin, HIGH);
- delayMicroseconds(800); // Sesuaikan dengan kecepatan motor Anda
- digitalWrite(stepPin, LOW);
- delayMicroseconds(800); // Sesuaikan dengan kecepatan motor Anda
- }
- // Reset status motor setelah mencapai posisi akhir
- if (atEndPosition && switchEndState == HIGH) {
- atStartPosition = false;
- atEndPosition = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement