-
Notifications
You must be signed in to change notification settings - Fork 1
/
TopArduinoSPICommented
595 lines (506 loc) · 15.4 KB
/
TopArduinoSPICommented
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* The script for top arduino on Minibot.
Sensors:
Buzzer: J3 port, (PWM) analog pin 3
RFID sensor: J8, ???
Functions: RFID, Sing
*/
//define notes for buzzer use
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#define REST 0
//melody pin 3 same as buzzerPin below
#define melodyPin 3
//initialize constands for buzzer use
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
//initialize PWM pin for buzzer use
//buzzer uses analog signal to change pitch
int buzzerPin = 3;
//mario melody
int melody[] = {
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0
};
//Mario main theme tempo
int tempo[] = {
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
};
//Underworld melody
int underworld_melody[] = {
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0, NOTE_DS4, NOTE_CS4, NOTE_D4,
NOTE_CS4, NOTE_DS4,
NOTE_DS4, NOTE_GS3,
NOTE_G3, NOTE_CS4,
NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
NOTE_GS4, NOTE_DS4, NOTE_B3,
NOTE_AS3, NOTE_A3, NOTE_GS3,
0, 0, 0
};
//Underworld tempo
int underworld_tempo[] = {
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
6, 18, 18, 18,
6, 6,
6, 6,
6, 6,
18, 18, 18, 18, 18, 18,
10, 10, 10,
10, 10, 10,
3, 3, 3
};
//Harry Potter tempo
// change this to make the song slower or faster
int tempoHarry = 144;
int melodyHarry[] = {
// Hedwig's theme fromn the Harry Potter Movies
// Socre from https://musescore.com/user/3811306/scores/4906610
REST, 2, NOTE_D4, 4,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
NOTE_G4, 2, NOTE_D5, 4,
NOTE_C5, -2,
NOTE_A4, -2,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
NOTE_F4, 2, NOTE_GS4, 4,
NOTE_D4, -1,
NOTE_D4, 4,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4, //10
NOTE_G4, 2, NOTE_D5, 4,
NOTE_F5, 2, NOTE_E5, 4,
NOTE_DS5, 2, NOTE_B4, 4,
NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_B4, 4,
NOTE_G4, -1,
NOTE_AS4, 4,
NOTE_D5, 2, NOTE_AS4, 4,//18
NOTE_D5, 2, NOTE_AS4, 4,
NOTE_DS5, 2, NOTE_D5, 4,
NOTE_CS5, 2, NOTE_A4, 4,
NOTE_AS4, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_D4, 4,
NOTE_D5, -1,
REST,4, NOTE_AS4,4,
NOTE_D5, 2, NOTE_AS4, 4,//26
NOTE_D5, 2, NOTE_AS4, 4,
NOTE_F5, 2, NOTE_E5, 4,
NOTE_DS5, 2, NOTE_B4, 4,
NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_AS4, 4,
NOTE_G4, -1,
};
// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melodyHarry) / sizeof(melodyHarry[0]) / 2;
//calculaes number of values in an array and divides by 2 as each note has two parts
// this calculates the duration of a whole note in ms (60s/tempo)*4 beats
int wholenote = (60000 * 4) / tempoHarry;
int divider = 0, noteDuration = 0;
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_PN532.h> //RFID Library
#define PN532_IRQ (17) //set up the interrupt and reset
#define PN532_RESET (9)
int val;
char buff [50];
volatile byte indx; //volatile (value can be changed by concurrently executing code (in the ISR))
volatile boolean process;
int interruptPin = 10;
//initialize the RFID with I2C connection
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
void setup() {
Serial.begin(115200);
pinMode(MISO,OUTPUT); //init spi
pinMode(3,OUTPUT);
SPCR |= bit (SPE); // slave control register
indx = 0; //buffer empty
process = false;
//RFID reader startup
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) { //checks connection to board
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// If find a board, print out the version
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
pinMode(interruptPin,INPUT);
int val = digitalRead(interruptPin); //allows ISR to receive data
delay(1000);
SPI.attachInterrupt(); //initializes interrupt
// attachInterrupt(digitalPinToInterrupt(interruptPin), tester, LOW); //enable interrupt
//val = 1;
//Serial.print(val);
}
ISR (SPI_STC_vect) { //SPI Interrupt Service Routine
// digitalWrite(3,1);
Serial.println("entered ISR"); //debugging print line to indicate interrupt has been entered
if (SPDR != c) //if new value does not equal old value
Serial.println("Value has been changed"); //debugging print line to show value updated
byte c = SPDR; //read byte from SPI data register
if (indx < sizeof (buff) - 1) //if the value of index is within the range of a byte (0-7)
buff [indx++] = c;// save data in the next index in the array buff
// if (c == '\r') //check for the end of the word
process = true;
}
void loop() {
Serial.println("entered loop"); //debugging statement, entered loop
if (process) { //if inturrupt received
buff[indx] = 0; //set the index at indx of buff to 0
process = false; //reset flag
// digitalWrite(3,0);
// Serial.println("work");
Serial.print(buff); //print to serial monitor, debugging
int i = 0;
if (i < sizeof(buff)) { //if the value of i is between (1-8) or a byte
switch(buff[i]) { //if the index of buff at i is... (checks buffer for RFID mode)
case 'O': //object detection
RFID(); //run RFID function
default: //anything else then add a value to i and rerun loop
i++;
break;
}
}
}
//SPDR = data //sends value to master via SPDR
indx = 0; //reset button to zero
}
void RFID(){ // RFID function
Serial.println("entered RFID function");
boolean detector;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID from tag
uint8_t uidLength; //Length of the UID (4 or 7 bytes depending on ISO14443A card type
//tags we have currently, labeled by color
uint8_t obj1[] = {0xB9, 0xC3, 0xD, 0xF4}; //the tag on the LED panel, Green Tower
uint8_t obj2[] = {0xB9, 0x12, 0xD, 0xF4}; //the tag on the micrstickII, Yellow Tower
uint8_t obj3[] = {0xC9, 0x82, 0xC, 0xF4}; //for the blue key tags, Red Tower
Serial.println("Sensor ready!");
detector = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if(detector){ //if true, object in range
Serial.println("Found a tag!"); //debugging print statement
Serial.println("This is ....."); //identification
if(memcmp(obj1, uid, 4) == 0){ //if obj1 is the same as uid for the first four indices
Serial.println("Object 1"); //debugging statement
sing(1); //run sing function with parameter 1 (Mario)
}
else if (memcmp(obj2, uid, 4) == 0){ //if obj2 is the same as uid for the first four indices
Serial.println("Object 2"); //debugging statement
firstSection(); //run firstSection function (Underworld)
}
else if(memcmp(obj3, uid, 4) == 0){ //if obj3 is the same as uid for the first four indices
Serial.println("Object 3"); //debugging statement
harryPotter(); //run harryPotter
}
else{ //if the buffer does not match any known objects
Serial.println("Not in database"); //debugging statment
delay(1000);
}
delay(1000);
}
else{ // if detector is false, no object in range
Serial.println("No Objects in Range");
}
}
void beep(int note, int duration) //Function used in Underworld Theme Theme
{
//Play tone on buzzerPin
tone(buzzerPin, note, duration);
delay(duration);
//Stop tone on buzzerPin
noTone(buzzerPin);
delay(50);
}
void firstSection() //Underworld Theme (if song==2 doesn't work then remove and only keep this function)
{
beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
beep(eH, 500);
beep(eH, 500);
beep(eH, 500);
beep(fH, 350);
beep(cH, 150);
beep(gS, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
}
/* unused function
void secondSection()
{
beep(aH, 500);
beep(a, 300);
beep(a, 150);
beep(aH, 500);
beep(gSH, 325);
beep(gH, 175);
beep(fSH, 125);
beep(fH, 125);
beep(fSH, 250);
delay(325);
beep(aS, 250);
beep(dSH, 500);
beep(dH, 325);
beep(cSH, 175);
beep(cH, 125);
beep(b, 125);
beep(cH, 250);
delay(350);
}
*/
int song = 0; //initialize variable for sing function
void sing(int s) { //Mario Theme
// iterate over the notes of the melody:
song = s;
if (song == 2) { //song never equals 2, so skips to else
Serial.println(" 'Underworld Theme'");
int size = sizeof(underworld_melody) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / underworld_tempo[thisNote];
buzz(melodyPin, underworld_melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(melodyPin, 0, noteDuration);
}
} else { //always runs when sing is run as song always equals 1
Serial.println(" 'Mario Theme'");
int size = sizeof(melody) / sizeof(int);
size=2*size/3-5;
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / tempo[thisNote];
buzz(melodyPin, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(melodyPin, 0, noteDuration);
}
}
}
void buzz(int targetPin, long frequency, long length) { //function used in Mario Theme
digitalWrite(13, HIGH);
long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait again or the calculated delay value
}
digitalWrite(13, LOW);
}
void harryPotter(){ //harry potter music
// iterate over the notes of the melody.
// Remember, the array is twice the number of notes (notes + durations)
divider = 0;
noteDuration=0;
for (int thisNote = 0; thisNote < (notes * .5-2); thisNote = thisNote + 2) {
// calculates the duration of each note
divider = melodyHarry[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(3, melodyHarry[thisNote], noteDuration*0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
Serial.println(noteDuration);
// stop the waveform generation before the next note.
noTone(3);
}
}