Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. /* Description:
  2.  *  This project is about a control of the 5V stepper motor with ULN2003 driver module and arduino uno.
  3.  *  When push a button the motor do a step. The motor move only a direction. the amount of steps is configurable from the code
  4.  *  
  5.  *  Circuit:
  6.  *  Arduino       ULN2003
  7.  *   8-------------> IN1
  8.  *   9-------------> IN2
  9.  *   10------------> IN3
  10.  *   11------------> IN4
  11.  *  
  12.  *   7 ------------> button
  13.  */
  14.  
  15. //definition de pins
  16. const int button = 7;
  17. const int motorPin1 = 8;    
  18. const int motorPin2 = 9;    
  19. const int motorPin3 = 10;  
  20. const int motorPin4 = 11;  
  21.                    
  22. //definition variables
  23. int motorSpeed = 1000;   //speed of motor
  24. int stepCounter = 0;     // count of steps
  25. int stepsPerRev = 1;
  26. //int stepsPerRev = 4076;  // steps for a full turn
  27.  
  28. //secuence 1-phase
  29. //const int numSteps = 4;
  30. //const int stepsLookup[4] = { B1000, B0100, B0010, B0001 };
  31.  
  32. //secuence 2-phases
  33. //const int numSteps = 4;
  34. //const int stepsLookup[4] = { B1100, B0110, B0011, B1001 };
  35.  
  36. //secuence half phase
  37. const int numSteps = 8;
  38. const int stepsLookup[8] = { B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 };
  39.  
  40.  
  41. void setup()
  42. {
  43.   //pins output
  44.   pinMode(motorPin1, OUTPUT);
  45.   pinMode(motorPin2, OUTPUT);
  46.   pinMode(motorPin3, OUTPUT);
  47.   pinMode(motorPin4, OUTPUT);
  48.   //pin input
  49.   pinMode(button,INPUT);
  50. }
  51.  
  52. void loop()
  53. {
  54.   if(digitalRead(button)==HIGH){
  55.      for (int i = 0; i < stepsPerRev; i++){
  56.     clockwise();
  57.     delay(motorSpeed);
  58.      }
  59.   }
  60.   delay(200);
  61. }
  62.  
  63. void clockwise()
  64. {
  65.   if (stepCounter >= numSteps) stepCounter = 0;
  66.   setOutput(stepCounter);
  67.   stepCounter++;
  68. }
  69.  
  70.  
  71. void setOutput(int steps)
  72. {
  73.   digitalWrite(motorPin1, bitRead(stepsLookup[steps], 0));
  74.   digitalWrite(motorPin2, bitRead(stepsLookup[steps], 1));
  75.   digitalWrite(motorPin3, bitRead(stepsLookup[steps], 2));
  76.   digitalWrite(motorPin4, bitRead(stepsLookup[steps], 3));
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement