-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject_Avoiding.ino
108 lines (91 loc) · 1.83 KB
/
Object_Avoiding.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
void Object_Avoiding(){
if (command == 'T') {
myservo1.attach(10);
myservo1.write(115);
myservo2.attach(9);
myservo2.write(120);
int distanceR = 0;
int distanceL = 0;
delay(40);
if(distance<=15){
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if(distanceR>=distanceL){
turnRight();
moveStop();
}else {
turnLeft();
moveStop();
}
} else {
moveForward();
}
distance = ReadPing();
}
Read_Data();
}
int lookRight(){
myservo1.write(50);
delay(500);
int distance = ReadPing();
delay(100);
myservo1.write(120);
return distance;
}
int lookLeft(){
myservo1.write(200);
delay(500);
int distance = ReadPing();
delay(100);
myservo1.write(120);
return distance;
delay(100);
}
void moveStop(){
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void moveForward(){
if(!goesForward){
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2){
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
delay(5);
}
}
}
void moveBackward(){
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2){
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
delay(5);
}
}
void turnRight(){
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void turnLeft(){
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
}