Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. #define DIR_PIN 2
  4. #define STEP_PIN 3
  5.  
  6. const int button = 48; // un bouton sur la broche 48
  7. const int led = 46;
  8.  
  9. Servo monServomoteur;
  10. float degTot=0;
  11.  
  12. void setup() {
  13. monServomoteur.attach(8);
  14. pinMode(button, INPUT_PULLUP);
  15. pinMode(led, OUTPUT);
  16. pinMode(DIR_PIN, OUTPUT);
  17. pinMode(STEP_PIN, OUTPUT);
  18.  
  19. digitalWrite(led, HIGH);
  20. while(digitalRead(button) == LOW){
  21. rotateDeg(-1, .2);
  22. delay(50);
  23. }
  24. degTot = 0;
  25. }
  26.  
  27. void rotate(int steps, float speed){
  28. int dir = (steps > 0)? HIGH:LOW;
  29. steps = abs(steps);
  30.  
  31. digitalWrite(DIR_PIN,dir);
  32.  
  33. float usDelay = (1/speed) * 70;
  34.  
  35. for(int i=0; i < steps; i++){
  36. degTot++;
  37. if(degTot == 3200){
  38. degTot = 0;
  39. delay(500);
  40. }
  41. digitalWrite(STEP_PIN, HIGH);
  42. delayMicroseconds(usDelay);
  43. digitalWrite(STEP_PIN, LOW);
  44. delayMicroseconds(usDelay);
  45. }
  46. }
  47.  
  48.  
  49. void rotateDeg(float deg, float speed){
  50.  
  51. int dir = (deg > 0)? HIGH:LOW;
  52. digitalWrite(DIR_PIN,dir);
  53.  
  54. int steps = abs(deg)*(2/0.225);
  55. float usDelay = (1/speed) * 70;
  56. for(int i=0; i < steps; i++){
  57. degTot++;
  58. if(degTot == 3200){
  59. degTot = 0;
  60. rotate(-3200, speed);
  61. digitalWrite(DIR_PIN,dir);
  62. delay(500);
  63. }
  64. digitalWrite(STEP_PIN, HIGH);
  65. delayMicroseconds(usDelay);
  66. digitalWrite(STEP_PIN, LOW);
  67. delayMicroseconds(usDelay);
  68. }
  69. }
  70.  
  71.  
  72. int position;
  73. void rotateServo(float angleBase, float angleFin){
  74. if(angleBase <= angleFin){
  75. for (position = angleBase; position <= angleFin; position++){
  76. monServomoteur.write(position);
  77. }
  78. }
  79. if(angleBase > angleFin){
  80. for (position = angleBase; position <= angleFin; position--){
  81. monServomoteur.write(position);
  82. }
  83. }
  84. }
  85.  
  86.  
  87. void loop(){
  88. // Fait bouger le bras de 0° à 180°
  89. //rotateServo(0,180);
  90. //for(int position = 0; position < 180; position++){
  91. // monServomoteur.write(position);
  92. // }
  93.  
  94.  
  95. if(digitalRead(button) == HIGH) // test si le bouton a un niveau logique HAUT
  96. {
  97. digitalWrite(led, LOW); //le bouton est relâché, la LED est allumée
  98. }
  99. else // test si le bouton a un niveau logique différent de HAUT (donc BAS)
  100. {
  101. digitalWrite(led, HIGH); //la LED reste éteinte
  102. }
  103.  
  104.  
  105. rotateDeg(40, .2);
  106. delay(500);
  107. rotateDeg(-30, .2);
  108. delay(500);
  109. degTot=0;
  110. //rotateDeg(400, .2);
  111. //delay(1000);
  112. //rotateDeg(-180, .2);
  113. //delay(1000);
  114.  
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement