1
2
3#define trigPin 6
4#define echoPin 5
5#define buzzer 2
6float new_delay;
7
8
9void setup()
10{
11 Serial.begin (9600);
12 pinMode(trigPin, OUTPUT);
13 pinMode(echoPin, INPUT);
14 pinMode(buzzer,OUTPUT);
15
16}
17
18
19void loop()
20{
21 long duration, distance;
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/2) / 29.1;
29 new_delay= (distance *3) +30;
30 Serial.print(distance);
31 Serial.println(" cm");
32 if (distance < 50)
33 {
34 digitalWrite(buzzer,HIGH);
35 delay(new_delay);
36 digitalWrite(buzzer,LOW);
37
38 }
39 else
40 {
41 digitalWrite(buzzer,LOW);
42
43 }
44
45 delay(200);
46}
47
rezznor87
10 months ago
Cool project. worked great !