-
Notifications
You must be signed in to change notification settings - Fork 1
/
carproximity.c
163 lines (88 loc) · 2.4 KB
/
carproximity.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
////Collision Warning System////
const int trigPin = 7;
const int echoPin = 8;
int buzz = 10;
int led = 13;
long duration;
int distance;
// the setup function runs once when you press reset or power the board
//********wheels************
const int leftForward = 2;
const int leftBackward = 3;
const int rightForward = 4;
const int rightBackward = 5;
//***************************
void setup() {
// initialize digital pin 13 as an output.
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(13, OUTPUT); //led
pinMode(10, OUTPUT); //buzzer
Serial.begin(9600);
//************wheels************
pinMode(leftForward , OUTPUT);
pinMode(leftBackward , OUTPUT);
pinMode(rightForward , OUTPUT);
pinMode(rightBackward , OUTPUT);
// ********************************
}
// the loop function runs over and over again forever
void loop()
{
//************//wheels***********
digitalWrite(leftForward , HIGH);
digitalWrite(leftBackward , LOW);
digitalWrite(rightForward , HIGH);
digitalWrite(rightBackward , LOW);
//******************************
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
if(distance <= 50 && distance >= 20)
{
digitalWrite(13, HIGH);
// turn the LED on (HIGH is the voltage level)
}
else
{
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
// wait for a second
}
if(distance <= 20)
{digitalWrite(13,HIGH);
digitalWrite(leftForward , LOW);
digitalWrite(leftBackward , LOW);
digitalWrite(rightForward , LOW);
digitalWrite(rightBackward , LOW);
digitalWrite(10, HIGH);
tone(buzz, 2000);
delay(100);
noTone(buzz);
delay(100);
tone(buzz, 2000);
delay(100);
noTone(buzz);
delay(100);
tone(buzz, 2000);
delay(100);
noTone(buzz);
tone(buzz, 2000);
delay(100);
noTone(buzz);
delay(100);
}
else
{
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
// wait for a second
}
}