-
Notifications
You must be signed in to change notification settings - Fork 0
/
capteurMvmnt.ino
50 lines (43 loc) · 1.02 KB
/
capteurMvmnt.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//definition des broches utilisées
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;
//definition variables
long duree;
int distance;
int mesureDistance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop(){
//clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
//sets the trigPin on HIGH
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//lecture du echoPin, et retourner le song
duree = pulseIn(echoPin, HIGH);
//calcul distance
distance = duree*0.034/2;
mesureDistance = distance;
if (mesureDistance <= 5){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{ // sinon on éteind la led
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
Serial.print(distance);
Serial.print(" cm ");
Serial.print(duree);
Serial.println(" ms");
}
delay(100);
}