AFMotor_MultiStepper.pde 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // AFMotor_MultiStepper.pde
  2. // -*- mode: C++ -*-
  3. //
  4. // Control both Stepper motors at the same time with different speeds
  5. // and accelerations.
  6. // Requires the AFMotor library (https://github.com/adafruit/Adafruit-Motor-Shield-library)
  7. // Caution, does not work with Adafruit Motor Shield V2
  8. // See https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
  9. // for examples that work with Adafruit Motor Shield V2.
  10. #include <AccelStepper.h>
  11. #include <AFMotor.h>
  12. // two stepper motors one on each port
  13. AF_Stepper motor1(200, 1);
  14. AF_Stepper motor2(200, 2);
  15. // you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
  16. // wrappers for the first motor!
  17. void forwardstep1() {
  18. motor1.onestep(FORWARD, SINGLE);
  19. }
  20. void backwardstep1() {
  21. motor1.onestep(BACKWARD, SINGLE);
  22. }
  23. // wrappers for the second motor!
  24. void forwardstep2() {
  25. motor2.onestep(FORWARD, SINGLE);
  26. }
  27. void backwardstep2() {
  28. motor2.onestep(BACKWARD, SINGLE);
  29. }
  30. // Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
  31. AccelStepper stepper1(forwardstep1, backwardstep1);
  32. AccelStepper stepper2(forwardstep2, backwardstep2);
  33. void setup()
  34. {
  35. stepper1.setMaxSpeed(200.0);
  36. stepper1.setAcceleration(100.0);
  37. stepper1.moveTo(24);
  38. stepper2.setMaxSpeed(300.0);
  39. stepper2.setAcceleration(100.0);
  40. stepper2.moveTo(1000000);
  41. }
  42. void loop()
  43. {
  44. // Change direction at the limits
  45. if (stepper1.distanceToGo() == 0)
  46. stepper1.moveTo(-stepper1.currentPosition());
  47. stepper1.run();
  48. stepper2.run();
  49. }