Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SyncDriver.h"
- #define MOTOR_STEPS 200
- #define MOTOR_X_RPM 240
- #define MOTOR_Y_RPM 240
- #define DIR_X 5
- #define STEP_X 2
- #define DIR_Y 6
- #define STEP_Y 3
- #define MICROSTEPS 4
- #define ENABLE 8
- #define STOP_PIN 9
- long X = 5;
- long Y = 5;
- bool doStop = false;
- bool lastPinState = false;
- BasicStepperDriver stepperX(MOTOR_STEPS, DIR_X, STEP_X);
- BasicStepperDriver stepperY(MOTOR_STEPS, DIR_Y, STEP_Y);
- SyncDriver controller(stepperX, stepperY);
- void stopMotor() {
- doStop = !doStop;
- }
- void setup() {
- Serial.begin(115200);
- Serial.println("START");
- pinMode(ENABLE, OUTPUT);
- pinMode(STOP_PIN, INPUT_PULLUP);
- digitalWrite(ENABLE, LOW);
- stepperX.begin(MOTOR_X_RPM, MICROSTEPS);
- stepperY.begin(MOTOR_Y_RPM, MICROSTEPS);
- }
- void loop() {
- // debounce del pulsante (senza usare delay!!!)
- static uint32_t debounceTime = millis();
- uint8_t pinState = (digitalRead(STOP_PIN) == LOW); // PIN a pullup, chiude su GND
- if (millis() - debounceTime > 100 && pinState != lastPinState ) {
- lastPinState = pinState;
- debounceTime = millis();
- doStop = !doStop;
- }
- // Inverti la direzione ogni 2 secondi
- static uint32_t changeTime;
- static uint16_t count = 0;
- if (millis() - changeTime > 2000) {
- changeTime = millis();
- if (count++ % 2) // Se dispari
- controller.startMove(X * 800, Y * 800);
- else // Se pari
- controller.startMove(-X * 800, -Y * 800);
- }
- // Se la condizione di STOP รจ attiva (pulsante premuto) NON faccio avanzare il motore
- if (!doStop) {
- // motor control loop - send pulse and return how long to wait until next pulse
- unsigned wait_time_micros = controller.nextAction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement