-
Notifications
You must be signed in to change notification settings - Fork 1
/
combatRobot2.ino
245 lines (208 loc) · 5.45 KB
/
combatRobot2.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#define m11 12 // rear motor
#define m12 11
#define m21 10 // front motor
#define m22 8
#include <Servo.h>
Servo test; // create servo object to control a servo GUN
Servo myservo2; // create servo object to control a servo TRIG
int pos1 = 0; // variable to store the servo position
int pos2 = 0; // variable to store the servo position
int pos3 = 0; // variable to store the servo position
//int hasObstacle = LOW; // HIGH MEANS NO OBSTACLE
//int obstaclePin = 3; // This is our input pin
int trigPin = 3; //Trig - green Jumper
int echoPin = 4; //Echo - yellow Jumper
long duration, cm;
char str[2],i;
void forward()
{
digitalWrite(m11, HIGH);
digitalWrite(m12, LOW);
digitalWrite(m21, HIGH);
digitalWrite(m22, LOW);
// motor1.run(FORWARD); //
//motor2.run(FORWARD); // BOT MOVE FORWARD
}
void backward()
{
digitalWrite(m11, LOW);
digitalWrite(m12, HIGH);
digitalWrite(m21, LOW);
digitalWrite(m22, HIGH);
//motor1.run(BACKWARD); //
//motor2.run(BACKWARD); // BOT MOVE BACKWARD
}
void left()
{
digitalWrite(m11, LOW);
digitalWrite(m12, HIGH);
delay(100);
digitalWrite(m21, HIGH);
digitalWrite(m22, LOW);
// motor1.run(BACKWARD); //
//motor2.run(FORWARD); // BOT MOVE LEFT
}
void right()
{
digitalWrite(m11, HIGH);
digitalWrite(m12, LOW);
delay(100);
digitalWrite(m21, LOW);
digitalWrite(m22, HIGH);
//motor1.run(FORWARD); //
//motor2.run(BACKWARD); // BOT MOVE RIGHT
}
void Stop()
{
digitalWrite(m11, LOW);
digitalWrite(m12, LOW);
digitalWrite(m21, LOW);
digitalWrite(m22, LOW);
//motor1.run(RELEASE); //
//motor2.run(RELEASE); // BOT STOPS
//motor3.run(RELEASE);
//motor4.run(RELEASE);
}
void gunservo()
{
/*motor3.run(FORWARD); //cleaner clock wise
delay(2000);
motor3.run(BACKWARD); //cleaner anti-clock wise
delay(2000);*/
// for(pos1 = 0; pos1 <= 120; pos1 += 1) // goes from 0 degrees to 180 degrees
// { // in steps of 1 degree
// myservo1.write(pos1); // tell servo to go to position in variable 'pos'
// delay(50); // waits 15ms for the servo to reach the position
// }
// for(pos1 = 120; pos1 >=0; pos1-=1) // goes from 180 degrees to 0 degrees
// {
// myservo1.write(pos1); // tell servo to go to position in variable 'pos'
// delay(50); // waits 15ms for the servo to reach the position
// }
}
void trigservo()
{
for(pos2 = 0; pos2 <= 130; pos2 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos2 = 130; pos2 >=0; pos2-=1) // goes from 180 degrees to 0 degrees
{
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void setup()
{
test.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(6); // attaches the servo on pin 6 to the servo object
//pinMode(obstaclePin, INPUT);
pinMode(m11, OUTPUT);
pinMode(m12, OUTPUT);
pinMode(m21, OUTPUT);
pinMode(m22, OUTPUT);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void motorControl()
{
while(Serial.available())
{
char ch=Serial.read();
str[i++]=ch;
if(str[i-1]=='1')
{
Serial.println("forward");
forward();
i=0;
}
else if(str[i-1]=='2')
{
Serial.println("left");
left();
i=0;
}
else if(str[i-1]=='3')
{
Serial.println("right");
right();
i=0;
}
else if(str[i-1]=='4')
{
Serial.println("backward");
backward();
i=0;
}
else if(str[i-1]=='5')
{
Serial.println("Stop");
Stop();
i=0;
}
else if(str[i-1]=='6')
{
Serial.println("trigservo");
trigservo();
i=0;
}
else if(str[i-1]=='7')
{
Serial.println("gunservo");
gunservo();
i=0;
}
delay(100);
}
}
long distanceSensor()
{
digitalWrite(trigPin,HIGH);
_delay_ms(500);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin,HIGH);
//distance=(duration/2)/29.1;
// convert the time into a distance
cm = (duration/2) / 29.1;
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
return cm;
}
long distance;
void loop()
{
for(pos3 = 0; pos3 <= 120; pos3 += 7) // goes from 0 degrees to 180 degrees
{
distance = distanceSensor();
if(distance>=50 || distance == 0)//move motor when there is no obstacle
{
test.write(pos3); // tell servo to go to position in variable 'pos'
motorControl();
}
else
{
Stop();
trigservo();
delay(15);
}
}
for(pos3 = 120; pos3 >=0; pos3-=7) // goes from 180 degrees to 0 degrees
{
distance = distanceSensor();
if(distance>=50 || distance == 0)
{
test.write(pos3); // tell servo to go to position in variable 'pos'
motorControl();
}
else
{
Stop();
trigservo();
delay(15);
}
}
}