Advertisement
Guest User

stepper

a guest
Apr 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. // testing a stepper motor with a Pololu A4988 driver board or equivalent
  2. // on an Uno the onboard led will flash with each step
  3. // this version uses delay() to manage timing
  4.  
  5. byte directionPin = 9;
  6. byte stepPin = 8;
  7. int numberOfSteps = 100;
  8. byte ledPin = 13;
  9. int pulseWidthMicros = 20;  // microseconds
  10. int millisbetweenSteps = 250; // milliseconds - or try 1000 for slower steps
  11.  
  12.  
  13. void setup() {
  14.  
  15.   Serial.begin(9600);
  16.   Serial.println("Starting StepperTest");
  17.   digitalWrite(ledPin, LOW);
  18.  
  19.   delay(2000);
  20.  
  21.   pinMode(directionPin, OUTPUT);
  22.   pinMode(stepPin, OUTPUT);
  23.   pinMode(ledPin, OUTPUT);
  24.  
  25.  
  26.   digitalWrite(directionPin, HIGH);
  27.   for(int n = 0; n < numberOfSteps; n++) {
  28.     digitalWrite(stepPin, HIGH);
  29.     delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
  30.     digitalWrite(stepPin, LOW);
  31.    
  32.     delay(millisbetweenSteps);
  33.    
  34.     digitalWrite(ledPin, !digitalRead(ledPin));
  35.   }
  36.  
  37.   delay(3000);
  38.  
  39.  
  40.   digitalWrite(directionPin, LOW);
  41.   for(int n = 0; n < numberOfSteps; n++) {
  42.     digitalWrite(stepPin, HIGH);
  43.     // delayMicroseconds(pulseWidthMicros); // probably not needed
  44.     digitalWrite(stepPin, LOW);
  45.    
  46.     delay(millisbetweenSteps);
  47.    
  48.     digitalWrite(ledPin, !digitalRead(ledPin));
  49.   }
  50. }
  51.  
  52. void loop() {
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement