|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- // ---------------------------------------------------------------- //
- // Arduino Ultrasoninc Sensor HC-SR04
- // Re-writed by Arbi Abdul Jabbaar
- // Using Arduino IDE 1.8.7
- // Using HC-SR04 Module
- // Tested on 17 September 2019
- // ---------------------------------------------------------------- //
-
- #define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
- #define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
-
- #include <Servo.h>
- Servo myServo;
-
-
-
- long duration;
- int distance;
-
- void setup() {
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(9, OUTPUT);
- pinMode(13, OUTPUT);
- digitalWrite(9, HIGH);
- Serial.begin(9600);
- Serial.println("Projet BTS Mathieu N.");
- Serial.println("BTS Ecole Technique EME");
- myServo.attach(10);
- myServo.write(5);
- }
- void loop() {
-
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
-
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
-
- duration = pulseIn(echoPin, HIGH);
-
- distance = duration * 0.034 / 2;
-
-
- if (distance < 30 ) { ouverture(); }
- if (distance > 30) { Serial.println("-"); }
-
- }
-
-
-
- void ouverture(){
-
- Serial.println("Ouverture du bouchon");
- digitalWrite(13, HIGH);
- myServo.write(170);
- delay(1000);
- Serial.println("Fermeture du bouchon");
- digitalWrite(13, LOW);
- delay(2000);
- myServo.write(5);
-
-
-
-
- }
-
-
|