1#include <Servo.h>
2
3Servo servo;
4int angle = 0;
5boolean forward1 = false ;
6String bluetoothRead, Str_x, Str_y, Str_p, Str_s;
7int x ;
8int y ;
9int points;
10int length;
11const int buzzer = 10;
12int s;
13unsigned long previousMillis = millis();
14
15const long interval = 1000;
16
17#define ENA 11
18#define ENB 6
19#define IN1 5
20#define IN2 7
21#define IN3 8
22#define IN4 9
23#define DELAY 20
24
25int carSpeed = (y*3);
26int speed_Coeff = (1 + (x/50));
27
28void setup() {
29 Serial.begin(9600);
30 Serial.flush();
31 pinMode(IN1,OUTPUT);
32 pinMode(IN2,OUTPUT);
33 pinMode(IN3,OUTPUT);
34 pinMode(IN4,OUTPUT);
35 pinMode(ENA,OUTPUT);
36 pinMode(ENB,OUTPUT);
37 pinMode(buzzer, OUTPUT);
38 stop();
39 servo.attach(13);
40 servo.write(angle);
41}
42
43void loop() {
44 int i=0;
45 char commandbuffer[200];
46unsigned long currentMillis = millis();
47
48 if(Serial.available()){
49
50 delay(10);
51
52 while( Serial.available() && i< 199) {
53 commandbuffer[i++] = Serial.read();
54
55
56 }
57 commandbuffer[i++]='\\0';
58 bluetoothRead = (char*)commandbuffer;
59 length = bluetoothRead.length();
60 Serial.println(bluetoothRead);
61
62
63 if(bluetoothRead.substring(0, 1).equals("x")){
64
65 int i=1;
66 while(bluetoothRead.substring(i, i+1) != ("y")){
67 i++;
68 }
69
70 Str_x = bluetoothRead.substring(1, i);
71 x = Str_x.toInt();
72
73 Str_y = bluetoothRead.substring(i+1, length - 2);
74 y = Str_y.toInt();
75
76 Str_p = bluetoothRead.substring(length - 2, length - 1);
77 points = Str_p.toInt();
78
79 Str_s = bluetoothRead.substring(length - 1, length);
80 s = Str_s.toInt();
81
82 i = 1;
83
84 Serial.println(x);
85 Serial.println(y);
86 Serial.println(points);
87 Serial.println(s);
88 Serial.println(length );
89
90 stop();
91 carSpeed = (y*2.5);
92 speed_Coeff = (1 + (x/50));
93
94
95 if(points == 1){
96 forward();
97 }
98 if(points == 0){
99 stop();
100 }
101 if(points == 2){
102 back();
103 }
104
105 if(points == 3){
106 fleft();
107 }
108
109 if(points == 4){
110 fright();
111 }
112
113 if(points == 5){
114 left();
115 }
116
117 if(points == 6){
118 right();
119 }
120
121 if(points == 7){
122 bleft();
123 }
124
125 if(points == 8){
126 bright();
127 }
128
129 if(points == 9){
130 findo();
131 }
132
133
134
135 if (s == 7){
136 play_do();
137 }
138
139 if (s == 6){
140 play_re();
141 }
142
143 if (s == 5){
144 play_mi();
145 }
146
147 if (s == 4){
148 play_fa();
149 }
150
151 if (s == 3){
152 play_sol();
153 }
154
155 if (s == 2){
156 play_la();
157 }
158
159 if (s == 1){
160 play_ci();
161 }
162
163
164
165 if (s == 8){
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
208void servomove(){
209 unsigned long currentMillis = millis();
210 if (currentMillis - previousMillis >= interval) {
211 previousMillis = currentMillis;
212
213 for(angle = 0; angle < 60; angle++)
214 {
215 servo.write(angle);
216 previousMillis = currentMillis;
217 delay(10);
218 }
219
220 for(angle = 60; angle > 0; angle--)
221 {
222 servo.write(angle);
223 }
224 previousMillis = currentMillis;
225 delay(10);
226}
227}
228
229
230void forward(){
231 analogWrite(ENA,carSpeed);
232 analogWrite(ENB,carSpeed);
233 digitalWrite(IN1,LOW);
234 digitalWrite(IN2,HIGH);
235 digitalWrite(IN3,LOW);
236 digitalWrite(IN4,HIGH);
237 delay(25);
238
239}
240
241void forwards(){
242 analogWrite(ENA,carSpeed);
243 analogWrite(ENB,carSpeed);
244 digitalWrite(IN1,LOW);
245 digitalWrite(IN2,HIGH);
246 digitalWrite(IN3,LOW);
247 digitalWrite(IN4,HIGH);
248 delay(25);
249
250}
251
252void back(){
253 analogWrite(ENA,(carSpeed - 20));
254 analogWrite(ENB,(carSpeed - 20));
255 digitalWrite(IN1,HIGH);
256 digitalWrite(IN2,LOW);
257 digitalWrite(IN3,HIGH);
258 digitalWrite(IN4,LOW);
259 delay(25);
260
261}
262
263void backs(){
264 analogWrite(ENA,(carSpeed - 20));
265 analogWrite(ENB,(carSpeed - 20));
266 digitalWrite(IN1,HIGH);
267 digitalWrite(IN2,LOW);
268 digitalWrite(IN3,HIGH);
269 digitalWrite(IN4,LOW);
270 delay(25);
271
272}
273void left(){
274 analogWrite(ENA,x);
275 analogWrite(ENB,x);
276 digitalWrite(IN1,LOW);
277 digitalWrite(IN2,HIGH);
278 digitalWrite(IN3,HIGH);
279 digitalWrite(IN4,LOW);
280 delay(25);
281
282}
283
284void right(){
285 analogWrite(ENA,x);
286 analogWrite(ENB,x);
287 digitalWrite(IN1,HIGH);
288 digitalWrite(IN2,LOW);
289 digitalWrite(IN3,LOW);
290 digitalWrite(IN4,HIGH);
291 delay(25);
292
293}
294
295
296void fright(){
297 analogWrite(ENA,carSpeed);
298 analogWrite(ENB,carSpeed/speed_Coeff);
299 digitalWrite(IN1,LOW);
300 digitalWrite(IN2,HIGH);
301 digitalWrite(IN3,LOW);
302 digitalWrite(IN4,HIGH);
303 delay(25);
304
305}
306
307
308
309void fleft(){
310 analogWrite(ENA,carSpeed/speed_Coeff);
311 analogWrite(ENB,carSpeed);
312 digitalWrite(IN1,LOW);
313 digitalWrite(IN2,HIGH);
314 digitalWrite(IN3,LOW);
315 digitalWrite(IN4,HIGH);
316 delay(25);
317
318}
319
320void bright(){
321 analogWrite(ENB,carSpeed);
322 analogWrite(ENA,carSpeed/speed_Coeff);
323 digitalWrite(IN1,HIGH);
324 digitalWrite(IN2,LOW);
325 digitalWrite(IN3,HIGH);
326 digitalWrite(IN4,LOW);
327 delay(25);
328
329}
330
331
332
333void bleft(){
334 analogWrite(ENB,carSpeed/speed_Coeff);
335 analogWrite(ENA,carSpeed);
336 digitalWrite(IN1,HIGH);
337 digitalWrite(IN2,LOW);
338 digitalWrite(IN3,HIGH);
339 digitalWrite(IN4,LOW);
340 delay(25);
341
342}
343
344void findo(){
345 analogWrite(ENA,90);
346 analogWrite(ENB,90);
347 digitalWrite(IN1,HIGH);
348 digitalWrite(IN2,LOW);
349 digitalWrite(IN3,LOW);
350 digitalWrite(IN4,HIGH);
351
352
353 delay(25) ;
354
355
356 }
357
358
359void stop(){
360 digitalWrite(ENA,LOW);
361 digitalWrite(ENB,LOW);
362
363}
364
365
366void play_do(){
367 tone(buzzer, 533);
368 delay(500);
369 noTone(buzzer);
370}
371
372void play_re(){
373 tone(buzzer, 587);
374 delay(500);
375 noTone(buzzer);
376}
377
378void play_mi(){
379 tone(buzzer, 659);
380 delay(500);
381 noTone(buzzer);
382}
383
384void play_fa(){
385 tone(buzzer, 698);
386 delay(500);
387 noTone(buzzer);
388}
389
390void play_sol(){
391 tone(buzzer, 784);
392 delay(500);
393 noTone(buzzer);
394}
395
396void play_la(){
397 tone(buzzer, 880);
398 delay(500);
399 noTone(buzzer);
400}
401
402void play_ci(){
403 tone(buzzer, 987);
404 delay(500);
405 noTone(buzzer);
406}
407