stepper.ino 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Stepper Drivers Functions */
  2. //writeStep write the step given the byte representation of a step on both stepper
  3. //e.g. writeStep(0b00110011);
  4. void writeStep(byte stepByte) {
  5. shiftOut(STP_DATA_PIN, STP_CLOCK_PIN, MSBFIRST, stepByte);
  6. digitalWrite(STP_LATCH_PIN, HIGH);
  7. digitalWrite(STP_LATCH_PIN, LOW);
  8. delayMicroseconds(840); //780 - 800 ms minimum, recommend 840 to 860ms
  9. }
  10. //Set all coils to off and enter standby mode
  11. //to save power and prevent overheating
  12. void standbySteppers() {
  13. writeStep(0b00000000);
  14. }
  15. //Move the robot forward by 8 * rev micro steps
  16. void forward(int rev) {
  17. for (int i = 0; i < rev; i++) {
  18. writeStep(0b00010001);
  19. writeStep(0b00110011);
  20. writeStep(0b00100010);
  21. writeStep(0b01100110);
  22. writeStep(0b01000100);
  23. writeStep(0b11001100);
  24. writeStep(0b10001000);
  25. writeStep(0b10011001);
  26. }
  27. standbySteppers();
  28. }
  29. //Move the robot backward by 8 * rev micro steps
  30. void backward(int rev) {
  31. for (int i = 0; i < rev; i++) {
  32. writeStep(0b10011001);
  33. writeStep(0b10001000);
  34. writeStep(0b11001100);
  35. writeStep(0b01000100);
  36. writeStep(0b01100110);
  37. writeStep(0b00100010);
  38. writeStep(0b00110011);
  39. writeStep(0b00010001);
  40. }
  41. standbySteppers();
  42. }
  43. //Differential offset the robot wheels by +- 8 * rev micro steps, anti-clockwise
  44. void rotateAntiClockwise(int rev) {
  45. for (int i = 0; i < rev; i++) {
  46. writeStep(0b00011001);
  47. writeStep(0b00111000);
  48. writeStep(0b00101100);
  49. writeStep(0b01100100);
  50. writeStep(0b01000110);
  51. writeStep(0b11000010);
  52. writeStep(0b10000011);
  53. writeStep(0b10010001);
  54. }
  55. standbySteppers();
  56. }
  57. //Differential offset the robot wheels by +- 8 * rev micro steps, clockwise
  58. void rotateClockwise(int rev) {
  59. for (int i = 0; i < rev; i++) {
  60. writeStep(0b10010001);
  61. writeStep(0b10000011);
  62. writeStep(0b11000010);
  63. writeStep(0b01000110);
  64. writeStep(0b01100100);
  65. writeStep(0b00101100);
  66. writeStep(0b00111000);
  67. writeStep(0b00011001);
  68. }
  69. standbySteppers();
  70. }