Advertisement
Ollivier

PorteServo2BP

Jul 16th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. //===============================================================
  2. // Exploration Hardware
  3. // Ouvrir une porte avec un servomoteur
  4. // Ollivier JULLIEN
  5. // Juillet 2020
  6. //===============================================================
  7.  
  8. //===============================================================
  9. // Ouverture d'une porte avec indicateurs lumineux et sonores
  10. // Commande par deux boutons poussoirs en appui constant
  11. // (ça s'ouvre ou ça se ferme que si on reste appuyé, sinon ça s'arrête)
  12. // Action de la porte par servo FS90
  13. //===============================================================
  14.  
  15. //===============================================================
  16. // Matériel :
  17. // 2 BP (pour moi 1 DFR0029W et 1 DFR0029B)
  18. // 2 LEDs blanches 5 mm
  19. // 2 LEDs rouges 5 mm
  20. // 1 LED bleue 5 mm
  21. // 1 LED verte 5 mm
  22. // 6 résistances 220 ohm
  23. // 1 buzzer SV12-5
  24. // 1 mini-servomoteur FS90
  25. // 1 Arduino Uno
  26. // Câblage
  27. //===============================================================
  28.  
  29. //===============================================================
  30. // ATTENTION :
  31. // La porte au repos est ouvert, donc langle Max est l'angle de la porte fermée
  32. // et l'angle Mini est l'angle de la porte ouverte
  33. // rien ne vous oblige à vous y tenir
  34. //===============================================================
  35.  
  36. #include <Servo.h>
  37.  
  38. Servo maPorte;
  39.  
  40. const int rouge = 2;
  41. const int bleu = 3;
  42. const int blanc = 4;
  43. const int lampe = 5;
  44. const int buzzer = 6;
  45. const int rouge2 = 7;
  46. const int ok = 8;
  47. const int porte = 10;
  48. const int veille = 12;
  49.  
  50. int rotationServo = 0;
  51. int evol = 1;
  52. long tempo;
  53. int tempoP = 40;
  54. int angleMax = 93;
  55. int angleMini = 3;
  56. bool etatRouge = true;
  57. bool buzzFerme = true;
  58. bool buzzOuvert = false;
  59.  
  60. //===============================================================
  61.  
  62. void setup() {
  63.   Serial.begin(9600);
  64.   maPorte.attach(porte);
  65.   pinMode(bleu, INPUT);
  66.   pinMode(blanc, INPUT);
  67.   pinMode(rouge, OUTPUT);
  68.   pinMode(rouge2, OUTPUT);
  69.   pinMode(lampe, OUTPUT);
  70.   pinMode(buzzer, OUTPUT);
  71.   pinMode(ok, OUTPUT);
  72.   pinMode(veille, OUTPUT);
  73.   rotationServo = angleMax;
  74.   digitalWrite(lampe, 0);
  75. }
  76.  
  77. //===============================================================
  78.  
  79. void loop() {
  80.  
  81.   if (digitalRead(bleu) == true) {
  82.     actionBoutBleu ();
  83.   } else if (digitalRead(blanc) == true) {
  84.     actionBoutBlanc ();
  85.   } else {
  86.     rien ();
  87.   }
  88.  
  89.   if ((digitalRead(bleu) == 1 && rotationServo < (angleMax - 2)) || (digitalRead(blanc) == 1 && rotationServo > (angleMini + 2))) {
  90.     flashAlt();
  91.   }
  92.  
  93.   if (rotationServo < (angleMini + 65)) {
  94.     digitalWrite(lampe, true);
  95.   } else {
  96.     digitalWrite(lampe, false);
  97.   }
  98.  
  99.   if (rotationServo < (angleMini + 2)) {
  100.     allumeOK ();
  101.   } else {
  102.     eteintOK ();
  103.   }
  104.  
  105.   if (rotationServo > (angleMax - 2)) {
  106.     allumeVeilleuse();
  107.   } else {
  108.     eteintVeilleuse();
  109.   }
  110.   actionPorte ();
  111. }
  112.  
  113. //===============================================================
  114.  
  115. void flashAlt() {
  116.   if (millis() - tempo >= 250) {
  117.     etatRouge = !etatRouge;
  118.     digitalWrite(rouge, etatRouge);
  119.     digitalWrite(rouge2, !etatRouge);
  120.     tempo = millis();
  121.     tone (buzzer, 329, 100);
  122.   }
  123. }
  124.  
  125. void allumeVeilleuse(){
  126.   digitalWrite(veille, true);
  127.     if (buzzFerme == false) {
  128.       delay(150);
  129.       tone (buzzer, 261, 500);
  130.       delay(600);
  131.       tone (buzzer, 261, 700);
  132.       buzzFerme = true;
  133.     }
  134. }
  135.  
  136. void eteintVeilleuse(){
  137.   digitalWrite(veille, false);
  138.   buzzFerme = false;
  139. }
  140.  
  141. void allumeOK () {
  142.   digitalWrite(ok, true);
  143.     if (buzzOuvert == false) {
  144.       delay(150);
  145.       tone (buzzer, 493, 75);
  146.       delay(150);
  147.       tone (buzzer, 493, 75);
  148.       buzzOuvert = true;
  149.     }
  150. }
  151.  
  152. void eteintOK () {
  153.   digitalWrite(ok, false);
  154.   buzzOuvert = false;
  155. }
  156.  
  157. void actionPorte () {
  158.   maPorte.write(rotationServo);
  159.   delay(tempoP);
  160. }
  161.  
  162. void actionBoutBleu () {
  163.   if (rotationServo < angleMax) {
  164.     rotationServo += evol;
  165.   }
  166. }
  167.  
  168. void actionBoutBlanc () {
  169.   if (rotationServo > angleMini) {
  170.     rotationServo -= evol;
  171.   }
  172. }
  173.  
  174. void rien () {
  175.   digitalWrite(rouge, false);
  176.   digitalWrite(rouge2, false);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement