-
Notifications
You must be signed in to change notification settings - Fork 2
/
obstacle.ino
162 lines (140 loc) · 3.39 KB
/
obstacle.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
# Auto detect text files and perform LF normalization
* text=auto
#include <Servo.h> //Servo motor library. This is standard library
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library
//our L298N control pins
const int in3 = 11;
const int in4 = 10;
int enb = 6;
//sensor pins
#define trig_pin 1 //analog input 1
#define echo_pin 0 //analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo motor on which ultrasonic sensro is mounted
Servo servo_motor_front;//our servo on which controls the right and left motion
float servo_motor_pos = 60;
float servo_motor_front_pos = 156.5;
void setup() {
Serial.begin(9600);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enb, OUTPUT);
servo_motor.attach(3); //our servo pin
servo_motor_front.attach(4);//fromt wheel servo motor pin
servo_motor_front.write(servo_motor_pos);
servo_motor.write(servo_motor_pos);//recheck the angle
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
analogWrite(enb, 200);
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
Serial.println(distance);
if (distance <= 20) {
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distance >= distanceLeft) {
turnRight();
moveStop();
}
else {
turnLeft();
moveStop();
}
}
else {
moveForward();
}
distance = readPing();
}
int lookRight() {
servo_motor.write(180);
servo_motor_front.write(servo_motor_pos);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(servo_motor_pos);
return distance;
}
int lookLeft() {
servo_motor.write(80);
servo_motor_front.write(servo_motor_pos);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(servo_motor_pos);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 250;
}
return cm;
}
void moveStop() {
servo_motor.write(servo_motor_pos);
servo_motor_front.write(servo_motor_front_pos);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void moveForward() {
servo_motor.write(servo_motor_pos);
if (!goesForward) {
goesForward = true;
servo_motor_front.write(servo_motor_front_pos);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
}
void moveBackward() {
servo_motor.write(servo_motor_pos);
goesForward = false;
servo_motor_front.write(servo_motor_front_pos);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void turnRight() {
servo_motor.write(servo_motor_pos);
servo_motor_front.write(180);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(1000);
servo_motor_front.write(servo_motor_front_pos);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(1000);
}
void turnLeft() {
servo_motor.write(servo_motor_pos);
servo_motor_front.write(90);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(1000);
servo_motor_front.write(servo_motor_front_pos);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(1000);
}