-
Notifications
You must be signed in to change notification settings - Fork 1
/
kolichka.ino.ino
156 lines (132 loc) · 2.69 KB
/
kolichka.ino.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
#include <Servo.h>
#include "Ultrasonic.h"
int motorA1 = 12;
int motorA2 = 11;
int motorB1 = 10;
int motorB2 = 9;
int servoPin = 13;
int utp = 5;
int uep = 4;
int enA = 8;
int enB = 7;
Servo servo;
Ultrasonic usonic(uep, utp);
int rightDistance = 0;
int leftDistance = 0;
void setup()
{
Serial.begin(9600);
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
servo.attach(servoPin);
pinMode(uep, INPUT);
pinMode(utp, OUTPUT);
analogWrite(enA, 240);
analogWrite(enB, 255);
}
void loop()
{
rightDistance = 0;
leftDistance = 0;
triggerParktronic();
long duration = pulseIn(uep, HIGH);
long cm = microsecondsToCentimeters(duration);
if(cm <= 10)
{
stopAndBackward();
rightDistance = lookRight();
delay(1500);
leftDistance = lookLeft();
delay(1500);
if(rightDistance > leftDistance)
{
right();
delay(1500);
}
else if(leftDistance > rightDistance)
{
left();
delay(1500);
}
else if(leftDistance == rightDistance)
{
right();
delay(1500);
}
}
forward();
servo.write(90);
delay(150);
}
void forward()
{
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, HIGH);
digitalWrite(motorB2, LOW);
}
void backward()
{
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, HIGH);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, HIGH);
}
void left()
{
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, HIGH);
digitalWrite(motorB2, LOW);
}
void right()
{
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, LOW);
}
void stopMotors()
{
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, LOW);
}
void triggerParktronic() // function to send sound waves
{
digitalWrite(utp, LOW);
delayMicroseconds(2);
digitalWrite(utp, HIGH);
delayMicroseconds(10);
digitalWrite(utp, LOW);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void stopAndBackward()
{
stopMotors();
backward();
delay(1000);
stopMotors();
}
int lookRight()
{
servo.write(150);
triggerParktronic();
long duration = pulseIn(uep, HIGH);
long cm = microsecondsToCentimeters(duration);
return cm;
}
int lookLeft()
{
servo.write(30);
triggerParktronic();
long duration = pulseIn(uep, HIGH);
long cm = microsecondsToCentimeters(duration);
return cm;
}