Advertisement
gabbyshimoni

NEMA17withA4899

Jul 30th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. // Define Constants
  2.  
  3. // Connections to A4988
  4. const int dirPin = 2;  // Direction
  5. const int stepPin = 3; // Step
  6.  
  7. // Motor steps per rotation
  8. const int STEPS_PER_REV = 200;
  9.  
  10. void setup() {
  11.  
  12.   // Setup the pins as Outputs
  13.   pinMode(stepPin,OUTPUT);
  14.   pinMode(dirPin,OUTPUT);
  15. }
  16. void loop() {
  17.  
  18.   // Set motor direction clockwise
  19.   digitalWrite(dirPin,HIGH);
  20.  
  21.   // Spin motor one rotation slowly
  22.   for(int x = 0; x < STEPS_PER_REV; x++) {
  23.     digitalWrite(stepPin,HIGH);
  24.     delayMicroseconds(2000);
  25.     digitalWrite(stepPin,LOW);
  26.     delayMicroseconds(2000);
  27.   }
  28.  
  29.   // Pause for one second
  30.   delay(1000);
  31.  
  32.   // Set motor direction counterclockwise
  33.   digitalWrite(dirPin,LOW);
  34.  
  35.   // Spin motor two rotations quickly
  36.   for(int x = 0; x < (STEPS_PER_REV * 2); x++) {
  37.     digitalWrite(stepPin,HIGH);
  38.     delayMicroseconds(1000);
  39.     digitalWrite(stepPin,LOW);
  40.     delayMicroseconds(1000);
  41.   }
  42.  
  43.   // Pause for one second
  44.   delay(1000);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement