-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision_avoidance_robot.ino
123 lines (114 loc) · 4.92 KB
/
Collision_avoidance_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
122
123
/*HELLO EVERYBOBY I AM NIKHIL G MUDAKAVI
IF YOU HAVE SOME KNOWLEDGE ABOUT ARDUIINO YOU CAN ALOS MAKE THIS ROBOT WITHIN FEW HOURS
******I FIXED MY SERVO IN SUCH A MANNER SUCH THAT 180 DEGREE ANGLE OF MY SERVO LIES IN THE LEFT AND RIGHT POSITION IS AT ZERO DEGREE******
YOU
THE CONNECTIONS ARE AS FOLLOWS(ALL MY PINS ON THE SHIELD ARE CONNECTED TO CORRESPONDING PINS ON THE ARDUINO)
MY SERVO WENT INTO PIN 11 OF MY SHIELD
INFRARED SENSOR ON PIN 3
PIN 8,6 12 ARE FOR MY RIGHT MOTOR
PIN 4 ,7,5 ARE FOR MY LEFT MOTOR
THE CODE IS ALMOST SELF EXPLANATORY
*/
#include <Servo.h> //sevo library
int x;
int pos;
Servo myservo;
void setup() {
myservo.attach(11); //attaching my servo to pin to 11 on my board
pinMode(3,INPUT); //setting pin mode for my infrared sensor
pinMode(8,OUTPUT); //setting up pins up for my left motor
pinMode(12,OUTPUT); // ''
pinMode(6,OUTPUT); //''
pinMode(7,OUTPUT); //setting up pins for my right motor
pinMode(4,OUTPUT); //''
pinMode(5,OUTPUT); //''
digitalWrite(6,HIGH); //starting the motors in the beginning
digitalWrite(5,HIGH); //starting the motors in the begininig
}
void loop()
{
myservo.write(90); //setiing up 90 degree position in my servo that is the forward direction
delay(1350); //giving the servo time to reach the desired position
if(digitalRead(3)==0) //if the infrared sensor shows no obstacle in front
{ //then
digitalWrite(12,HIGH);//the next four lines are for moving the robot forward
digitalWrite(8,LOW);
digitalWrite(4,HIGH);
digitalWrite(7,LOW);
for (pos = 90; pos <= 120; pos += 1)//moving the eyes of the robot (to give it a moving eye effect) this for moving the eyes in the left,you have to alter the angles according to you
{
myservo.write(pos);
delay(15);
}
for (pos = 120; pos >= 60; pos -= 1)//moving the eyes of the robot (to give it a moving eye effect) this for moving the eyes in the right,you have to alter the angles acoording to you
{
myservo.write(pos);
delay(15);
}
}
if(digitalRead(3)==1) //if the infrared sensor tells that there is a obstacle in front
{ //then the next 4 line tells the motors to stop
digitalWrite(12,LOW);
digitalWrite(8,LOW);
digitalWrite(4,LOW);
digitalWrite(7,LOW);
delay(15);
myservo.write(180); //this moves the servo to the left
delay(1350); //time given to the servo to move to that positon
if(digitalRead(3)==0) //then in the left position the avaailbility of the object is again checked(0 mean no obastacle)
{ //then the next four lines tell the robot to move left
digitalWrite(12,HIGH);
digitalWrite(8,LOW);
digitalWrite(4,LOW);
digitalWrite(7,HIGH);
delay(150);
}
else //if in the left position also there is a obsatcle then the eyes are moved to the right
{
myservo.write(0); //angle 0 in the servo means moving the eyes the to the right
delay(2700);
if(digitalRead(3)==0) //in the right positon the presence of the object is checked, if there is a no object present
{ //then the following four lines move the robot to the right
digitalWrite(12,LOW);
digitalWrite(8,HIGH);
digitalWrite(4,HIGH);
digitalWrite(7,LOW);
delay(150);
}
else //if there is a obstaclemin the right too
{
myservo.write(90); // then the eyes of the robot are moved back to the initial position thet is straight(and for me 90degree)
delay(1350); //time given to the servo to move to that positionand the following four lines moves the robot backwards
digitalWrite(12,LOW);
digitalWrite(8,HIGH);
digitalWrite(4,LOW);
digitalWrite(7,HIGH);
delay(350);
}
myservo.write(180); //while moving backwards the servo eyes are moved to the left
delay(2700); //time goven to the servo tho move to that position
if(digitalRead(3)==0) // while moving backwards the left position the presence of the obstacle is again checked,when there is no object in the left
//then the next four lines move the robot to the left
{
digitalWrite(12,HIGH);
digitalWrite(8,LOW);
digitalWrite(4,LOW);
digitalWrite(7,HIGH);
delay(150);
}
else
{
myservo.write(0); //if while moving backwards there is a object in the left then the eyes are moved to the right
delay(2700); //time given to the dervo to move to that position
if(digitalRead(3)==0) //the presence of object is checked and if there is not object then following four lines move the robot in the right
{
digitalWrite(12,LOW);
digitalWrite(8,HIGH);
digitalWrite(4,HIGH);
digitalWrite(7,LOW);
delay(150);
}
}
}
}
}