-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
173 lines (139 loc) · 3.09 KB
/
main.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
#include <Servo.h>
#define servoPin 3
#define hcTrigPin 5
#define hcEchoPin 4
#define leftAxisEnable 5
#define leftAxisFront 7
#define leftAxisBack 8
#define rightAxisEnable 6
#define rightAxisFront 9
#define rightAxisBack 11
Servo servo;
long r_range = 0;
long l_range = 0;
long dst;
bool debug = false;
// При обращении к ней мы получаем расстояние в сантиметрах
long getDistance()
{
long duration, distance;
digitalWrite(hcTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(hcTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(hcTrigPin, LOW);
duration = pulseIn(hcEchoPin, HIGH);
distance = duration / 58;
return distance;
}
void setup()
{
servo.attach(servoPin);
pinMode(hcTrigPin, OUTPUT);
pinMode(hcEchoPin, INPUT);
pinMode(rightAxisEnable, OUTPUT);
pinMode(rightAxisFront, OUTPUT);
pinMode(rightAxisBack, OUTPUT);
pinMode(leftAxisEnable, OUTPUT);
pinMode(leftAxisFront, OUTPUT);
pinMode(leftAxisBack, OUTPUT);
digitalWrite(rightAxisEnable, HIGH);
digitalWrite(leftAxisEnable, HIGH);
Serial.begin(9600);
servo.write(90);
delay(1000);
if(debug)
{
Serial.println("debug on!");
}
}
void loop()
{
if(!debug)
{
// Смотрим направо и измеряем расстояние
servo.write(0);
delay(500);
r_range = getDistance();
delay(500);
// Смотрим налево и измеряем расстояние
servo.write(180);
delay(500);
l_range = getDistance();
delay(500);
servo.write(90);
Serial.println("Right: " + String(r_range));
Serial.println("Left: " + String(l_range));
delay(500);
if(r_range > l_range)
{
Serial.println("carRight");
carRight();
delay(360);
carStop();
}
if(r_range <= l_range)
{
Serial.println("carLeft");
carLeft();
delay(360);
carStop();
}
Serial.println("Go!");
dst = getDistance();
while(dst >= 20)
{
dst = getDistance();
carFront();
}
Serial.println("Stop!");
carStop();
delay(500);
}
else
{
carRight();
delay(2000);
carLeft();
delay(2000);
}
}
void carStop()
{
digitalWrite(rightAxisFront, LOW);
digitalWrite(rightAxisBack, LOW);
digitalWrite(leftAxisFront, LOW);
digitalWrite(leftAxisBack, LOW);
}
void carRight()
{
carStop();
digitalWrite(rightAxisFront, LOW);
digitalWrite(rightAxisBack, HIGH);
digitalWrite(leftAxisFront, HIGH);
digitalWrite(leftAxisBack, LOW);
}
void carLeft()
{
carStop();
digitalWrite(rightAxisFront, HIGH);
digitalWrite(rightAxisBack, LOW);
digitalWrite(leftAxisFront, LOW);
digitalWrite(leftAxisBack, HIGH);
}
void carBack()
{
carStop();
digitalWrite(rightAxisFront, LOW);
digitalWrite(rightAxisBack, HIGH);
digitalWrite(leftAxisFront, LOW);
digitalWrite(leftAxisBack, HIGH);
}
void carFront()
{
carStop();
digitalWrite(rightAxisFront, HIGH);
digitalWrite(rightAxisBack, LOW);
digitalWrite(leftAxisFront, HIGH);
digitalWrite(leftAxisBack, LOW);
}