Advertisement
Nusa_Techno

Stepper

Jul 4th, 2024
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | Source Code | 0 0
  1. // Definisikan pin yang digunakan
  2. const int stepPin = 2;
  3. const int dirPin = 3;
  4. const int buttonPin = 7;
  5. const int limitSwitchStartPin = 8;
  6. const int limitSwitchEndPin = 9;
  7.  
  8. // Variable untuk menyimpan status motor dan arah gerakan
  9. bool motorActivated = false;
  10. bool moveDirection = true; // true = maju, false = mundur
  11. bool atStartPosition = false;
  12. bool atEndPosition = false;
  13.  
  14. void setup() {
  15.   // Set pin sebagai OUTPUT untuk step dan direction
  16.   pinMode(stepPin, OUTPUT);
  17.   pinMode(dirPin, OUTPUT);
  18.  
  19.   // Set pin sebagai INPUT untuk push button dan limit switch dengan pull-up resistor
  20.   pinMode(buttonPin, INPUT_PULLUP);
  21.   pinMode(limitSwitchStartPin, INPUT_PULLUP);
  22.   pinMode(limitSwitchEndPin, INPUT_PULLUP);
  23.  
  24.   // Mulai dengan arah motor maju
  25.   digitalWrite(dirPin, HIGH);
  26. }
  27.  
  28. void loop() {
  29.   // Baca status limit switch untuk posisi awal
  30.   bool switchStartState = digitalRead(limitSwitchStartPin);
  31.  
  32.   // Jika limit switch posisi awal aktif dan motor tidak bergerak
  33.   if (switchStartState == LOW && !motorActivated && !atStartPosition) {
  34.     atStartPosition = true; // Motor berada di posisi awal
  35.   }
  36.  
  37.   // Baca status limit switch untuk posisi akhir
  38.   bool switchEndState = digitalRead(limitSwitchEndPin);
  39.  
  40.   // Jika limit switch posisi akhir aktif
  41.   if (switchEndState == LOW && atStartPosition && !atEndPosition) {
  42.     atEndPosition = true; // Motor mencapai posisi akhir
  43.     motorActivated = false; // Matikan motor
  44.     moveDirection = false; // Setel arah motor ke mundur
  45.     delay(1000); // Tunda 1 detik untuk stabilisasi
  46.   }
  47.  
  48.   // Baca status push button
  49.   bool buttonState = digitalRead(buttonPin);
  50.  
  51.   // Jika push button ditekan dan motor berada di posisi awal
  52.   if (buttonState == LOW && atStartPosition && !atEndPosition) {
  53.     motorActivated = true; // Aktifkan motor
  54.     moveDirection = true; // Setel arah motor ke maju
  55.   }
  56.  
  57.   // Jika motor telah diaktifkan dan berada di posisi awal atau akhir
  58.   if (motorActivated && (atStartPosition || atEndPosition)) {
  59.     // Lanjutkan gerakan motor ke arah yang ditentukan
  60.     digitalWrite(stepPin, HIGH);
  61.     delayMicroseconds(800); // Sesuaikan dengan kecepatan motor Anda
  62.     digitalWrite(stepPin, LOW);
  63.     delayMicroseconds(800); // Sesuaikan dengan kecepatan motor Anda
  64.   }
  65.  
  66.   // Reset status motor setelah mencapai posisi akhir
  67.   if (atEndPosition && switchEndState == HIGH) {
  68.     atStartPosition = false;
  69.     atEndPosition = false;
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement