forked from embeddedlab786/Obstacle_Avoiding_Robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Obstacle_Avoiding_Robot.ino
121 lines (101 loc) · 2.78 KB
/
Obstacle_Avoiding_Robot.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
#include "AFMotor.h"
#include <Servo.h>
#define echopin A4 // echo pin
#define trigpin A5 // Trigger pin
Servo myservo;
const int MOTOR_1 = 1;
const int MOTOR_2 = 2;
const int MOTOR_3 = 3;
const int MOTOR_4 = 4;
AF_DCMotor motor1(MOTOR_1, MOTOR12_64KHZ); // create motor object, 64KHz pwm
AF_DCMotor motor2(MOTOR_2, MOTOR12_64KHZ); // create motor object, 64KHz pwm
AF_DCMotor motor3(MOTOR_3, MOTOR12_64KHZ); // create motor object, 64KHz pwm
AF_DCMotor motor4(MOTOR_4, MOTOR12_64KHZ); // create motor object, 64KHz pwm
//===============================================================================
// Initialization
//===============================================================================
int distance_L, distance_F, distance_R;
long distance;
int set = 20;
void setup() {
Serial.begin(9600); // Initialize serial port
Serial.println("Start");
myservo.attach(10);
myservo.write(90);
pinMode (trigpin, OUTPUT);
pinMode (echopin, INPUT );
motor1.setSpeed(180); // set the motor speed to 0-255
motor2.setSpeed(180);
motor3.setSpeed(180);
motor4.setSpeed(180);
}
//===============================================================================
// Main
//===============================================================================
void loop() {
distance_F = data();
Serial.print("S=");
Serial.println(distance_F);
if (distance_F > set){
Serial.println("Forward");
motor1.run(FORWARD); // turn it on going forward
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
else{hc_sr4();}
}
long data(){
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
distance = pulseIn (echopin, HIGH);
return distance / 29 / 2;
}
void compareDistance(){
if (distance_L > distance_R){
motor1.run(BACKWARD); // turn it on going left
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(350);
}
else if (distance_R > distance_L){
motor1.run(FORWARD); // the other right
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(350);
}
else{
motor1.run(BACKWARD); // the other way
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(300);
motor1.run(BACKWARD); // turn it on going left
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(500);
}
}
void hc_sr4(){
Serial.println("Stop");
motor1.run(RELEASE); // stopped
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
myservo.write(0);
delay(300);
distance_R = data();
delay(100);
myservo.write(170);
delay(500);
distance_L = data();
delay(100);
myservo.write(90);
delay(300);
compareDistance();
}