1
0

motors.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Serial interface:
  3. * - activate / deactivate IR LEDs: 'd'
  4. * - send command to both motors: '+200:50', where
  5. * - '+' or '-' is the direction (mandatory), - CW, + CCW
  6. * - '200' is steps to move
  7. * - '50' is a speed
  8. * which reads as "Move 200 steps at a speed of 50 counterclockwise"
  9. */
  10. // Include the Stepper library:
  11. #include <AccelStepper.h>
  12. // Give the motors control pins names:
  13. #define M1dirPin 4
  14. #define M1stepPin 5
  15. #define M2dirPin 8
  16. #define M2stepPin 9
  17. #define M1diodePin 11
  18. #define M2diodePin 13
  19. // Define number of steps per revolution:
  20. int defaultSpeed = 20;
  21. int ind = 0;
  22. int dir = 1; // rotation direction 1 or -1
  23. int steps = 0; // number of steps to rotate
  24. int spd = 20; // speed in steps per second
  25. long curPos = 0; // curent position of the motor
  26. long newPos = 0; // curent position of the motor
  27. unsigned long currTime = 0; // tracking time since the beginning for blinking
  28. int diodesOn = 0; // diodes on / off
  29. int blinkInterval = 3000; // in millis
  30. int blinkDuration = 500; // in millis
  31. String command; // input command
  32. // Initialize the stepper library on the motor shield:
  33. AccelStepper stepper1 = AccelStepper(1, M1stepPin, M1dirPin);
  34. AccelStepper stepper2 = AccelStepper(1, M2stepPin, M2dirPin);
  35. void setup() {
  36. pinMode(6, OUTPUT); // Enable - if connected
  37. pinMode(10, OUTPUT);
  38. pinMode(M1diodePin, OUTPUT);
  39. pinMode(M2diodePin, OUTPUT);
  40. updateLEDs();
  41. // Set the motor max and default speed (RPMs):
  42. stepper1.setMaxSpeed(600);
  43. stepper1.setSpeed(defaultSpeed);
  44. stepper1.setCurrentPosition(0);
  45. stepper2.setMaxSpeed(600);
  46. stepper2.setSpeed(defaultSpeed);
  47. stepper2.setCurrentPosition(0);
  48. Serial.begin(9600);
  49. }
  50. /*
  51. * Rotate 2 motors simultaneously
  52. */
  53. void rotate(int dir, int steps, int spd) {
  54. curPos = stepper1.currentPosition(); // should be same for both motors
  55. newPos = curPos + (dir * steps);
  56. stepper1.setSpeed(dir * spd);
  57. stepper2.setSpeed(dir * spd);
  58. while ((stepper1.currentPosition() != newPos) || (stepper2.currentPosition() != newPos)) {
  59. updateLEDs();
  60. digitalWrite(6, LOW); // Set Enable low
  61. digitalWrite(10, LOW); // Set Enable low
  62. if (stepper1.currentPosition() != newPos) {
  63. stepper1.runSpeed();
  64. }
  65. if (stepper2.currentPosition() != newPos) {
  66. stepper2.runSpeed();
  67. }
  68. }
  69. }
  70. /*
  71. * Blink the motor LEDs
  72. */
  73. void updateLEDs() {
  74. currTime = millis();
  75. if ((currTime % blinkInterval < blinkDuration) && (diodesOn == 1)) {
  76. digitalWrite(M1diodePin, HIGH); // Set diodes to HIGH
  77. digitalWrite(M2diodePin, HIGH); // Set diodes to HIGH
  78. } else {
  79. digitalWrite(M1diodePin, LOW); // Set diodes to LOW
  80. digitalWrite(M2diodePin, LOW); // Set diodes to LOW
  81. }
  82. }
  83. void loop() {
  84. updateLEDs();
  85. digitalWrite(6, HIGH); // turn off motor drivers
  86. digitalWrite(10, HIGH); // turn off motor drivers
  87. if (Serial.available()) {
  88. char c = Serial.read(); // gets one byte from serial buffer
  89. if (c == '\n') {
  90. //Serial.println(command); // prints string to serial port out
  91. String dirCmd = command.substring(0, 1);
  92. // turn LEDs on
  93. if (dirCmd.equals("d")) {
  94. diodesOn = (diodesOn == 1) ? 0 : 1;
  95. // rotate motors
  96. } else {
  97. // rotation direction
  98. if (dirCmd.equals("+")) {
  99. dir = 1;
  100. } else {
  101. dir = -1;
  102. }
  103. // number of steps to rotate
  104. ind = command.indexOf(':');
  105. steps = command.substring(1, ind).toInt();
  106. // rotation speed
  107. spd = command.substring(ind + 1, command.length()).toInt();
  108. rotate(dir, steps, spd);
  109. }
  110. command = ""; //clears variable for new input
  111. // output of the current position
  112. Serial.println(stepper1.currentPosition());
  113. }
  114. else {
  115. command += c; //makes the string readString
  116. }
  117. }
  118. }