-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloud_Progress.ino
377 lines (319 loc) · 10.5 KB
/
Cloud_Progress.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//////////////////////////////////////////////INCLUDES LIBRARIES AND VARIABLES//////////////////////////////////////
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <IRremote.h>
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)
dir_t dirBuf; // buffer for directory reads
/*
* Define macro to put error messages in flash memory
*/
#define error(msg) error_P(PSTR(msg))
// Function definitions (we define them here, but the code is below)
unsigned long thunderTime;
unsigned long number;
unsigned long brightness;
unsigned long tester;
unsigned long delay1;
unsigned long delay2;
unsigned long delay3;
unsigned long delay4;
int filename;
///////////////////////////IR REMOTE///////////////////////
int RECV_PIN = A0;
IRrecv irrecv(RECV_PIN);
decode_results results;
int value;
#define thunderPin 5
#define sunPin 6
// this handy function will return the number of bytes currently free in RAM, great for debugging!
int freeRam(void)
{
extern int __bss_end;
extern int *__brkval;
int free_memory;
if((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__bss_end);
}
else {
free_memory = ((int)&free_memory) - ((int)__brkval);
}
return free_memory;
}
void sdErrorCheck(void)
{
if (!card.errorCode()) return;
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
//////////////////////////////////////////////////SETUP/////////////////////////////////////////////////////////////////////
void lsR(FatReader &d)
{
int8_t r; // indicates the level of recursion
while ((r = d.readDir(dirBuf)) > 0) { // read the next file in the directory
// skip subdirs . and ..
if (dirBuf.name[0] == '.')
continue;
for (uint8_t i = 0; i < dirLevel; 1)
Serial.print(' '); // this is for prettyprinting, put spaces in front
Serial.print("file found"); // print the name of the file we just found
Serial.println(); // and a new line
if (DIR_IS_SUBDIR(dirBuf)) { // we will recurse on any direcory
FatReader s; // make a new directory object to hold information
dirLevel += 2; // indent 2 spaces for future prints
if (s.open(vol, dirBuf))
lsR(s); // list all the files in this directory now!
dirLevel -=2; // remove the extra indentation
}
}
sdErrorCheck(); // are we doign OK?
}
void play(FatReader &dir)
{
FatReader file;
while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time
// skip . and .. directories
if (dirBuf.name[0] == '.')
continue;
Serial.println(); // clear out a new line
for (uint8_t i = 0; i < dirLevel; i++)
Serial.print(' '); // this is for prettyprinting, put spaces in front
if (!file.open(vol, dirBuf)) { // open the file in the directory
Serial.println("file.open failed"); // something went wrong :(
while(1); // halt
}
if (file.isDir()) { // check if we opened a new directory
putstring("Subdir: ");
printEntryName(dirBuf);
dirLevel += 2; // add more spaces
// play files in subdirectory
play(file); // recursive!
dirLevel -= 2;
}
else {
// Aha! we found a file that isnt a directory
putstring("Playing ");
printEntryName(dirBuf); // print it out
if (!wave.create(file)) { // Figure out, is it a WAV proper?
putstring(" Not a valid WAV"); // ok skip it
} else {
Serial.println(); // Hooray it IS a WAV proper!
if(value == 22695){
wave.play();
}
else if(value == -32641){
wave.play();
}
while (wave.isplaying) { // playing occurs in interrupts, so we print dots in realtime
if (irrecv.decode(&results)) {
value = results.value;
Serial.println(value);
irrecv.resume();
if(value == 8415){
wave.stop();
digitalWrite(thunderPin,LOW);
break;
}
else if(value == -32641){
wave.stop();
delay(1000);
break;
}
if(value == -24481){
wave.pause();
Serial.println("Paused");
}
else if(value == 255){
wave.resume();
Serial.println("Resuming...");
}
}
}
sdErrorCheck(); // everything OK?
// if (wave.errors)Serial.println(wave.errors); // wave decoding errors
}
}
}
}
void setup() {
irrecv.enableIRIn();
Serial.begin(9600);
putstring_nl("WaveHC with 6 buttons");
putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
Serial.println(freeRam()); // if this is under 150 bytes it may spell trouble!
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(thunderPin,OUTPUT);
pinMode(sunPin,OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(thunderPin,LOW);
digitalWrite(sunPin,LOW);
delay1 = 1;
delay3 = 0;
/////////////////////////////////////////////////////////SD CARD CHECK//////////////////////////////////////////////////////////
// if (!card.init(true)){ //play with 4 MHz spi if 8MHz isn't working for you
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
lsR(root);
filename = dirLevel;
Serial.println(filename);
}
void loop() {
IRremote();
}
void IRremote(){
if (irrecv.decode(&results)) {
//Serial.println(results.value);
Serial.println("button pressed");
value = results.value;
Serial.println(value);
if(value == -2041){
playfile("BIRD.WAV");
Serial.println("This is the A button");
delay1 = 1;
delay2 = 0;
delay3 = 0;
delay4 = 255;
while(wave.isplaying){
if (irrecv.decode(&results)){
value = results.value;
Serial.println(value);
irrecv.resume();
if(value == 8415){
wave.stop();
digitalWrite(thunderPin,LOW);
break;
}
}
delay(7);
if(delay1 == 1){
delay2 = delay2++;
analogWrite(sunPin,delay2);
}
if(delay2 == 255){
delay2 = 0;
delay1 = 0;
delay3 = 1;
}
if(delay3 == 1){
delay4 = delay4--;
analogWrite(sunPin,delay4);
}
if(delay4 == 0){
delay1 = 1;
delay4 = 255;
delay3 = 0;
}
}
}
if(value == 30855){
playfile("THUN.WAV");
Serial.println("This is the B button");
while(wave.isplaying){
if (irrecv.decode(&results)) {
value = results.value;
Serial.println(value);
irrecv.resume();
if(value == 8415)
{
wave.stop();
digitalWrite(thunderPin,LOW);
break;
}
}
thunderTime = random(50,400);
delay(thunderTime);
number = random(0,255);
analogWrite(thunderPin,number);
delay(15);
digitalWrite(thunderPin,LOW);
}
}
if(value == 22695){
root.rewind();
play(root);
}
if(value == -24481){
wave.pause();
Serial.println("Paused");
}
if(value == 8415){
wave.stop();
Serial.println("Sound has stopped playing");
}
if(value == 255){
wave.resume();
Serial.println("Resuming...");
}
digitalWrite(thunderPin,LOW);
digitalWrite(sunPin,LOW);
irrecv.resume(); // Receive the next value
}
}
/////////////////////////BOTH BOTTOM SETS THE FUNCTIONS FOR playcomplete() & playfile() LEAVE ALONE //////////////////////
// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
// call our helper to find and play this name
playfile(name);
while (wave.isplaying) {
// do nothing while its playing
}
// now its done playing
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file "); Serial.print(name); return;
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV"); return;
}
// ok time to play! start playback
wave.play();
}