Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. /* Simple Stepper Motor Control Exaple Code
  2. *
  3. * by Dejan Nedelkovski, www.HowToMechatronics.com
  4. *
  5. */
  6.  
  7. // Defines pins numbers
  8. const int stepPin = 10;
  9. const int dirPin = 9;
  10. int customDelay,customDelayMapped; // Defines variables
  11.  
  12. void setup() {
  13. // Sets the two pins as Outputs
  14. pinMode(stepPin,OUTPUT);
  15. pinMode(dirPin,OUTPUT);
  16.  
  17. digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
  18. }
  19. void loop() {
  20.  
  21. customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
  22. // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
  23. digitalWrite(stepPin, LOW);
  24. delayMicroseconds(customDelayMapped);
  25. digitalWrite(stepPin, HIGH);
  26. delayMicroseconds(customDelayMapped);
  27. }
  28. // Function for reading the Potentiometer
  29. int speedUp() {
  30. int customDelay = analogRead(A0); // Reads the potentiometer
  31. int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
  32. return newCustom;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement