MultipleSteppers.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*==========================================================================
  2. * The sketch shows how to move more than one motor.
  3. *
  4. * If more than one motor is moved by one controller all motors will arrive at
  5. * their targets at the same time. E.g., if the motors are part of a
  6. * x/y transport system, the transport move on a straight diagonal line to the
  7. * target coordinates.
  8. *
  9. * The sketch also shows examples how the motor properties are set up
  10. *
  11. * A 1/16 microstep driver is assumed. You probably want to adjust speed,
  12. * acceleration and distances if you are using a driver with another microstep
  13. * resolution.
  14. ===========================================================================*/
  15. #include "TeensyStep.h"
  16. Stepper motor_1(2, 3); //STEP pin = 2, DIR pin = 3
  17. Stepper motor_2(9,10); //STEP pin = 9, DIR pin = 10
  18. Stepper motor_3(14,15); //STEP pin = 14, DIR pin = 15
  19. StepControl controller;
  20. void setup()
  21. {
  22. // setup the motors
  23. motor_1
  24. .setMaxSpeed(50000) // steps/s
  25. .setAcceleration(200000); // steps/s^2
  26. motor_2
  27. .setMaxSpeed(50000) // steps/s
  28. .setAcceleration(200000); // steps/s^2
  29. motor_3
  30. //.setPullInSpeed(300) // steps/s currently deactivated...
  31. .setMaxSpeed(10000) // steps/s
  32. .setAcceleration(50000) // steps/s^2
  33. .setStepPinPolarity(LOW); // driver expects active low pulses
  34. }
  35. void loop()
  36. {
  37. constexpr int spr = 16*200; // 3200 steps per revolution
  38. // lets shake
  39. for(int i = 0; i < 5; i++)
  40. {
  41. motor_1.setTargetRel(spr/4); // 1/4 revolution
  42. controller.move(motor_1);
  43. motor_1.setTargetRel(-spr/4);
  44. controller.move(motor_1);
  45. }
  46. delay(500);
  47. // move motor_1 to absolute position (10 revolutions from zero)
  48. // move motor_2 half a revolution forward
  49. // both motors will arrive their target positions at the same time
  50. motor_1.setTargetAbs(10*spr);
  51. motor_2.setTargetRel(spr/2);
  52. controller.move(motor_1, motor_2);
  53. // now move motor_2 together with motor_3
  54. motor_2.setTargetRel(300);
  55. motor_3.setTargetRel(-800);
  56. controller.move(motor_2, motor_3);
  57. // move all motors back to their start positions
  58. motor_1.setTargetAbs(0);
  59. motor_2.setTargetAbs(0);
  60. motor_3.setTargetAbs(0);
  61. controller.move(motor_1, motor_2, motor_3);
  62. delay(1000);
  63. }