Skip to content

Commit 23467dd

Browse files
Combat Robot using Ultrasonic Sensor
1 parent d410dd3 commit 23467dd

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

combatRobot2.ino

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
#define m11 12 // rear motor
2+
#define m12 11
3+
#define m21 10 // front motor
4+
#define m22 8
5+
6+
#include <Servo.h>
7+
8+
Servo test; // create servo object to control a servo GUN
9+
Servo myservo2; // create servo object to control a servo TRIG
10+
11+
int pos1 = 0; // variable to store the servo position
12+
int pos2 = 0; // variable to store the servo position
13+
int pos3 = 0; // variable to store the servo position
14+
15+
//int hasObstacle = LOW; // HIGH MEANS NO OBSTACLE
16+
//int obstaclePin = 3; // This is our input pin
17+
int trigPin = 3; //Trig - green Jumper
18+
int echoPin = 4; //Echo - yellow Jumper
19+
long duration, cm;
20+
21+
char str[2],i;
22+
23+
void forward()
24+
{
25+
digitalWrite(m11, HIGH);
26+
digitalWrite(m12, LOW);
27+
digitalWrite(m21, HIGH);
28+
digitalWrite(m22, LOW);
29+
// motor1.run(FORWARD); //
30+
//motor2.run(FORWARD); // BOT MOVE FORWARD
31+
}
32+
33+
void backward()
34+
{
35+
digitalWrite(m11, LOW);
36+
digitalWrite(m12, HIGH);
37+
digitalWrite(m21, LOW);
38+
digitalWrite(m22, HIGH);
39+
//motor1.run(BACKWARD); //
40+
//motor2.run(BACKWARD); // BOT MOVE BACKWARD
41+
42+
}
43+
44+
void left()
45+
{
46+
digitalWrite(m11, LOW);
47+
digitalWrite(m12, HIGH);
48+
delay(100);
49+
digitalWrite(m21, HIGH);
50+
digitalWrite(m22, LOW);
51+
52+
// motor1.run(BACKWARD); //
53+
//motor2.run(FORWARD); // BOT MOVE LEFT
54+
}
55+
56+
void right()
57+
{
58+
digitalWrite(m11, HIGH);
59+
digitalWrite(m12, LOW);
60+
delay(100);
61+
digitalWrite(m21, LOW);
62+
digitalWrite(m22, HIGH);
63+
64+
//motor1.run(FORWARD); //
65+
//motor2.run(BACKWARD); // BOT MOVE RIGHT
66+
}
67+
68+
void Stop()
69+
{
70+
digitalWrite(m11, LOW);
71+
digitalWrite(m12, LOW);
72+
digitalWrite(m21, LOW);
73+
digitalWrite(m22, LOW);
74+
75+
//motor1.run(RELEASE); //
76+
//motor2.run(RELEASE); // BOT STOPS
77+
//motor3.run(RELEASE);
78+
//motor4.run(RELEASE);
79+
}
80+
void gunservo()
81+
{
82+
83+
/*motor3.run(FORWARD); //cleaner clock wise
84+
delay(2000);
85+
motor3.run(BACKWARD); //cleaner anti-clock wise
86+
delay(2000);*/
87+
// for(pos1 = 0; pos1 <= 120; pos1 += 1) // goes from 0 degrees to 180 degrees
88+
// { // in steps of 1 degree
89+
// myservo1.write(pos1); // tell servo to go to position in variable 'pos'
90+
// delay(50); // waits 15ms for the servo to reach the position
91+
// }
92+
// for(pos1 = 120; pos1 >=0; pos1-=1) // goes from 180 degrees to 0 degrees
93+
// {
94+
// myservo1.write(pos1); // tell servo to go to position in variable 'pos'
95+
// delay(50); // waits 15ms for the servo to reach the position
96+
// }
97+
98+
}
99+
100+
void trigservo()
101+
{
102+
103+
104+
for(pos2 = 0; pos2 <= 130; pos2 += 1) // goes from 0 degrees to 180 degrees
105+
{ // in steps of 1 degree
106+
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
107+
delay(15); // waits 15ms for the servo to reach the position
108+
}
109+
for(pos2 = 130; pos2 >=0; pos2-=1) // goes from 180 degrees to 0 degrees
110+
{
111+
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
112+
delay(15); // waits 15ms for the servo to reach the position
113+
}
114+
115+
}
116+
117+
void setup()
118+
{
119+
test.attach(9); // attaches the servo on pin 9 to the servo object
120+
myservo2.attach(6); // attaches the servo on pin 6 to the servo object
121+
122+
//pinMode(obstaclePin, INPUT);
123+
124+
pinMode(m11, OUTPUT);
125+
pinMode(m12, OUTPUT);
126+
pinMode(m21, OUTPUT);
127+
pinMode(m22, OUTPUT);
128+
Serial.begin(9600);
129+
pinMode(trigPin, OUTPUT);
130+
pinMode(echoPin, INPUT);
131+
}
132+
133+
void motorControl()
134+
{
135+
while(Serial.available())
136+
{
137+
char ch=Serial.read();
138+
str[i++]=ch;
139+
140+
if(str[i-1]=='1')
141+
{
142+
Serial.println("forward");
143+
forward();
144+
i=0;
145+
}
146+
147+
else if(str[i-1]=='2')
148+
{
149+
Serial.println("left");
150+
left();
151+
i=0;
152+
}
153+
154+
else if(str[i-1]=='3')
155+
{
156+
Serial.println("right");
157+
right();
158+
i=0;
159+
}
160+
161+
else if(str[i-1]=='4')
162+
{
163+
Serial.println("backward");
164+
backward();
165+
i=0;
166+
}
167+
168+
else if(str[i-1]=='5')
169+
{
170+
Serial.println("Stop");
171+
Stop();
172+
i=0;
173+
}
174+
else if(str[i-1]=='6')
175+
{
176+
Serial.println("trigservo");
177+
trigservo();
178+
i=0;
179+
}
180+
181+
else if(str[i-1]=='7')
182+
{
183+
Serial.println("gunservo");
184+
gunservo();
185+
i=0;
186+
}
187+
188+
delay(100);
189+
}
190+
}
191+
long distanceSensor()
192+
{
193+
digitalWrite(trigPin,HIGH);
194+
_delay_ms(500);
195+
digitalWrite(trigPin, LOW);
196+
197+
duration=pulseIn(echoPin,HIGH);
198+
//distance=(duration/2)/29.1;
199+
// convert the time into a distance
200+
cm = (duration/2) / 29.1;
201+
202+
Serial.print(cm);
203+
Serial.print("cm");
204+
Serial.println();
205+
206+
delay(100);
207+
return cm;
208+
}
209+
210+
long distance;
211+
void loop()
212+
{
213+
for(pos3 = 0; pos3 <= 120; pos3 += 7) // goes from 0 degrees to 180 degrees
214+
{
215+
distance = distanceSensor();
216+
if(distance>=50 || distance == 0)//move motor when there is no obstacle
217+
{
218+
test.write(pos3); // tell servo to go to position in variable 'pos'
219+
motorControl();
220+
}
221+
else
222+
{
223+
Stop();
224+
trigservo();
225+
delay(15);
226+
}
227+
}
228+
for(pos3 = 120; pos3 >=0; pos3-=7) // goes from 180 degrees to 0 degrees
229+
{
230+
distance = distanceSensor();
231+
if(distance>=50 || distance == 0)
232+
{
233+
test.write(pos3); // tell servo to go to position in variable 'pos'
234+
motorControl();
235+
}
236+
else
237+
{
238+
Stop();
239+
trigservo();
240+
delay(15);
241+
242+
}
243+
}
244+
245+
}

0 commit comments

Comments
 (0)