Advertisement
Guest User

MECA

a guest
May 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1.  
  2. // Pour piloter un moteur pas à pas, 3 "commandes" sur le driver :
  3. // STEP qui permet de dire quand le moteur bouge d'un pas
  4. // DIR qui permet d'indiquer dans quel sens ton moteur tourne au prochain pas
  5. // ENABLE ( EN ) qui permet d'activer ou pas le driver.
  6.  
  7. // NE PAS TOUCHER
  8.  
  9. // Définit les numéros de pins
  10. // Moteur M1 sur X de la carte CNC shield
  11. const int stepPinM1 = 2;
  12. const int dirPinM1 = 5;
  13.  
  14. // Moteur M2 sur X de la carte CNC shield
  15. const int stepPinM2 = 3;
  16. const int dirPinM2 = 6;
  17.  
  18. // Moteur M3 sur X de la carte CNC shield
  19. const int stepPinM3 = 4;
  20. const int dirPinM3 = 7;
  21.  
  22. const int enabled = 8;
  23.  
  24. // 7 combinaisons possibles = M1 ou M2 ou M3 ou M1M2 ou M1M3 ou M2M3 ou M1M2M3
  25. int nbreAleatoire;
  26.  
  27.  
  28. void setup() {
  29.  
  30.  
  31. // NE PAS TOUCHER
  32. // Définit les broches de sortie des moteurs
  33. pinMode(stepPinM1, OUTPUT);
  34. pinMode(dirPinM1, OUTPUT);
  35.  
  36. pinMode(stepPinM2, OUTPUT);
  37. pinMode(dirPinM2, OUTPUT);
  38.  
  39. pinMode(stepPinM3, OUTPUT);
  40. pinMode(dirPinM3, OUTPUT);
  41.  
  42. pinMode(enabled, OUTPUT);
  43.  
  44. digitalWrite(enabled, LOW);
  45.  
  46. }
  47. void loop() {
  48.  
  49. //1 tour complet d'un moteur NEMA = 200 pas/tour
  50. //Gestion Moteur par moteur
  51. //MOTEUR M1
  52. int temps1 = random(0, 20000); // DÉFINIR LE TEMPS MINI ET MAX
  53. int nb_croq1 = random(50, 200); //NBRE DE PAS
  54.  
  55. //MOTEUR M2
  56. int temps2 = random(0, 20000); // DÉFINIR LE TEMPS MINI ET MAX
  57. int nb_croq2 = random(50, 200); //NBRE DE PAS
  58.  
  59. //MOTEUR M3
  60. int temps3 = random(0, 20000); // DÉFINIR LE TEMPS MINI ET MAX
  61. int nb_croq3 = random(50, 200); //NBRE DE PAS
  62.  
  63. // Gestion Combo moteur
  64. //M1M2
  65. int temps4 = random(0, 20000); // DÉFINIR LE TEMPS MINI ET MAX
  66. int nb_croq4 = random(50, 200); //NBRE DE PAS
  67. //M1M3
  68. int temps5 = random(0, 20000); // DÉFINIR LE TEMPS MINI ET MAX
  69. int nb_croq5 = random(50, 200); //NBRE DE PAS
  70. //M2M3
  71. int temps6 = random(0, 20000); // DÉFINIR LE TEMPS MINI ET MAX
  72. int nb_croq6 = random(50, 200); //NBRE DE PAS
  73. //M1M2M3
  74. int temps7 = random(0, 20000); // DÉFINIR LE TEMPS MINI ET MAX
  75. int nb_croq7 = random(50, 200); //NBRE DE PAS
  76.  
  77.  
  78. // 7 combinaisons possibles = M1 M2 M3 M1M2 M1M3 M2M3 M1M2M3
  79.  
  80. nbreAleatoire = random(1,7);
  81.  
  82. // si on passe les 3 lignes suivantes en commentaire, les moteurs changent de sens
  83. digitalWrite(dirPinM1, HIGH);
  84. digitalWrite(dirPinM2, HIGH);
  85. digitalWrite(dirPinM3, HIGH);
  86.  
  87. // Moteur 1
  88. if (nbreAleatoire == 1){
  89.  
  90. for (int x = 0; x < nb_croq1; x++) {
  91. digitalWrite(stepPinM1, HIGH);
  92. delay(1);
  93. digitalWrite(stepPinM1, LOW);
  94.  
  95. delay(1);
  96.  
  97. }
  98. delay(temps1);
  99.  
  100. }
  101. // Moteur 2
  102. if (nbreAleatoire == 2){
  103.  
  104. for (int x = 0; x < nb_croq2; x++) {
  105. digitalWrite(stepPinM2, HIGH);
  106. delay(1);
  107. digitalWrite(stepPinM2, LOW);
  108.  
  109. delay(1);
  110. }
  111. delay(temps2);
  112.  
  113. }
  114.  
  115. // Moteur 3
  116. if (nbreAleatoire == 3){
  117.  
  118. for (int x = 0; x < nb_croq3; x++) {
  119. digitalWrite(stepPinM3, HIGH);
  120. delay(1);
  121. digitalWrite(stepPinM3, LOW);
  122.  
  123. delay(1);
  124. }
  125. delay(temps3);
  126.  
  127. }
  128.  
  129. // Moteur 1 & 2
  130. if (nbreAleatoire == 4){
  131.  
  132. for (int x = 0; x < nb_croq4; x++) {
  133. digitalWrite(stepPinM1, HIGH);
  134. digitalWrite(stepPinM2, HIGH);
  135. delay(1);
  136. digitalWrite(stepPinM1, LOW);
  137. digitalWrite(stepPinM2, LOW);
  138.  
  139. delay(1);
  140. }
  141. delay(temps4);
  142.  
  143. }
  144.  
  145. // Moteur 1 & 3
  146. if (nbreAleatoire == 5){
  147.  
  148. for (int x = 0; x < nb_croq5; x++) {
  149. digitalWrite(stepPinM1, HIGH);
  150. digitalWrite(stepPinM3, HIGH);
  151. delay(1);
  152. digitalWrite(stepPinM1, LOW);
  153. digitalWrite(stepPinM3, LOW);
  154.  
  155. delay(1);
  156. }
  157. delay(temps5);
  158.  
  159. }
  160.  
  161. // Moteur 2 & 3
  162. if (nbreAleatoire == 6){
  163.  
  164. for (int x = 0; x < nb_croq6; x++) {
  165. digitalWrite(stepPinM2, HIGH);
  166. digitalWrite(stepPinM3, HIGH);
  167. delay(1);
  168. digitalWrite(stepPinM2, LOW);
  169. digitalWrite(stepPinM3, LOW);
  170.  
  171. delay(1);
  172. }
  173.  
  174. delay(temps6);
  175.  
  176. }
  177.  
  178. // Moteur 1 & 2 & 3
  179. if (nbreAleatoire == 7){
  180.  
  181. for (int x = 0; x < nb_croq7; x++) {
  182. digitalWrite(stepPinM1, HIGH);
  183. digitalWrite(stepPinM2, HIGH);
  184. digitalWrite(stepPinM3, HIGH);
  185. delay(1);
  186. digitalWrite(stepPinM1, LOW);
  187. digitalWrite(stepPinM2, LOW);
  188. digitalWrite(stepPinM3, LOW);
  189.  
  190. delay(1);
  191. }
  192. delay(temps7);
  193.  
  194. }
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement