forked from Abhayanil/SSB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino code
More file actions
126 lines (59 loc) · 1.52 KB
/
Arduino code
File metadata and controls
126 lines (59 loc) · 1.52 KB
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
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance,d1,d2,spee;
int enable2 = 12;
int motorPin1 = 6;
int motorPin2 = 7;
int dis()
{
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
Serial.print("Distance: ");
Serial.println(distance);
return distance;
}
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
// Set the pin modes of the above IO pins to OUTPUT
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enable2, OUTPUT);
}
void loop()
{
d1=dis();
delay(1000);
d2=dis();
spee=d1-d2;
Serial.print("Speed: ");
Serial.println(spee);
digitalWrite(LED_BUILTIN, HIGH);
If(spee>20){
digitalWrite(LED_BUILTIN, HIGH);
// Turn the motor in one direction
analogWrite(enable2, 50);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(500);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
delay(500);
}
else
digitalWrite(LED_BUILTIN,LOW);
digitalWrite(motorPin2, LOW);
}