-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal-project-code
More file actions
162 lines (127 loc) · 2.61 KB
/
final-project-code
File metadata and controls
162 lines (127 loc) · 2.61 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
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
#define trigPin 10
#define echoPin 9
#define led 13 //green
#define led2 12 //yellow
#define led3 11 //red
//2nd sensor
#define trigPin_two 5
#define echoPin_two 4
#define led_two 8 //green
#define led2_two 7 //yellow
#define led3_two 6 //red
//servo control set uo
#include <Servo.h>
int sensorPin = A0;
int servoPin = 3;
int sensorValue = 0;
int servoGrad = 90;
int tolerance = 40;
Servo myservo;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
//2nd sensor
pinMode(trigPin_two, OUTPUT);
pinMode(echoPin_two, INPUT);
pinMode(led_two, OUTPUT);
pinMode(led2_two, OUTPUT);
pinMode(led3_two, OUTPUT);
//motor
pinMode( sensorPin, INPUT);
myservo.attach( servoPin );
myservo.write( servoGrad );
}
void loop() {
//motor
sensorValue = analogRead(sensorPin);
if ( sensorValue < (512-tolerance) )
{
if (servoGrad < 180) servoGrad++;
}
if ( sensorValue > (512+tolerance) )
{
if (servoGrad > 0) servoGrad--;
}
myservo.write( servoGrad );
delay(1);
//ultrasound
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
//1st led
if (distance <= 10) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led,LOW);
}
//2nd led
if (distance < 20) {
digitalWrite(led2, HIGH);
}
else {
digitalWrite(led2, LOW);
}
//3rd led
if (distance < 40) {
digitalWrite(led3, HIGH);
}
else {
digitalWrite(led3, LOW);
}
//out of range
if (distance > 30 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
//2nd sensor
digitalWrite(trigPin_two, LOW);
delayMicroseconds(2);
digitalWrite(trigPin_two, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin_two, LOW);
duration = pulseIn(echoPin_two, HIGH);
distance = (duration/2) / 29.1;
//1st led
if (distance <= 10) {
digitalWrite(led_two, HIGH);
}
else {
digitalWrite(led_two,LOW);
}
//2nd led
if (distance < 20) {
digitalWrite(led2_two, HIGH);
}
else {
digitalWrite(led2_two, LOW);
}
//3rd led
if (distance < 40) {
digitalWrite(led3_two, HIGH);
}
else {
digitalWrite(led3_two, LOW);
}
//out of range
if (distance > 30 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(10);
}