-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raulinho.ino
346 lines (304 loc) · 6.86 KB
/
Raulinho.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <Ultrasonic.h>
#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>
SoftwareSerial mp3Serial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;
#define redPin A1
#define greenPin A3
#define bluePin A2
#define motorEsq1 6
#define motorEsq2 7
#define motorEsqVel 5
#define motorDir1 8
#define motorDir2 A0
#define motorDirVel 3
#define TRIGGER_PIN 2
#define ECHO_PIN 4
Ultrasonic ultrasonic (TRIGGER_PIN, ECHO_PIN);
//SoftwareSerial mySerial(A5, A4); // RX, TX
char opcao;
int distancia;
long randNum, tempo;
void setup() {
// put your setup code here, to run once:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(motorEsq1, OUTPUT);
pinMode(motorEsq2, OUTPUT);
pinMode(motorEsqVel, OUTPUT);
pinMode(motorDir1, OUTPUT);
pinMode(motorDir2, OUTPUT);
pinMode(motorDirVel, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(5));
mp3Serial.begin(9600);
myMP3.begin(mp3Serial, true);
//Serial.println("Setting volume to max");
myMP3.volume(30);
corOlhos(0, 0, 255);
moveStop();
}
void loop() {
// put your main code here, to run repeatedly:
//corOlhos(0, 0, 255);
if (Serial.available() > 0) {
opcao = Serial.read();
Serial.println(opcao);
switch (opcao) {
case 'F':
corOlhos(0, 0, 255);
myMP3.stop();
moveStop();
setSpeedM(150, 150);
moveF();
break;
case 'S':
corOlhos(0, 0, 255);
myMP3.stop();
moveStop();
break;
case 'L':
corOlhos(0, 0, 255);
myMP3.stop();
moveStop();
setSpeedM(0, 150);
moveL();
break;
case 'R':
corOlhos(0, 0, 255);
myMP3.stop();
moveStop();
setSpeedM(150, 0);
moveR();
break;
case 'B':
corOlhos(0, 0, 255);
myMP3.stop();
moveStop();
setSpeedM(150, 150);
moveB();
break;
case '1':
//nome
corOlhos(0, 0, 255);
moveStop();
myMP3.play(1);
break;
case '2':
//Dançar
corOlhos(0, 0, 255);
moveStop();
dancar();
break;
case '3':
//Brincar
corOlhos(0, 0, 255);
moveStop();
myMP3.play(8);
break;
case '4':
//Andar Sozinho
corOlhos(0, 0, 255);
moveStop();
myMP3.play(3);
delay(6000);
do {
autonomo();
opcao = Serial.read();
if (opcao != -1)
break;
} while (true);
break;
case '5':
//Musica
corOlhos(0, 0, 255);
moveStop();
musica();
break;
case '6':
//Amigo
corOlhos(0, 0, 255);
moveStop();
myMP3.play(6);
delay(5000);
corOlhos(255, 0, 0);
break;
case '7':
//Cor Olhos
corOlhos(0, 0, 255);
moveStop();
myMP3.play(5);
mudaOlhos();
break;
case '8':
//Sono
corOlhos(0, 0, 255);
moveStop();
myMP3.play(2);
delay(8000);
corOlhos(0, 0, 0);
delay(100);
do {
myMP3.loop(11);
opcao = Serial.read();
} while (opcao == '8');
break;
case '9':
//Dorminhoco
corOlhos(0, 0, 255);
moveStop();
myMP3.play(4);
break;
case '0':
//Fome
corOlhos(0, 0, 255);
moveStop();
myMP3.play(9);
break;
case 'X':
corOlhos(0, 0, 255);
myMP3.stop();
moveStop();
do {
autonomo();
opcao = Serial.read();
} while (opcao != 'F' && opcao != 'B' && opcao != 'L' && opcao && 'R');
break;
}
}
}
void moveF () {
digitalWrite(motorEsq1, HIGH);
digitalWrite(motorEsq2, LOW);
digitalWrite(motorDir1, HIGH);
digitalWrite(motorDir2, LOW);
}
void moveB () {
digitalWrite(motorEsq1, LOW);
digitalWrite(motorEsq2, HIGH);
digitalWrite(motorDir1, LOW);
digitalWrite(motorDir2, HIGH);
}
void moveR () {
digitalWrite(motorEsq1, HIGH);
digitalWrite(motorEsq2, LOW);
digitalWrite(motorDir1, LOW);
digitalWrite(motorDir2, LOW);
}
void moveL () {
digitalWrite(motorEsq1, LOW);
digitalWrite(motorEsq2, LOW);
digitalWrite(motorDir1, HIGH);
digitalWrite(motorDir2, LOW);
}
void moveStop() {
digitalWrite(motorEsq1, LOW);
digitalWrite(motorEsq2, LOW);
digitalWrite(motorDir1, LOW);
digitalWrite(motorDir2, LOW);
}
void autonomo() {
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
tempo = pulseIn(ECHO_PIN, HIGH);
// Calculating the distance
distancia = tempo * 0.034 / 2;
Serial.println(distancia);
moveF();
setSpeedM(150, 120);
if (distancia < 25) {
moveStop();
setSpeedM(0, 0);
delay(500);
randNum = random(0, 2);
if (randNum == 0)
{
moveB();
setSpeedM(150, 150);
delay(1000);
moveL();
setSpeedM(0, 150);
delay(900);
} else {
moveB();
setSpeedM(150, 150);
delay(1000);
moveR();
setSpeedM(150, 0);
delay(900);
}
}
}
void setSpeedM(int speedl, int speedr) {
analogWrite(motorEsqVel, speedl);
analogWrite(motorDirVel, speedr);
}
void mudaOlhos() {
delay(3000);
corOlhos(255, 0, 0);
delay(1500);
corOlhos(0, 255, 0);
delay(1500);
corOlhos(255, 0, 128);
delay(1500);
corOlhos(0, 0, 255);
}
void corOlhos(int r, int g, int b) {
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
void dancar() {
myMP3.play(10);
delay(4000);
myMP3.loop(12);
do {
setSpeedM(150, 150);
moveF();
delay(1000);
opcao = Serial.read();
if (opcao != -1)
break;
setSpeedM(150, 150);
moveB();
delay(1000);
opcao = Serial.read();
if (opcao != -1)
break;
setSpeedM(0, 150);
moveL();
delay(1500);
opcao = Serial.read();
if (opcao != -1)
break;
setSpeedM(150, 0);
moveR();
delay(1500);
opcao = Serial.read();
if (opcao != -1)
break;
} while (true);
}
void musica() {
myMP3.play(7);
delay(8000);
randNum = random(0, 3);
Serial.println(randNum);
switch (randNum) {
case 0:
myMP3.play(13);
break;
case 1:
myMP3.play(14);
break;
case 2:
myMP3.play(15);
break;
}
}