-
Notifications
You must be signed in to change notification settings - Fork 2
/
sourceCode
214 lines (185 loc) · 3.66 KB
/
sourceCode
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
// MOTOR///////////////////////
#define IN1 A0
#define IN2 A1
#define IN3 A2
#define IN4 A3
#define MAX_SPEED 255
#define MIN_SPEED 0
// COLOR SENSOR/////////////////
#define S0_r 1 // right motor
#define S1_r 2
#define S2_r 3
#define S3_r 4
#define S0_l 5 // left motor
#define S1_l 6
#define S2_l 7
#define S3_l 8
#define sensorOut A4
////////////////////////////////
#define TRIG 9
#define ECHO 10
int STOP=0,FD=1,BD=-1;
int R1,G1,B1,C1,R2,G2,B2,C2;
void motor1Act(int act,int spd);
void motor2Act(int act,int spd);
void forward(int speed);
void backward(int speed);
void rotate(int speed);
void findEnemy();
void colorRead();
int getDistance();
///////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}
void loop() {
while(findEnemy()==true)
{
forward(MAX_SPEED);
}
}
/////////////////////////////////////////////////////////////////////////////////
void colorRead()
{
digitalWrite(S2_r,HIGH);
digitalWrite(S3_r,LOW);
C1= pulseIn(sensorOut,LOW);
Serial.print("C1 ");
Serial.print(C1);
Serial.print("\t");
delay(50);
digitalWrite(S2_r,LOW);
digitalWrite(S3_r,HIGH);
B1= pulseIn(sensorOut,LOW);
Serial.print("B1 ");
Serial.print(B1);
Serial.print("\t");
delay(50);
digitalWrite(S2_r,LOW);
digitalWrite(S3_r,LOW);
R1= pulseIn(sensorOut,LOW);
Serial.print("R1 ");
Serial.print(R1);
Serial.print("\t");
delay(50);
digitalWrite(S2_r,HIGH);
digitalWrite(S3_r,HIGH);
G1= pulseIn(sensorOut,LOW);
Serial.print("G1 ");
Serial.print(G1);
Serial.print("\n");
delay(50);
/////////
digitalWrite(S2_l,HIGH);
digitalWrite(S3_l,LOW);
C2= pulseIn(sensorOut,LOW);
Serial.print("C2 ");
Serial.print(C2);
Serial.print("\t");
delay(50);
digitalWrite(S2_l,LOW);
digitalWrite(S3_l,HIGH);
B2= pulseIn(sensorOut,LOW);
Serial.print("B2 ");
Serial.print(B2);
Serial.print("\t");
delay(50);
digitalWrite(S2_l,LOW);
digitalWrite(S3_l,LOW);
R2= pulseIn(sensorOut,LOW);
Serial.print("R2 ");
Serial.print(R2);
Serial.print("\t");
delay(50);
digitalWrite(S2_l,HIGH);
digitalWrite(S3_l,HIGH);
G2= pulseIn(sensorOut,LOW);
Serial.print("G2 ");
Serial.print(G2);
Serial.print("\n");
delay(50);
}
void motor1Act(int act,int spd)
{
spd = constrain(spd, MIN_SPEED, MAX_SPEED);
switch(act)
{
case 0: //STOP
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
break;
case 1: //FORWARD
digitalWrite(IN1,HIGH);
analogWrite(IN2,255-spd);
break;
case -1: //BACKWARD
digitalWrite(IN1,LOW);
analogWrite(IN2,spd);
break;
}
}
void motor2Act(int act,int spd)
{
spd = constrain(spd, MIN_SPEED, MAX_SPEED);
switch(act)
{
case 0: //STOP
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
break;
case 1: //FORWARD
digitalWrite(IN3,HIGH);
analogWrite(IN4,255-spd);
break;
case -1: //BACKWARD
digitalWrite(IN3,LOW);
analogWrite(IN4,spd);
break;
}
}
void forward(int speed)
{
motor1Act(FD,speed);
motor2Act(FD,speed);
}
void backward(int speed)
{
motor1Act(BD,speed);
motor2Act(BD,speed);
}
void stop()
{
motor1Act(STOP,0)
motor2Act(STOP,0)
}
void rotate(int speed)
{
motor1Act(FD,speed);
motor2Act(BD,speed);
}
bool findEnemy()
{
stop();
while(dis>...)
{
int dis=getDistance();
rotate(50);
}
return true;
}
int getDistance()
{
float Time,dis;
digitalWrite(TRIG,0);
delay(10);
digitalWrite(TRIG,1);
delay(10);
digitalWrite(TRIG,0);
Time=pulseIn(ECHO,1,5000);
dis=Time/2/29.412;
return dis;
}