You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.2 KiB

  1. // ---------------------------------------------------------------- //
  2. // ---------------------------------------------------------------- //
  3. #define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
  4. #define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
  5. #include <Servo.h>
  6. Servo myServo;
  7. long duration;
  8. int distance;
  9. void setup() {
  10. pinMode(trigPin, OUTPUT);
  11. pinMode(echoPin, INPUT);
  12. pinMode(9, OUTPUT);
  13. pinMode(13, OUTPUT);
  14. digitalWrite(9, HIGH);
  15. Serial.begin(9600);
  16. Serial.println("Projet BTS Mathieu N.");
  17. Serial.println("BTS Ecole Technique EME");
  18. myServo.attach(10);
  19. myServo.write(5);
  20. }
  21. void loop() {
  22. digitalWrite(trigPin, LOW);
  23. delayMicroseconds(2);
  24. digitalWrite(trigPin, HIGH);
  25. delayMicroseconds(10);
  26. digitalWrite(trigPin, LOW);
  27. duration = pulseIn(echoPin, HIGH);
  28. distance = duration * 0.034 / 2;
  29. if (distance < 30 ) { ouverture(); }
  30. if (distance > 30) { Serial.println("-"); }
  31. }
  32. void ouverture(){
  33. Serial.println("Ouverture du bouchon");
  34. digitalWrite(13, HIGH);
  35. myServo.write(170);
  36. delay(1000);
  37. Serial.println("Fermeture du bouchon");
  38. digitalWrite(13, LOW);
  39. delay(2000);
  40. myServo.write(5);
  41. }