1
2
3
4
5const int trigPin = 3;
6const int echoPin = 2;
7const int buzzer = 5;
8const int motorPin = 6;
9
10
11long duration;
12int distance;
13int safetyDistance;
14
15
16void setup() {
17pinMode(trigPin, OUTPUT);
18pinMode(echoPin, INPUT);
19pinMode(buzzer, OUTPUT);
20pinMode(motorPin, OUTPUT);
21Serial.begin(9600);
22}
23
24
25void loop() {
26
27digitalWrite(trigPin, LOW);
28delayMicroseconds(2);
29
30
31digitalWrite(trigPin, HIGH);
32delayMicroseconds(10);
33digitalWrite(trigPin, LOW);
34
35
36duration = pulseIn(echoPin, HIGH);
37
38
39distance= duration*0.034/2;
40
41safetyDistance = distance;
42if (safetyDistance <= 30){
43 digitalWrite(buzzer, HIGH);
44 digitalWrite(motorPin, HIGH);
45}
46else{
47 digitalWrite(buzzer, LOW);
48 digitalWrite(motorPin, LOW);
49}
50
51
52Serial.print("Distance: ");
53Serial.println(distance);
54}
55
lara_kanafani
5 months ago
it doesnt work and how do you add multiple connections onto gnd