| @@ -0,0 +1,69 @@ | |||||
| // ---------------------------------------------------------------- // | |||||
| // 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); | |||||
| } | |||||