Skip to content

Commit d410dd3

Browse files
Combat Robot using IR sensor
1 parent defe555 commit d410dd3

File tree

1 file changed

+253
-0
lines changed

1 file changed

+253
-0
lines changed

CombatRobot.ino

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

0 commit comments

Comments
 (0)