Advertisement
beall619

arduino help

Oct 28th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.44 KB | None | 0 0
  1. to forum https://forum.arduino.cc/index.php?topic=356161.0
  2. >errors
  3. STARWARS AND MARIO DOUBLE CODE.ino:286:56: error: function definition is not allowed here
  4. void buzz(int targetPin, long frequency, long length){
  5. ^
  6. STARWARS AND MARIO DOUBLE CODE.ino:305:6: error: redefinition of 'loop'
  7. void loop(){
  8. ^
  9. STARWARS AND MARIO DOUBLE CODE.ino:227:6: note: previous definition is here
  10. void loop(){
  11. ^
  12. 4 errors generated.
  13. ----------------------------------------------
  14. (my project)\/
  15. const int c = 261;
  16. const int d = 294;
  17. const int e = 329;
  18. const int f = 349;
  19. const int g = 391;
  20. const int gS = 415;
  21. const int a = 440;
  22. const int aS = 455;
  23. const int b = 466;
  24. const int cH = 523;
  25. const int cSH = 554;
  26. const int dH = 587;
  27. const int dSH = 622;
  28. const int eH = 659;
  29. const int fH = 698;
  30. const int fSH = 740;
  31. const int gH = 784;
  32. const int gSH = 830;
  33. const int aH = 880;
  34. const int buzzerPin = 11;
  35. const int ledPin1 = 7;
  36. const int ledPin2 = 2;
  37. int counter = 0;
  38. #define NOTE_B0 31
  39. #define NOTE_C1 33
  40. #define NOTE_CS1 35
  41. #define NOTE_D1 37
  42. #define NOTE_DS1 39
  43. #define NOTE_E1 41
  44. #define NOTE_F1 44
  45. #define NOTE_FS1 46
  46. #define NOTE_G1 49
  47. #define NOTE_GS1 52
  48. #define NOTE_A1 55
  49. #define NOTE_AS1 58
  50. #define NOTE_B1 62
  51. #define NOTE_C2 65
  52. #define NOTE_CS2 69
  53. #define NOTE_D2 73
  54. #define NOTE_DS2 78
  55. #define NOTE_E2 82
  56. #define NOTE_F2 87
  57. #define NOTE_FS2 93
  58. #define NOTE_G2 98
  59. #define NOTE_GS2 104
  60. #define NOTE_A2 110
  61. #define NOTE_AS2 117
  62. #define NOTE_B2 123
  63. #define NOTE_C3 131
  64. #define NOTE_CS3 139
  65. #define NOTE_D3 147
  66. #define NOTE_DS3 156
  67. #define NOTE_E3 165
  68. #define NOTE_F3 175
  69. #define NOTE_FS3 185
  70. #define NOTE_G3 196
  71. #define NOTE_GS3 208
  72. #define NOTE_A3 220
  73. #define NOTE_AS3 233
  74. #define NOTE_B3 247
  75. #define NOTE_C4 262
  76. #define NOTE_CS4 277
  77. #define NOTE_D4 294
  78. #define NOTE_DS4 311
  79. #define NOTE_E4 330
  80. #define NOTE_F4 349
  81. #define NOTE_FS4 370
  82. #define NOTE_G4 392
  83. #define NOTE_GS4 415
  84. #define NOTE_A4 440
  85. #define NOTE_AS4 466
  86. #define NOTE_B4 494
  87. #define NOTE_C5 523
  88. #define NOTE_CS5 554
  89. #define NOTE_D5 587
  90. #define NOTE_DS5 622
  91. #define NOTE_E5 659
  92. #define NOTE_F5 698
  93. #define NOTE_FS5 740
  94. #define NOTE_G5 784
  95. #define NOTE_GS5 831
  96. #define NOTE_A5 880
  97. #define NOTE_AS5 932
  98. #define NOTE_B5 988
  99. #define NOTE_C6 1047
  100. #define NOTE_CS6 1109
  101. #define NOTE_D6 1175
  102. #define NOTE_DS6 1245
  103. #define NOTE_E6 1319
  104. #define NOTE_F6 1397
  105. #define NOTE_FS6 1480
  106. #define NOTE_G6 1568
  107. #define NOTE_GS6 1661
  108. #define NOTE_A6 1760
  109. #define NOTE_AS6 1865
  110. #define NOTE_B6 1976
  111. #define NOTE_C7 2093
  112. #define NOTE_CS7 2217
  113. #define NOTE_D7 2349
  114. #define NOTE_DS7 2489
  115. #define NOTE_E7 2637
  116. #define NOTE_F7 2794
  117. #define NOTE_FS7 2960
  118. #define NOTE_G7 3136
  119. #define NOTE_GS7 3322
  120. #define NOTE_A7 3520
  121. #define NOTE_AS7 3729
  122. #define NOTE_B7 3951
  123. #define NOTE_C8 4186
  124. #define NOTE_CS8 4435
  125. #define NOTE_D8 4699
  126. #define NOTE_DS8 4978
  127. #define melodyPin 11
  128. #define st 8
  129. int melody[] = {
  130. NOTE_E7, NOTE_E7, 0, NOTE_E7,
  131. 0, NOTE_C7, NOTE_E7, 0,
  132. NOTE_G7, 0, 0, 0,
  133. NOTE_G6, 0, 0, 0,
  134.  
  135. NOTE_C7, 0, 0, NOTE_G6,
  136. 0, 0, NOTE_E6, 0,
  137. 0, NOTE_A6, 0, NOTE_B6,
  138. 0, NOTE_AS6, NOTE_A6, 0,
  139.  
  140. NOTE_G6, NOTE_E7, NOTE_G7,
  141. NOTE_A7, 0, NOTE_F7, NOTE_G7,
  142. 0, NOTE_E7, 0, NOTE_C7,
  143. NOTE_D7, NOTE_B6, 0, 0,
  144.  
  145. NOTE_C7, 0, 0, NOTE_G6,
  146. 0, 0, NOTE_E6, 0,
  147. 0, NOTE_A6, 0, NOTE_B6,
  148. 0, NOTE_AS6, NOTE_A6, 0,
  149.  
  150. NOTE_G6, NOTE_E7, NOTE_G7,
  151. NOTE_A7, 0, NOTE_F7, NOTE_G7,
  152. 0, NOTE_E7, 0, NOTE_C7,
  153. NOTE_D7, NOTE_B6, 0, 0
  154. };
  155. //Mario main them tempo
  156. int tempo[] = {
  157. 12, 12, 12, 12,
  158. 12, 12, 12, 12,
  159. 12, 12, 12, 12,
  160. 12, 12, 12, 12,
  161.  
  162. 12, 12, 12, 12,
  163. 12, 12, 12, 12,
  164. 12, 12, 12, 12,
  165. 12, 12, 12, 12,
  166.  
  167. 9, 9, 9,
  168. 12, 12, 12, 12,
  169. 12, 12, 12, 12,
  170. 12, 12, 12, 12,
  171.  
  172. 12, 12, 12, 12,
  173. 12, 12, 12, 12,
  174. 12, 12, 12, 12,
  175. 12, 12, 12, 12,
  176.  
  177. 9, 9, 9,
  178. 12, 12, 12, 12,
  179. 12, 12, 12, 12,
  180. 12, 12, 12, 12,
  181. };
  182. //Underworld melody
  183. int underworld_melody[] = {
  184. NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  185. NOTE_AS3, NOTE_AS4, 0,
  186. 0,
  187. NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  188. NOTE_AS3, NOTE_AS4, 0,
  189. 0,
  190. NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  191. NOTE_DS3, NOTE_DS4, 0,
  192. 0,
  193. NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  194. NOTE_DS3, NOTE_DS4, 0,
  195. 0, NOTE_DS4, NOTE_CS4, NOTE_D4,
  196. NOTE_CS4, NOTE_DS4,
  197. NOTE_DS4, NOTE_GS3,
  198. NOTE_G3, NOTE_CS4,
  199. NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  200. NOTE_GS4, NOTE_DS4, NOTE_B3,
  201. NOTE_AS3, NOTE_A3, NOTE_GS3,
  202. 0, 0, 0
  203. };
  204. //Underwolrd tempo
  205. int underworld_tempo[] = {
  206. 12, 12, 12, 12,
  207. 12, 12, 6,
  208. 3,
  209. 12, 12, 12, 12,
  210. 12, 12, 6,
  211. 3,
  212. 12, 12, 12, 12,
  213. 12, 12, 6,
  214. 3,
  215. 12, 12, 12, 12,
  216. 12, 12, 6,
  217. 6, 18, 18, 18,
  218. 6, 6,
  219. 6, 6,
  220. 6, 6,
  221. 18, 18, 18, 18, 18, 18,
  222. 10, 10, 10,
  223. 10, 10, 10,
  224. 3, 3, 3
  225. };
  226.  
  227. void setup(){
  228. pinMode(st, INPUT);
  229. pinMode(buzzerPin, OUTPUT);
  230. pinMode(ledPin1, OUTPUT);
  231. pinMode(ledPin2, OUTPUT);
  232. pinMode(11, OUTPUT);
  233. pinMode(7, OUTPUT);
  234. pinMode(4, OUTPUT);
  235. pinMode(2, OUTPUT);
  236. digitalWrite(4, HIGH);
  237. digitalWrite(7, HIGH);
  238. digitalWrite(2, HIGH);
  239. Serial.println(st);
  240. }
  241. void loop(){
  242. while (st, HIGH){
  243. void loop()
  244. {
  245. sing(1);
  246. sing(1);
  247. sing(2);
  248. }
  249. int song = 0;
  250. void sing(int s) {
  251. // iterate over the notes of the melody:
  252. song = s;
  253. if (song == 2) {
  254. Serial.println(" 'Underworld Theme'");
  255. int size = sizeof(underworld_melody) / sizeof(int);
  256. for (int thisNote = 0; thisNote < size; thisNote++) {
  257.  
  258. // to calculate the note duration, take one second
  259. // divided by the note type.
  260. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  261. int noteDuration = 1000 / underworld_tempo[thisNote];
  262.  
  263. buzz(melodyPin, underworld_melody[thisNote], noteDuration);
  264.  
  265. // to distinguish the notes, set a minimum time between them.
  266. // the note's duration + 30% seems to work well:
  267. int pauseBetweenNotes = noteDuration * 1.30;
  268. delay(pauseBetweenNotes);
  269.  
  270. // stop the tone playing:
  271. buzz(melodyPin, 0, noteDuration);
  272.  
  273. }
  274.  
  275. } else {
  276.  
  277. Serial.println(" 'Mario Theme'");
  278. int size = sizeof(melody) / sizeof(int);
  279. for (int thisNote = 0; thisNote < size; thisNote++) {
  280.  
  281. // to calculate the note duration, take one second
  282. // divided by the note type.
  283. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  284. int noteDuration = 1000 / tempo[thisNote];
  285.  
  286. buzz(melodyPin, melody[thisNote], noteDuration);
  287.  
  288. // to distinguish the notes, set a minimum time between them.
  289. // the note's duration + 30% seems to work well:
  290. int pauseBetweenNotes = noteDuration * 1.30;
  291. delay(pauseBetweenNotes);
  292.  
  293. // stop the tone playing:
  294. buzz(melodyPin, 0, noteDuration);
  295.  
  296. }
  297. }
  298. }
  299.  
  300. void buzz(int targetPin, long frequency, long length){
  301. digitalWrite(2, LOW);
  302. long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  303. //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  304. //// there are two phases to each cycle
  305. long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  306. //// multiply frequency, which is really cycles per second, by the number of seconds to
  307. //// get the total number of cycles to produce
  308. for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  309. digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  310. delayMicroseconds(delayValue); // wait for the calculated delay value
  311. digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  312. delayMicroseconds(delayValue); // wait again or the calculated delay value
  313. }
  314. digitalWrite(2, HIGH);
  315.  
  316. }
  317. }
  318. }
  319. void loop(){
  320. digitalWrite(4, HIGH);
  321. //Play first section
  322. firstSection();
  323. //Play second section
  324. secondSection();
  325. //Variant 1
  326. beep(f, 250);
  327. beep(gS, 500);
  328. beep(f, 350);
  329. beep(a, 125);
  330. beep(cH, 500);
  331. beep(a, 375);
  332. beep(cH, 125);
  333. beep(eH, 650);
  334. delay(500);
  335. //Repeat second section
  336. secondSection();
  337. //Variant 2
  338. beep(f, 250);
  339. beep(gS, 500);
  340. beep(f, 375);
  341. beep(cH, 125);
  342. beep(a, 500);
  343. beep(f, 375);
  344. beep(cH, 125);
  345. beep(a, 650);
  346. delay(650);
  347. }
  348.  
  349. void beep(int note, int duration)
  350. {
  351. //Play tone on buzzerPin
  352. tone(buzzerPin, note, duration);
  353.  
  354. //Play different LED depending on value of 'counter'
  355. if(counter % 2 == 0)
  356. {
  357. digitalWrite(4, HIGH);
  358. digitalWrite(ledPin1, LOW);
  359. delay(duration);
  360. digitalWrite(ledPin1, HIGH);
  361. }else
  362. {
  363. digitalWrite(4, HIGH);
  364. digitalWrite(ledPin2, LOW);
  365. delay(duration);
  366. digitalWrite(ledPin2, HIGH);
  367. }
  368.  
  369. //Stop tone on buzzerPin
  370. noTone(buzzerPin);
  371.  
  372. delay(50);
  373.  
  374. //Increment counter
  375. counter++;
  376. }
  377.  
  378. void firstSection()
  379. {
  380. beep(a, 500);
  381. beep(a, 500);
  382. beep(a, 500);
  383. beep(f, 350);
  384. beep(cH, 150);
  385. beep(a, 500);
  386. beep(f, 350);
  387. beep(cH, 150);
  388. beep(a, 650);
  389.  
  390. delay(500);
  391.  
  392. beep(eH, 500);
  393. beep(eH, 500);
  394. beep(eH, 500);
  395. beep(fH, 350);
  396. beep(cH, 150);
  397. beep(gS, 500);
  398. beep(f, 350);
  399. beep(cH, 150);
  400. beep(a, 650);
  401.  
  402. delay(500);
  403. }
  404.  
  405. void secondSection()
  406. {
  407. beep(aH, 500);
  408. beep(a, 300);
  409. beep(a, 150);
  410. beep(aH, 500);
  411. beep(gSH, 325);
  412. beep(gH, 175);
  413. beep(fSH, 125);
  414. beep(fH, 125);
  415. beep(fSH, 250);
  416.  
  417. delay(325);
  418.  
  419. beep(aS, 250);
  420. beep(dSH, 500);
  421. beep(dH, 325);
  422. beep(cSH, 175);
  423. beep(cH, 125);
  424. beep(b, 125);
  425. beep(cH, 250);
  426.  
  427. delay(350);
  428. }
  429.  
  430. (Star wars original)\/
  431.  
  432. [code]const int c = 261;
  433. const int d = 294;
  434. const int e = 329;
  435. const int f = 349;
  436. const int g = 391;
  437. const int gS = 415;
  438. const int a = 440;
  439. const int aS = 455;
  440. const int b = 466;
  441. const int cH = 523;
  442. const int cSH = 554;
  443. const int dH = 587;
  444. const int dSH = 622;
  445. const int eH = 659;
  446. const int fH = 698;
  447. const int fSH = 740;
  448. const int gH = 784;
  449. const int gSH = 830;
  450. const int aH = 880;
  451.  
  452. const int buzzerPin = 11; // Digital Pin 8
  453. const int ledPin1 = 7; // Digital Pin 12
  454. const int ledPin2 = 2; // Digital Pin 13 Built In Led can Change it if you want
  455.  
  456. int counter = 0;
  457.  
  458. void setup()
  459. {
  460. //Setup pin modes
  461. pinMode(buzzerPin, OUTPUT); // Digital Pin 8
  462. pinMode(ledPin1, OUTPUT); // Digital Pin 12
  463. pinMode(ledPin2, OUTPUT); // Digital Pin 13 Built In Led can Change it if you want
  464. digitalWrite(4, HIGH);
  465. digitalWrite(7, HIGH);
  466. digitalWrite(2, HIGH);
  467. }
  468.  
  469. void loop()
  470. {
  471. digitalWrite(4, HIGH);
  472. //Play first section
  473. firstSection();
  474.  
  475. //Play second section
  476. secondSection();
  477.  
  478. //Variant 1
  479. beep(f, 250);
  480. beep(gS, 500);
  481. beep(f, 350);
  482. beep(a, 125);
  483. beep(cH, 500);
  484. beep(a, 375);
  485. beep(cH, 125);
  486. beep(eH, 650);
  487.  
  488. delay(500);
  489.  
  490. //Repeat second section
  491. secondSection();
  492.  
  493. //Variant 2
  494. beep(f, 250);
  495. beep(gS, 500);
  496. beep(f, 375);
  497. beep(cH, 125);
  498. beep(a, 500);
  499. beep(f, 375);
  500. beep(cH, 125);
  501. beep(a, 650);
  502.  
  503. delay(650);
  504. }
  505.  
  506. void beep(int note, int duration)
  507. {
  508. //Play tone on buzzerPin
  509. tone(buzzerPin, note, duration);
  510.  
  511. //Play different LED depending on value of 'counter'
  512. if(counter % 2 == 0)
  513. {
  514. digitalWrite(4, HIGH);
  515. digitalWrite(ledPin1, LOW);
  516. delay(duration);
  517. digitalWrite(ledPin1, HIGH);
  518. }else
  519. {
  520. digitalWrite(4, HIGH);
  521. digitalWrite(ledPin2, LOW);
  522. delay(duration);
  523. digitalWrite(ledPin2, HIGH);
  524. }
  525.  
  526. //Stop tone on buzzerPin
  527. noTone(buzzerPin);
  528.  
  529. delay(50);
  530.  
  531. //Increment counter
  532. counter++;
  533. }
  534.  
  535. void firstSection()
  536. {
  537. beep(a, 500);
  538. beep(a, 500);
  539. beep(a, 500);
  540. beep(f, 350);
  541. beep(cH, 150);
  542. beep(a, 500);
  543. beep(f, 350);
  544. beep(cH, 150);
  545. beep(a, 650);
  546.  
  547. delay(500);
  548.  
  549. beep(eH, 500);
  550. beep(eH, 500);
  551. beep(eH, 500);
  552. beep(fH, 350);
  553. beep(cH, 150);
  554. beep(gS, 500);
  555. beep(f, 350);
  556. beep(cH, 150);
  557. beep(a, 650);
  558.  
  559. delay(500);
  560. }
  561.  
  562. void secondSection()
  563. {
  564. beep(aH, 500);
  565. beep(a, 300);
  566. beep(a, 150);
  567. beep(aH, 500);
  568. beep(gSH, 325);
  569. beep(gH, 175);
  570. beep(fSH, 125);
  571. beep(fH, 125);
  572. beep(fSH, 250);
  573.  
  574. delay(325);
  575.  
  576. beep(aS, 250);
  577. beep(dSH, 500);
  578. beep(dH, 325);
  579. beep(cSH, 175);
  580. beep(cH, 125);
  581. beep(b, 125);
  582. beep(cH, 250);
  583.  
  584. delay(350);
  585. }
  586.  
  587. ---------------------------------------------------------------------
  588. (Mario original)\/
  589.  
  590. /*
  591. Arduino Mario Bros Tunes
  592. With Piezo Buzzer and PWM
  593.  
  594. Connect the positive side of the Buzzer to pin 3,
  595. then the negative side to a 1k ohm resistor. Connect
  596. the other side of the 1 k ohm resistor to
  597. ground(GND) pin on the Arduino.
  598.  
  599. by: Dipto Pratyaksa
  600. last updated: 31/3/13
  601. */
  602.  
  603. /*************************************************
  604. * Public Constants
  605. *************************************************/
  606.  
  607. #define NOTE_B0 31
  608. #define NOTE_C1 33
  609. #define NOTE_CS1 35
  610. #define NOTE_D1 37
  611. #define NOTE_DS1 39
  612. #define NOTE_E1 41
  613. #define NOTE_F1 44
  614. #define NOTE_FS1 46
  615. #define NOTE_G1 49
  616. #define NOTE_GS1 52
  617. #define NOTE_A1 55
  618. #define NOTE_AS1 58
  619. #define NOTE_B1 62
  620. #define NOTE_C2 65
  621. #define NOTE_CS2 69
  622. #define NOTE_D2 73
  623. #define NOTE_DS2 78
  624. #define NOTE_E2 82
  625. #define NOTE_F2 87
  626. #define NOTE_FS2 93
  627. #define NOTE_G2 98
  628. #define NOTE_GS2 104
  629. #define NOTE_A2 110
  630. #define NOTE_AS2 117
  631. #define NOTE_B2 123
  632. #define NOTE_C3 131
  633. #define NOTE_CS3 139
  634. #define NOTE_D3 147
  635. #define NOTE_DS3 156
  636. #define NOTE_E3 165
  637. #define NOTE_F3 175
  638. #define NOTE_FS3 185
  639. #define NOTE_G3 196
  640. #define NOTE_GS3 208
  641. #define NOTE_A3 220
  642. #define NOTE_AS3 233
  643. #define NOTE_B3 247
  644. #define NOTE_C4 262
  645. #define NOTE_CS4 277
  646. #define NOTE_D4 294
  647. #define NOTE_DS4 311
  648. #define NOTE_E4 330
  649. #define NOTE_F4 349
  650. #define NOTE_FS4 370
  651. #define NOTE_G4 392
  652. #define NOTE_GS4 415
  653. #define NOTE_A4 440
  654. #define NOTE_AS4 466
  655. #define NOTE_B4 494
  656. #define NOTE_C5 523
  657. #define NOTE_CS5 554
  658. #define NOTE_D5 587
  659. #define NOTE_DS5 622
  660. #define NOTE_E5 659
  661. #define NOTE_F5 698
  662. #define NOTE_FS5 740
  663. #define NOTE_G5 784
  664. #define NOTE_GS5 831
  665. #define NOTE_A5 880
  666. #define NOTE_AS5 932
  667. #define NOTE_B5 988
  668. #define NOTE_C6 1047
  669. #define NOTE_CS6 1109
  670. #define NOTE_D6 1175
  671. #define NOTE_DS6 1245
  672. #define NOTE_E6 1319
  673. #define NOTE_F6 1397
  674. #define NOTE_FS6 1480
  675. #define NOTE_G6 1568
  676. #define NOTE_GS6 1661
  677. #define NOTE_A6 1760
  678. #define NOTE_AS6 1865
  679. #define NOTE_B6 1976
  680. #define NOTE_C7 2093
  681. #define NOTE_CS7 2217
  682. #define NOTE_D7 2349
  683. #define NOTE_DS7 2489
  684. #define NOTE_E7 2637
  685. #define NOTE_F7 2794
  686. #define NOTE_FS7 2960
  687. #define NOTE_G7 3136
  688. #define NOTE_GS7 3322
  689. #define NOTE_A7 3520
  690. #define NOTE_AS7 3729
  691. #define NOTE_B7 3951
  692. #define NOTE_C8 4186
  693. #define NOTE_CS8 4435
  694. #define NOTE_D8 4699
  695. #define NOTE_DS8 4978
  696. #define melodyPin 11
  697. //Mario main theme melody
  698. int melody[] = {
  699. NOTE_E7, NOTE_E7, 0, NOTE_E7,
  700. 0, NOTE_C7, NOTE_E7, 0,
  701. NOTE_G7, 0, 0, 0,
  702. NOTE_G6, 0, 0, 0,
  703.  
  704. NOTE_C7, 0, 0, NOTE_G6,
  705. 0, 0, NOTE_E6, 0,
  706. 0, NOTE_A6, 0, NOTE_B6,
  707. 0, NOTE_AS6, NOTE_A6, 0,
  708.  
  709. NOTE_G6, NOTE_E7, NOTE_G7,
  710. NOTE_A7, 0, NOTE_F7, NOTE_G7,
  711. 0, NOTE_E7, 0, NOTE_C7,
  712. NOTE_D7, NOTE_B6, 0, 0,
  713.  
  714. NOTE_C7, 0, 0, NOTE_G6,
  715. 0, 0, NOTE_E6, 0,
  716. 0, NOTE_A6, 0, NOTE_B6,
  717. 0, NOTE_AS6, NOTE_A6, 0,
  718.  
  719. NOTE_G6, NOTE_E7, NOTE_G7,
  720. NOTE_A7, 0, NOTE_F7, NOTE_G7,
  721. 0, NOTE_E7, 0, NOTE_C7,
  722. NOTE_D7, NOTE_B6, 0, 0
  723. };
  724. //Mario main them tempo
  725. int tempo[] = {
  726. 12, 12, 12, 12,
  727. 12, 12, 12, 12,
  728. 12, 12, 12, 12,
  729. 12, 12, 12, 12,
  730.  
  731. 12, 12, 12, 12,
  732. 12, 12, 12, 12,
  733. 12, 12, 12, 12,
  734. 12, 12, 12, 12,
  735.  
  736. 9, 9, 9,
  737. 12, 12, 12, 12,
  738. 12, 12, 12, 12,
  739. 12, 12, 12, 12,
  740.  
  741. 12, 12, 12, 12,
  742. 12, 12, 12, 12,
  743. 12, 12, 12, 12,
  744. 12, 12, 12, 12,
  745.  
  746. 9, 9, 9,
  747. 12, 12, 12, 12,
  748. 12, 12, 12, 12,
  749. 12, 12, 12, 12,
  750. };
  751. //Underworld melody
  752. int underworld_melody[] = {
  753. NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  754. NOTE_AS3, NOTE_AS4, 0,
  755. 0,
  756. NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  757. NOTE_AS3, NOTE_AS4, 0,
  758. 0,
  759. NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  760. NOTE_DS3, NOTE_DS4, 0,
  761. 0,
  762. NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  763. NOTE_DS3, NOTE_DS4, 0,
  764. 0, NOTE_DS4, NOTE_CS4, NOTE_D4,
  765. NOTE_CS4, NOTE_DS4,
  766. NOTE_DS4, NOTE_GS3,
  767. NOTE_G3, NOTE_CS4,
  768. NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  769. NOTE_GS4, NOTE_DS4, NOTE_B3,
  770. NOTE_AS3, NOTE_A3, NOTE_GS3,
  771. 0, 0, 0
  772. };
  773. //Underwolrd tempo
  774. int underworld_tempo[] = {
  775. 12, 12, 12, 12,
  776. 12, 12, 6,
  777. 3,
  778. 12, 12, 12, 12,
  779. 12, 12, 6,
  780. 3,
  781. 12, 12, 12, 12,
  782. 12, 12, 6,
  783. 3,
  784. 12, 12, 12, 12,
  785. 12, 12, 6,
  786. 6, 18, 18, 18,
  787. 6, 6,
  788. 6, 6,
  789. 6, 6,
  790. 18, 18, 18, 18, 18, 18,
  791. 10, 10, 10,
  792. 10, 10, 10,
  793. 3, 3, 3
  794. };
  795.  
  796. void setup(void)
  797. {
  798. pinMode(11, OUTPUT);//buzzer
  799. pinMode(13, OUTPUT);//led indicator when singing a note
  800. digitalWrite(4, HIGH);
  801. digitalWrite(7, HIGH);
  802. digitalWrite(2, HIGH);
  803. pinMode(7, OUTPUT);
  804. pinMode(4, OUTPUT);
  805. pinMode(2, OUTPUT);
  806.  
  807. }
  808. void loop()
  809. {
  810. //sing the tunes
  811. sing(1);
  812. sing(1);
  813. sing(2);
  814. }
  815. int song = 0;
  816.  
  817. void sing(int s) {
  818. // iterate over the notes of the melody:
  819. song = s;
  820. if (song == 2) {
  821. Serial.println(" 'Underworld Theme'");
  822. int size = sizeof(underworld_melody) / sizeof(int);
  823. for (int thisNote = 0; thisNote < size; thisNote++) {
  824.  
  825. // to calculate the note duration, take one second
  826. // divided by the note type.
  827. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  828. int noteDuration = 1000 / underworld_tempo[thisNote];
  829.  
  830. buzz(melodyPin, underworld_melody[thisNote], noteDuration);
  831.  
  832. // to distinguish the notes, set a minimum time between them.
  833. // the note's duration + 30% seems to work well:
  834. int pauseBetweenNotes = noteDuration * 1.30;
  835. delay(pauseBetweenNotes);
  836.  
  837. // stop the tone playing:
  838. buzz(melodyPin, 0, noteDuration);
  839.  
  840. }
  841.  
  842. } else {
  843.  
  844. Serial.println(" 'Mario Theme'");
  845. int size = sizeof(melody) / sizeof(int);
  846. for (int thisNote = 0; thisNote < size; thisNote++) {
  847.  
  848. // to calculate the note duration, take one second
  849. // divided by the note type.
  850. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  851. int noteDuration = 1000 / tempo[thisNote];
  852.  
  853. buzz(melodyPin, melody[thisNote], noteDuration);
  854.  
  855. // to distinguish the notes, set a minimum time between them.
  856. // the note's duration + 30% seems to work well:
  857. int pauseBetweenNotes = noteDuration * 1.30;
  858. delay(pauseBetweenNotes);
  859.  
  860. // stop the tone playing:
  861. buzz(melodyPin, 0, noteDuration);
  862.  
  863. }
  864. }
  865. }
  866.  
  867. void buzz(int targetPin, long frequency, long length) {
  868. digitalWrite(2, LOW);
  869. long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  870. //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  871. //// there are two phases to each cycle
  872. long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  873. //// multiply frequency, which is really cycles per second, by the number of seconds to
  874. //// get the total number of cycles to produce
  875. for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  876. digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  877. delayMicroseconds(delayValue); // wait for the calculated delay value
  878. digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  879. delayMicroseconds(delayValue); // wait again or the calculated delay value
  880. }
  881. digitalWrite(2, HIGH);
  882.  
  883. }
  884. ---------------------------------------------------------------
  885.  
  886. STARWARS ORIGINAl\/
  887.  
  888. const int c = 261;
  889. const int d = 294;
  890. const int e = 329;
  891. const int f = 349;
  892. const int g = 391;
  893. const int gS = 415;
  894. const int a = 440;
  895. const int aS = 455;
  896. const int b = 466;
  897. const int cH = 523;
  898. const int cSH = 554;
  899. const int dH = 587;
  900. const int dSH = 622;
  901. const int eH = 659;
  902. const int fH = 698;
  903. const int fSH = 740;
  904. const int gH = 784;
  905. const int gSH = 830;
  906. const int aH = 880;
  907.  
  908. const int buzzerPin = 11; // Digital Pin 8
  909. const int ledPin1 = 7; // Digital Pin 12
  910. const int ledPin2 = 2; // Digital Pin 13 Built In Led can Change it if you want
  911.  
  912. int counter = 0;
  913.  
  914. void setup()
  915. {
  916. //Setup pin modes
  917. pinMode(buzzerPin, OUTPUT); // Digital Pin 8
  918. pinMode(ledPin1, OUTPUT); // Digital Pin 12
  919. pinMode(ledPin2, OUTPUT); // Digital Pin 13 Built In Led can Change it if you want
  920. digitalWrite(4, HIGH);
  921. digitalWrite(7, HIGH);
  922. digitalWrite(2, HIGH);
  923. }
  924.  
  925. void loop()
  926. {
  927. digitalWrite(4, HIGH);
  928. //Play first section
  929. firstSection();
  930.  
  931. //Play second section
  932. secondSection();
  933.  
  934. //Variant 1
  935. beep(f, 250);
  936. beep(gS, 500);
  937. beep(f, 350);
  938. beep(a, 125);
  939. beep(cH, 500);
  940. beep(a, 375);
  941. beep(cH, 125);
  942. beep(eH, 650);
  943.  
  944. delay(500);
  945.  
  946. //Repeat second section
  947. secondSection();
  948.  
  949. //Variant 2
  950. beep(f, 250);
  951. beep(gS, 500);
  952. beep(f, 375);
  953. beep(cH, 125);
  954. beep(a, 500);
  955. beep(f, 375);
  956. beep(cH, 125);
  957. beep(a, 650);
  958.  
  959. delay(650);
  960. }
  961.  
  962. void beep(int note, int duration)
  963. {
  964. //Play tone on buzzerPin
  965. tone(buzzerPin, note, duration);
  966.  
  967. //Play different LED depending on value of 'counter'
  968. if(counter % 2 == 0)
  969. {
  970. digitalWrite(4, HIGH);
  971. digitalWrite(ledPin1, LOW);
  972. delay(duration);
  973. digitalWrite(ledPin1, HIGH);
  974. }else
  975. {
  976. digitalWrite(4, HIGH);
  977. digitalWrite(ledPin2, LOW);
  978. delay(duration);
  979. digitalWrite(ledPin2, HIGH);
  980. }
  981.  
  982. //Stop tone on buzzerPin
  983. noTone(buzzerPin);
  984.  
  985. delay(50);
  986.  
  987. //Increment counter
  988. counter++;
  989. }
  990.  
  991. void firstSection()
  992. {
  993. beep(a, 500);
  994. beep(a, 500);
  995. beep(a, 500);
  996. beep(f, 350);
  997. beep(cH, 150);
  998. beep(a, 500);
  999. beep(f, 350);
  1000. beep(cH, 150);
  1001. beep(a, 650);
  1002.  
  1003. delay(500);
  1004.  
  1005. beep(eH, 500);
  1006. beep(eH, 500);
  1007. beep(eH, 500);
  1008. beep(fH, 350);
  1009. beep(cH, 150);
  1010. beep(gS, 500);
  1011. beep(f, 350);
  1012. beep(cH, 150);
  1013. beep(a, 650);
  1014.  
  1015. delay(500);
  1016. }
  1017.  
  1018. void secondSection()
  1019. {
  1020. beep(aH, 500);
  1021. beep(a, 300);
  1022. beep(a, 150);
  1023. beep(aH, 500);
  1024. beep(gSH, 325);
  1025. beep(gH, 175);
  1026. beep(fSH, 125);
  1027. beep(fH, 125);
  1028. beep(fSH, 250);
  1029.  
  1030. delay(325);
  1031.  
  1032. beep(aS, 250);
  1033. beep(dSH, 500);
  1034. beep(dH, 325);
  1035. beep(cSH, 175);
  1036. beep(cH, 125);
  1037. beep(b, 125);
  1038. beep(cH, 250);
  1039.  
  1040. delay(350);
  1041. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement