-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubleGarage.ino
175 lines (122 loc) · 2.54 KB
/
doubleGarage.ino
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
164
165
166
167
168
169
170
171
172
173
174
175
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include<avr/power.h>
#endif
int trig = 9;
int echo = 8;
long lecture_echo;
long cm;
int ltrig = 4;
int lecho = 6;
long lecture_lecho;
long lcm;
int x = 0;
#define PIN 3
#define NUMPIXELS 30
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(trig, OUTPUT);
pinMode(ltrig, OUTPUT);
digitalWrite(trig, LOW);
digitalWrite(ltrig, LOW);
pinMode(echo, INPUT);
pinMode(lecho, INPUT);
Serial.begin(9600);
pixels.begin();
pixels.clear();
}
void loop() {
lread();
read();
delay(2000);
lread();
read();
while (lcm < 71) {
rightCar();
}
while (cm < 71) {
leftCar();
}
}
void read() {
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
lecture_echo = pulseIn(echo, HIGH);
cm = lecture_echo / 58;
}
void lread() {
digitalWrite(ltrig, HIGH);
delayMicroseconds(10);
digitalWrite(ltrig, LOW);
lecture_lecho = pulseIn(lecho, HIGH);
lcm = lecture_lecho / 58;
}
void rightCar() {
read();
while (cm < 71 && cm > 29) {
int mapped = map(cm, 30, 70, 0, 30);
pixels.clear();
for (x = 0; x <= 30 - mapped; x++) {
pixels.setPixelColor(x, pixels.Color(0, 255, 255));
read();
}
pixels.show();
read();
}
while (cm < 30) {
for (x = 0; x < 30; x++) {
pixels.setPixelColor(x, pixels.Color(255, 0, 0));
read();
}
pixels.show();
}
while (cm > 70 && cm < 200) {
for (x = 0; x < 30; x++) {
pixels.setPixelColor(x, pixels.Color(0, 255, 0));
read();
}
pixels.show();
}
while (cm >= 200) {
for (x = 0; x < 30; x++) {
pixels.setPixelColor(x, pixels.Color(0, 0 , 0));
read();
}
pixels.show();
}
}
void leftCar() {
lread();
while (lcm < 71 && lcm > 29) {
int mapped = map(lcm, 30, 70, 0, 30);
pixels.clear();
for (x = 0; x <= 30 - mapped; x++) {
pixels.setPixelColor(x, pixels.Color(0, 255, 255));
lread();
}
pixels.show();
lread();
}
while (lcm < 30) {
for (x = 0; x < 30; x++) {
pixels.setPixelColor(x, pixels.Color(255, 0, 0));
lread();
}
pixels.show();
}
while (lcm > 70 && lcm < 200) {
for (x = 0; x < 30; x++) {
pixels.setPixelColor(x, pixels.Color(0, 255, 0));
lread();
}
pixels.show();
}
while (cm >= 200) {
for (x = 0; x < 30; x++) {
pixels.setPixelColor(x, pixels.Color(0, 0 , 0));
lread();
}
pixels.show();
}
}