Advertisement
colray

Untitled

Jul 8th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. int button = 8;
  2. int startStepping;
  3. //Put all the pins in an array to make them easy to work with
  4. int pins[] {
  5. 2, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
  6. 3, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
  7. 4, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
  8. 5 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
  9. };
  10.  
  11. //Define the wave drive sequence.
  12. //With the pin (coil) states as an array of arrays
  13. int waveStepCount = 4;
  14. int waveSteps[][4] = {
  15. {HIGH, LOW, LOW, LOW},
  16. {LOW, HIGH, LOW, LOW},
  17. {LOW, LOW, HIGH, LOW},
  18. {LOW, LOW, LOW, HIGH}
  19. };
  20.  
  21. //Define the full step sequence.
  22. //With the pin (coil) states as an array of arrays
  23. int fullStepCount = 4;
  24. int fullSteps[][4] = {
  25. {HIGH, HIGH, LOW, LOW},
  26. {LOW, HIGH, HIGH, LOW},
  27. {LOW, LOW, HIGH, HIGH},
  28. {HIGH, LOW, LOW, HIGH}
  29. };
  30.  
  31.  
  32. //Keeps track of the current step.
  33. //We'll use a zero based index.
  34. int currentStep = 0;
  35.  
  36. //Keeps track of the current direction
  37. //Relative to the face of the motor.
  38. //Clockwise (true) or Counterclockwise(false)
  39. //We'll default to clockwise
  40. bool clockwise = true;
  41.  
  42. bool counterclockwise = false;
  43. // How many steps to go before reversing, set to zero to not bounce.
  44. //int targetSteps = 0; //targetSteps 0 means the motor will just run in a single direction.
  45. //int targetSteps = 2048; //2049 steps per rotation when wave or full stepping
  46. int targetSteps = 3072; //3072 steps for 1.5 rotations when wave or full stepping
  47. //int targetSteps = 4096; //4096 steps per rotation when half stepping
  48.  
  49. void setup() {
  50. // put your setup code here, to run once:
  51. Serial.begin(9600);
  52.  
  53. for (int pin = 0; pin < 4; pin++) {
  54. pinMode(pins[pin], OUTPUT);
  55. digitalWrite(pins[pin], LOW);
  56. }
  57. pinMode(button, INPUT_PULLUP);
  58. }
  59.  
  60. void step(int steps[][4], int stepCount) {
  61. //Then we can figure out what our current step within the sequence from the overall current step
  62. //and the number of steps in the sequence
  63. int currentStepInSequence = currentStep % stepCount;
  64.  
  65. //Figure out which step to use. If clock wise, it is the same is the current step
  66. //if not clockwise, we fire them in the reverse order...
  67. int directionStep = clockwise ? currentStepInSequence : (stepCount - 1) - currentStepInSequence;
  68.  
  69. //Set the four pins to their proper state for the current step in the sequence,
  70. //and for the current direction
  71. for (int pin = 0; pin < 4; pin++) {
  72. digitalWrite(pins[pin], steps[directionStep][pin]);
  73. }
  74. }
  75. void loop() {
  76. startStepping = digitalRead(button);
  77. while (startStepping == 0) {
  78. //Comment out the Serial prints to speed things up
  79. //Serial.print("Step: ");
  80. //Serial.println(currentStep);
  81.  
  82. //Get a local reference to the number of steps in the sequence
  83. //And call the step method to advance the motor in the proper direction
  84.  
  85. //Wave Drive
  86. //int stepCount = waveStepCount;
  87. //step(waveSteps,waveStepCount);
  88.  
  89. //Full Step
  90. int stepCount = fullStepCount;
  91. step(fullSteps, fullStepCount);
  92.  
  93. // Increment the program field tracking the current step we are on
  94. ++currentStep;
  95.  
  96. // If targetSteps has been specified, and we have reached
  97. // that number of steps, reset the currentStep, and reverse directions
  98. if (targetSteps != 0 && currentStep == targetSteps) {
  99. currentStep = 0;
  100. startStepping = 1;
  101. //clockwise = !clockwise;
  102. } else if (targetSteps == 0 && currentStep == (stepCount - 1)) {
  103. // don't reverse direction, just reset the currentStep to 0
  104. // resetting this will prevent currentStep from
  105. // eventually overflowing the int variable it is stored in.
  106. currentStep = 0;
  107. }
  108.  
  109. //2000 microseconds, or 2 milliseconds seems to be
  110. //about the shortest delay that is usable. Anything
  111. //lower and the motor starts to freeze.
  112. //delayMicroseconds(2250);
  113. delay(2);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement