Advertisement
djkvidp

Untitled

Dec 29th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. /*
  2. BYJ48 Stepper motor Connect :
  3. ULN2003 --- Arduino Uno
  4. ======== ==============
  5. IN1 >> D8
  6. IN2 >> D9
  7. IN3 >> D10
  8. IN4 >> D11
  9. VCC ... 5V Prefer to use external 5V Source
  10. Gnd
  11.  
  12. ULN2003 --- ESP-12F(WeMos D1 mini)
  13. ======== =======================
  14. IN1 >> GPIO 14 (D5)
  15. IN2 >> GPIO 12 (D6)
  16. IN3 >> GPIO 13 (D7)
  17. IN4 >> GPIO 15 (D8)
  18. VCC(+) >> ... 5V Prefer to use external 5V Source
  19. Gnd(-) >> GND (G)
  20.  
  21. */
  22.  
  23. // Arduino UNO Pins:
  24. //#define IN1 8
  25. //#define IN2 9
  26. //#define IN3 10
  27. //#define IN4 11
  28.  
  29. // ESP-12F(WeMos D1 mini)
  30. #define IN1 14 //GPIO 14
  31. #define IN2 12 //GPIO 12
  32. #define IN3 13 //GPIO 13
  33. #define IN4 15 //GPIO 15
  34.  
  35. const int NBSTEPS = 4095;
  36. const int STEPTIME = 900;
  37. int Step = 0;
  38. boolean Clockwise = true;
  39.  
  40. int arrayDefault[4] = {LOW, LOW, LOW, LOW};
  41.  
  42. int stepsMatrix[8][4] = {
  43. {LOW, LOW, LOW, HIGH},
  44. {LOW, LOW, HIGH, HIGH},
  45. {LOW, LOW, HIGH, LOW},
  46. {LOW, HIGH, HIGH, LOW},
  47. {LOW, HIGH, LOW, LOW},
  48. {HIGH, HIGH, LOW, LOW},
  49. {HIGH, LOW, LOW, LOW},
  50. {HIGH, LOW, LOW, HIGH},
  51. };
  52.  
  53. unsigned long lastTime = 0L;
  54. unsigned long time = 0L;
  55.  
  56. void writeStep(int outArray[4]);
  57. void stepper();
  58. void setDirection();
  59.  
  60. void setup() {
  61. //Arduino UNO
  62. //Serial.begin(9600);
  63.  
  64. //ESP8266
  65. Serial.begin(115200);
  66. Serial.println("Starting...");
  67. pinMode(IN1, OUTPUT);
  68. pinMode(IN2, OUTPUT);
  69. pinMode(IN3, OUTPUT);
  70. pinMode(IN4, OUTPUT);
  71. }
  72.  
  73. void loop() {
  74. unsigned long currentMicros;
  75. int stepsLeft = NBSTEPS;
  76. time = 0;
  77. lastTime = micros();
  78. while (stepsLeft > 0) {
  79. currentMicros = micros();
  80. if (currentMicros - lastTime >= STEPTIME) {
  81. stepper();
  82. time += micros() - lastTime;
  83. lastTime = micros();
  84. stepsLeft--;
  85. }
  86. delay(1);
  87. }
  88. Serial.println(time);
  89. Serial.println("Wait...!");
  90. delay(2000);
  91. Clockwise = !Clockwise;
  92. stepsLeft = NBSTEPS;
  93. }
  94.  
  95. void writeStep(int outArray[4]) {
  96. digitalWrite(IN1, outArray[0]);
  97. digitalWrite(IN2, outArray[1]);
  98. digitalWrite(IN3, outArray[2]);
  99. digitalWrite(IN4, outArray[3]);
  100. }
  101.  
  102. void stepper() {
  103. if ((Step >= 0) && (Step < 8)) {
  104. writeStep(stepsMatrix[Step]);
  105. } else {
  106. writeStep(arrayDefault);
  107. }
  108. setDirection();
  109. }
  110.  
  111. void setDirection() {
  112. (Clockwise == true) ? (Step++) : (Step--);
  113.  
  114. if (Step > 7) {
  115. Step = 0;
  116. } else if (Step < 0) {
  117. Step = 7;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement