stepper.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }
  28. //Move the robot backward by 8 * rev micro steps
  29. void backward(int rev) {
  30. for (int i = 0; i < rev; i++) {
  31. writeStep(0b10011001);
  32. writeStep(0b10001000);
  33. writeStep(0b11001100);
  34. writeStep(0b01000100);
  35. writeStep(0b01100110);
  36. writeStep(0b00100010);
  37. writeStep(0b00110011);
  38. writeStep(0b00010001);
  39. }
  40. }
  41. //Differential offset the robot wheels by +- 8 * rev micro steps, anti-clockwise
  42. void rotateAntiClockwise(int rev) {
  43. for (int i = 0; i < rev; i++) {
  44. writeStep(0b00011001);
  45. writeStep(0b00111000);
  46. writeStep(0b00101100);
  47. writeStep(0b01100100);
  48. writeStep(0b01000110);
  49. writeStep(0b11000010);
  50. writeStep(0b10000011);
  51. writeStep(0b10010001);
  52. }
  53. }
  54. //Differential offset the robot wheels by +- 8 * rev micro steps, clockwise
  55. void rotateClockwise(int rev) {
  56. for (int i = 0; i < rev; i++) {
  57. writeStep(0b10010001);
  58. writeStep(0b10000011);
  59. writeStep(0b11000010);
  60. writeStep(0b01000110);
  61. writeStep(0b01100100);
  62. writeStep(0b00101100);
  63. writeStep(0b00111000);
  64. writeStep(0b00011001);
  65. }
  66. }