Advertisement
TolentinoCotesta

StepperDriver

Nov 28th, 2021
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include "SyncDriver.h"
  2.  
  3. #define MOTOR_STEPS 200
  4. #define MOTOR_X_RPM 240
  5. #define MOTOR_Y_RPM 240
  6.  
  7. #define DIR_X 5
  8. #define STEP_X 2
  9. #define DIR_Y 6
  10. #define STEP_Y 3
  11. #define MICROSTEPS 4
  12.  
  13. #define ENABLE 8
  14. #define STOP_PIN 9
  15.  
  16. long X = 5;
  17. long Y = 5;
  18. bool doStop = false;
  19. bool lastPinState = false;
  20.  
  21. BasicStepperDriver stepperX(MOTOR_STEPS, DIR_X, STEP_X);
  22. BasicStepperDriver stepperY(MOTOR_STEPS, DIR_Y, STEP_Y);
  23. SyncDriver controller(stepperX, stepperY);
  24.  
  25. void stopMotor() {
  26.   doStop = !doStop;
  27. }
  28.  
  29. void setup() {
  30.   Serial.begin(115200);
  31.   Serial.println("START");
  32.   pinMode(ENABLE, OUTPUT);
  33.   pinMode(STOP_PIN, INPUT_PULLUP);
  34.   digitalWrite(ENABLE, LOW);
  35.   stepperX.begin(MOTOR_X_RPM, MICROSTEPS);
  36.   stepperY.begin(MOTOR_Y_RPM, MICROSTEPS);
  37. }
  38.  
  39. void loop() {
  40.   // debounce del pulsante (senza usare delay!!!)
  41.   static uint32_t debounceTime = millis();
  42.   uint8_t pinState = (digitalRead(STOP_PIN) == LOW); // PIN a pullup, chiude su GND
  43.   if (millis() - debounceTime > 100 && pinState != lastPinState ) {
  44.     lastPinState = pinState;
  45.     debounceTime = millis();
  46.     doStop = !doStop;
  47.   }
  48.  
  49.   // Inverti la direzione ogni 2 secondi
  50.   static uint32_t changeTime;
  51.   static uint16_t count = 0;  
  52.   if (millis() - changeTime > 2000) {
  53.     changeTime = millis();
  54.     if (count++ % 2)  // Se dispari
  55.       controller.startMove(X * 800, Y * 800);
  56.     else              // Se pari
  57.       controller.startMove(-X * 800, -Y * 800);
  58.   }
  59.  
  60.   // Se la condizione di STOP รจ attiva (pulsante premuto) NON faccio avanzare il motore
  61.   if (!doStop) {
  62.     // motor control loop - send pulse and return how long to wait until next pulse
  63.     unsigned wait_time_micros = controller.nextAction();
  64.   }
  65.  
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement