-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparrow.ino
166 lines (149 loc) · 5.3 KB
/
sparrow.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
163
164
165
166
/*Sparrow 1.2(An autonomous obstacle avoider) Build by Pratim Sarangi, 2014*/
#include <Servo.h> //Include the servo library to use the head servo
#include <NewPing.h>//Include the NewPing library to use the US HC SR-04
#define USTrigger 8//Ultra sonic trigger pin
#define USEcho 9//Ultra sonic echo pin
#define MaxDistance 200 //Maximum distance to be measure by the ultra sonic senesor
int LeftMotorForward = 2;//Pin A1 of L293D motor driver board to digital pin 2 of Arduino
int LeftMotorReverse = 3;//Pin A2 of L293D motor driver board to digital pin 3 of Arduino
int RightMotorForward = 5;//Pin B1 of L293D motor driver board to digital pin 5 of Arduino
int RightMotorReverse = 4;//Pin A2 of L293D motor driver board to digital pin 4 of Arduino
unsigned int Time; // For measuring Distance
unsigned int distance; //Distance read by the sensor
int LeftDistance = 0;
int RightDistance = 0;
int object = 20; //Distance at which the robot should look for another route
NewPing sonar(USTrigger, USEcho, MaxDistance); //Activating the ultra sonic sensor
Servo HeadServo;
void setup ()//This block happens once on startup
{
Serial.begin(9600);
//Here we are setting the pin modes. As we will sending out signals from the pins we set them as outputs
pinMode(LeftMotorForward,OUTPUT);
pinMode(LeftMotorReverse,OUTPUT);
pinMode(RightMotorForward,OUTPUT);
pinMode(RightMotorReverse,OUTPUT);
HeadServo.attach(6);//Head servo is now attached to pin 6
HeadServo.write(90);//Moving the head to the front
delay(700);//Wait for the head to get there
}
void loop()//This block repeats itself while the Arduino is turned on
{
scan(); //Go to the scan function to start the scannig the distance
Serial.println("Front distance = ");
Serial.print(distance);
if(distance > object)//If no obstacle is detected
{
MoveForward(); //Then sparrow moves forward
}
else //If obstacle is detected
{
Halt(); //Stop
FindRoute();//Find for new route
}
}
void MoveForward()
{
Serial.println("");
Serial.println("Moving forward");
digitalWrite(LeftMotorForward,HIGH);
digitalWrite(LeftMotorReverse,LOW);
digitalWrite(RightMotorForward,HIGH);
digitalWrite(RightMotorReverse,LOW);
return;
}
void FindRoute()
{
Halt(); //stop
Serial.println("There's an obstacle!");
Reverse(); //Go backwards
LookLeft(); //Go to subroutine LookLeft
LookRight(); //Go to subroutine LookRight
if ( LeftDistance > RightDistance ) //If there is left side distance is greater than the right side
{
TurnLeft(); //Make a left turn
}
else //If there is right side distance is greater than the left side
{
TurnRight(); //Make a right turn
}
}
void Reverse() //Reverse gear ;-)
{
Serial.println("");
Serial.println("Moving Reverse");
digitalWrite(LeftMotorForward,LOW);
digitalWrite(LeftMotorReverse,HIGH);
digitalWrite(RightMotorForward,LOW);
digitalWrite(RightMotorReverse,HIGH);
delay(900); //Halt the program unless Sparrow reverse himself
Halt();
return;
}
void Halt ()//This function works as Sparrow's Break
{
Serial.println("");
Serial.println("Stopping");
digitalWrite(LeftMotorForward,LOW);
digitalWrite(LeftMotorReverse,LOW);
digitalWrite(RightMotorForward,LOW);
digitalWrite(RightMotorReverse,LOW);
delay(500); //wait after stopping
return;
}
void LookLeft() //Turns Sparrow's head to the left
{
HeadServo.write(160); //Rotating the head
delay(700); //Wait till servo moves the head
scan(); //Look out for obstacle
LeftDistance = distance;
Serial.println("Left distance = ");
Serial.print(distance);
HeadServo.write(90); //Moves the head to the center position
delay(700); //Wait for the servo to get there
return;
}
void LookRight () //Turns Sparrow's head to the right
{
HeadServo.write(20); //Rotating the head
delay(700); //Wait till servo moves the head
scan(); //Look out for obstacle
RightDistance = distance;
Serial.println("Right distance = ");
Serial.print(distance);
HeadServo.write(90); //Moves the head to the center position
delay(700); //wait for the servo to get there
return;
}
void TurnLeft () // Turns sparrow to the left
{
Serial.println("");
Serial.println("Moving left");
digitalWrite(LeftMotorForward,HIGH);
digitalWrite(LeftMotorReverse,LOW);
digitalWrite(RightMotorForward,LOW);
digitalWrite(RightMotorReverse,HIGH);
delay(700); // wait for Sparrow to make the turn
Halt();
return;
}
void TurnRight () // Turns sparrow to the right
{
Serial.println("");
Serial.println("Moving right");
digitalWrite(LeftMotorForward,LOW);
digitalWrite(LeftMotorReverse,HIGH);
digitalWrite(RightMotorForward,HIGH);
digitalWrite(RightMotorReverse,LOW);
delay(700); // Wait for the robot to make the turn
Halt();
return;
}
void scan() //This function determines the distance things are away from the ultrasonic sensor
{
Serial.println("");
Serial.println("Scanning....");
Time = sonar.ping();
distance = Time / US_ROUNDTRIP_CM;
delay(500);
}