Advertisement
ChristineWu

classical preset

Feb 10th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.76 KB | None | 0 0
  1. //Christine Wu Final Project
  2.  
  3. #include <LiquidCrystal.h>
  4. LiquidCrystal lcd(14,15,16,17,18,19);
  5.  
  6. #define NOTE_C2 65
  7. #define NOTE_CS2 69
  8. #define NOTE_D2 73
  9. #define NOTE_DS2 78
  10. #define NOTE_E2 82
  11. #define NOTE_F2 87
  12. #define NOTE_FS2 93
  13. #define NOTE_G2 98
  14. #define NOTE_GS2 104
  15. #define NOTE_A2 110
  16. #define NOTE_AS2 117
  17. #define NOTE_B2 123
  18. #define NOTE_C3 131
  19. #define NOTE_CS3 139
  20. #define NOTE_D3 147
  21. #define NOTE_DS3 156
  22. #define NOTE_E3 165
  23. #define NOTE_F3 175
  24. #define NOTE_FS3 185
  25. #define NOTE_G3 196
  26. #define NOTE_GS3 208
  27. #define NOTE_A3 220
  28. #define NOTE_AS3 233
  29. #define NOTE_B3 247
  30. #define NOTE_C4 262
  31. #define NOTE_CS4 277
  32. #define NOTE_D4 294
  33. #define NOTE_DS4 311
  34. #define NOTE_E4 330
  35. #define NOTE_F4 349
  36. #define NOTE_FS4 370
  37. #define NOTE_G4 392
  38. #define NOTE_GS4 415
  39. #define NOTE_A4 440
  40. #define NOTE_AS4 466
  41. #define NOTE_B4 494
  42. #define NOTE_C5 523
  43. #define NOTE_CS5 554
  44. #define NOTE_D5 587
  45. #define NOTE_DS5 622
  46. #define NOTE_E5 659
  47. #define NOTE_F5 698
  48. #define NOTE_FS5 740
  49. #define NOTE_G5 784
  50. #define NOTE_GS5 831
  51. #define NOTE_A5 880
  52. #define NOTE_AS5 932
  53. #define NOTE_B5 988
  54. #define NOTE_C6 1047
  55. #define NOTE_CS6 1109
  56. #define NOTE_D6 1175
  57. #define NOTE_DS6 1245
  58. #define NOTE_E6 1319
  59. #define NOTE_F6 1397
  60. #define NOTE_FS6 1480
  61. #define NOTE_G6 1568
  62. #define NOTE_GS6 1661
  63. #define NOTE_A6 1760
  64. #define NOTE_AS6 1865
  65. #define NOTE_B6 1976
  66. #define NOTE_C7 2093
  67. #define NOTE_CS7 2217
  68. #define NOTE_D7 2349
  69. #define NOTE_DS7 2489
  70. #define NOTE_E7 2637
  71. #define NOTE_F7 2794
  72. #define NOTE_FS7 2960
  73. #define NOTE_G7 3136
  74. #define NOTE_GS7 3322
  75. #define NOTE_A7 3520
  76.  
  77. #define melodyPin 13
  78.  
  79. //Fur Elise Beethoven
  80. int melody7[] =
  81. {
  82. NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_DS5,
  83. NOTE_E5, NOTE_B4, NOTE_D5, NOTE_C5,
  84. NOTE_A4, 0, 0, NOTE_C4,
  85. NOTE_E4, NOTE_A4, NOTE_B4, 0,
  86.  
  87. 0, NOTE_E4, NOTE_GS4, NOTE_B4,
  88. NOTE_C5, 0, 0, NOTE_E4,
  89. NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_DS5,
  90. NOTE_E5, NOTE_B4, NOTE_D5, NOTE_C5,
  91.  
  92. NOTE_A4, 0, 0, NOTE_C4,
  93. NOTE_E4, NOTE_A4, NOTE_B4, 0,
  94. 0, NOTE_E4, NOTE_C5, NOTE_B4,
  95. NOTE_A4, 0, 0, 0,
  96.  
  97. };
  98. int tempo7[] =
  99. {
  100. 12, 12, 12, 12,
  101. 12, 12, 12, 12,
  102. 12, 12, 12, 12,
  103. 12, 12, 12, 12,
  104.  
  105. 12, 12, 12, 12,
  106. 12, 12, 12, 12,
  107. 12, 12, 12, 12,
  108. 12, 12, 12, 12,
  109.  
  110. 12, 12, 12, 12,
  111. 12, 12, 12, 12,
  112. 12, 12, 12, 12,
  113. 12, 12, 12, 12,
  114.  
  115. };
  116.  
  117. //song B Bach minuet in G major
  118. int melody8[] =
  119. {
  120. NOTE_D5, 0, NOTE_G4, NOTE_A4,
  121. NOTE_B4, NOTE_C5, NOTE_D5, 0,
  122. NOTE_G4, 0, NOTE_G4, 0,
  123. NOTE_E5, 0, NOTE_C5, NOTE_D5,
  124.  
  125. NOTE_E5, NOTE_FS5, NOTE_G5, 0,
  126. NOTE_G4, 0, NOTE_G4, 0,
  127. NOTE_C5, 0, NOTE_D5, NOTE_C5,
  128. NOTE_B4, NOTE_A4, NOTE_B4, 0,
  129.  
  130. NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4,
  131. NOTE_FS4, 0, NOTE_G4, NOTE_A4,
  132. NOTE_B4, NOTE_G4, NOTE_B4, NOTE_A4,
  133. 0, 0, 0, 0,
  134.  
  135. NOTE_D5, 0, NOTE_G4, NOTE_A4,
  136. NOTE_B4, NOTE_C5, NOTE_D5, 0,
  137. NOTE_G4, 0, NOTE_G4, 0,
  138. NOTE_E5, 0, NOTE_C5, NOTE_D5,
  139.  
  140. NOTE_E5, NOTE_FS5, NOTE_G5, 0,
  141. NOTE_G4, 0, NOTE_G4, 0,
  142. NOTE_C5, 0, NOTE_D5, NOTE_C5,
  143. NOTE_B4, NOTE_A4, NOTE_B4, 0,
  144.  
  145. NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4,
  146. NOTE_A4, 0, NOTE_B4, NOTE_A4,
  147. NOTE_G4, NOTE_FS4, NOTE_G4, 0,
  148. 0, 0, 0, 0,
  149.  
  150.  
  151. };
  152. int tempo8[] =
  153. {
  154. 12, 12, 12, 12,
  155. 12, 12, 12, 12,
  156. 12, 12, 12, 12,
  157. 12, 12, 12, 12,
  158.  
  159. 12, 12, 12, 12,
  160. 12, 12, 12, 12,
  161. 12, 12, 12, 12,
  162. 12, 12, 12, 12,
  163.  
  164. 12, 12, 12, 12,
  165. 12, 12, 12, 12,
  166. 12, 12, 12, 12,
  167. 12, 12, 12, 12,
  168.  
  169. 12, 12, 12, 12,
  170. 12, 12, 12, 12,
  171. 12, 12, 12, 12,
  172. 12, 12, 12, 12,
  173.  
  174. 12, 12, 12, 12,
  175. 12, 12, 12, 12,
  176. 12, 12, 12, 12,
  177. 12, 12, 12, 12,
  178.  
  179. 12, 12, 12, 12,
  180. 12, 12, 12, 12,
  181. 12, 12, 12, 12,
  182. 12, 12, 12, 12,
  183. };
  184.  
  185.  
  186. //song C Rondo Alla Turca Mozart
  187. int melodymozart[] =
  188. {
  189. NOTE_B4, NOTE_A4, NOTE_GS4, NOTE_A4,
  190. NOTE_C5, 0, 0, 0,
  191. NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5,
  192. NOTE_E5, 0, 0, 0,
  193.  
  194. NOTE_F5, NOTE_E5, NOTE_DS5, NOTE_E5,
  195. NOTE_B5, NOTE_A5, NOTE_GS5, NOTE_A5,
  196. NOTE_B5, NOTE_A5, NOTE_GS5, NOTE_A5,
  197. NOTE_C6, 0, 0, 0,
  198.  
  199. NOTE_A5, 0, NOTE_C6, 0,
  200. NOTE_B5, 0, NOTE_A5, 0,
  201. NOTE_G5, 0, NOTE_A5, 0,
  202. NOTE_B5, 0, NOTE_A5, 0,
  203.  
  204. NOTE_G5, 0, NOTE_A5, 0,
  205. NOTE_B5, 0, NOTE_A5, 0,
  206. NOTE_G5, 0, NOTE_FS5, 0,
  207. NOTE_E5, 0, 0, 0,
  208.  
  209. };
  210.  
  211.  
  212. int tempomozart[] =
  213. {
  214. 12, 12, 12, 12,
  215. 12, 12, 12, 12,
  216. 12, 12, 12, 12,
  217. 12, 12, 12, 12,
  218.  
  219. 12, 12, 12, 12,
  220. 12, 12, 12, 12,
  221. 12, 12, 12, 12,
  222. 12, 12, 12, 12,
  223.  
  224. 12, 12, 12, 12,
  225. 12, 12, 12, 12,
  226. 12, 12, 12, 12,
  227. 12, 12, 12, 12,
  228.  
  229. 12, 12, 12, 12,
  230. 12, 12, 12, 12,
  231. 12, 12, 12, 12,
  232. 12, 12, 12, 12,
  233.  
  234. };
  235.  
  236. //In the hall of the mountain king grieg
  237. int melody9[] =
  238. {
  239. NOTE_B3, NOTE_CS4, NOTE_D4, NOTE_E4,
  240. NOTE_FS4, NOTE_D4, NOTE_FS4, 0,
  241. NOTE_F4, NOTE_CS4, NOTE_F4, 0,
  242. NOTE_E4, NOTE_C4, NOTE_E4, 0,
  243.  
  244. NOTE_B3, NOTE_CS4, NOTE_D4, NOTE_E4,
  245. NOTE_FS4, NOTE_D4, NOTE_FS4, NOTE_B4,
  246. NOTE_A4, NOTE_FS4, NOTE_D4, NOTE_FS4,
  247. NOTE_A4, 0, 0, 0,
  248.  
  249. NOTE_FS4, NOTE_GS4, NOTE_AS4, NOTE_B4,
  250. NOTE_CS5, NOTE_AS4, NOTE_CS5, 0,
  251. NOTE_D5, NOTE_AS4, NOTE_D5, 0,
  252. NOTE_CS5, NOTE_AS4, NOTE_CS5, 0,
  253.  
  254. NOTE_FS4, NOTE_GS4, NOTE_AS4, NOTE_B4,
  255. NOTE_CS5, NOTE_AS4, NOTE_CS5, 0,
  256. NOTE_D5, NOTE_AS4, NOTE_D5, 0,
  257. NOTE_CS5, 0, 0, 0,
  258.  
  259. };
  260.  
  261.  
  262. int tempo9[] =
  263. {
  264. 12, 12, 12, 12,
  265. 12, 12, 12, 12,
  266. 12, 12, 12, 12,
  267. 12, 12, 12, 12,
  268.  
  269. 12, 12, 12, 12,
  270. 12, 12, 12, 12,
  271. 12, 12, 12, 12,
  272. 12, 12, 12, 12,
  273.  
  274. 12, 12, 12, 12,
  275. 12, 12, 12, 12,
  276. 12, 12, 12, 12,
  277. 12, 12, 12, 12,
  278.  
  279. 12, 12, 12, 12,
  280. 12, 12, 12, 12,
  281. 12, 12, 12, 12,
  282. 12, 12, 12, 12,
  283.  
  284. };
  285.  
  286. //Nocturne opus 9 no 2
  287. int melodyC[] =
  288. {
  289. NOTE_AS4, NOTE_G5, 0, 0,
  290. 0, NOTE_F5, NOTE_G5, NOTE_F5,
  291. 0, 0, NOTE_DS5, 0,
  292. NOTE_AS4, NOTE_G5, 0, NOTE_C5,
  293.  
  294. NOTE_C6, 0, NOTE_G5, NOTE_AS5,
  295. 0, 0, NOTE_GS5, 0,
  296. NOTE_G5, NOTE_F5, 0, 0,
  297. NOTE_G5, 0, NOTE_D5, NOTE_DS5,
  298.  
  299. 0, 0, NOTE_C5, 0,
  300. 0, NOTE_AS4, NOTE_D6, NOTE_C6,
  301. NOTE_AS5, NOTE_GS5, NOTE_G5, NOTE_GS5,
  302. NOTE_C5, NOTE_D5, NOTE_DS5, 0,
  303.  
  304.  
  305. };
  306.  
  307.  
  308. int tempoC[] =
  309. {
  310. 12, 12, 12, 12,
  311. 12, 12, 12, 12,
  312. 12, 12, 12, 12,
  313. 12, 12, 12, 12,
  314.  
  315. 12, 12, 12, 12,
  316. 12, 12, 12, 12,
  317. 12, 12, 12, 12,
  318. 12, 12, 12, 12,
  319.  
  320. 12, 12, 12, 12,
  321. 12, 12, 12, 12,
  322. 16, 16, 16, 16,
  323. 16, 16, 12, 12,
  324.  
  325. };
  326.  
  327. //traumerei
  328. int melody0[] =
  329. {
  330. 0, 0, NOTE_C4, 0,
  331.  
  332. NOTE_F4, 0, 0, 0,
  333. 0, NOTE_E4, NOTE_F4, NOTE_A4,
  334. NOTE_C5, NOTE_F5, NOTE_F5, 0,
  335. 0, 0, NOTE_E5, NOTE_D5,
  336.  
  337. NOTE_C5, NOTE_F5, NOTE_G4, NOTE_A4,
  338. NOTE_AS4, NOTE_D5, NOTE_F4, NOTE_G4,
  339. NOTE_A4, NOTE_C5, NOTE_G4, 0,
  340. 0, 0, NOTE_C4, 0,
  341.  
  342. NOTE_F4, 0, 0, 0,
  343. 0, NOTE_E4, NOTE_F4, NOTE_A4,
  344. NOTE_C5, NOTE_A5, NOTE_A5,
  345. 0, NOTE_G5, NOTE_F5, NOTE_E5,
  346.  
  347. NOTE_F5, NOTE_A5, NOTE_D5, NOTE_F5,
  348. NOTE_E5, NOTE_C5, NOTE_E4, NOTE_DS5,
  349. NOTE_D5, NOTE_G4, NOTE_E5, 0,
  350. NOTE_C5, 0, 0, 0,
  351. };
  352.  
  353.  
  354. int tempo0[] =
  355. {
  356. 12, 12, 12, 12,
  357.  
  358. 12, 12, 12, 12,
  359. 12, 12, 12, 12,
  360. 12, 12, 12, 12,
  361. 12, 12, 12, 12,
  362.  
  363. 12, 12, 12, 12,
  364. 12, 12, 12, 12,
  365. 12, 12, 12, 12,
  366. 12, 12, 12, 12,
  367.  
  368. 12, 12, 12, 12,
  369. 12, 12, 12, 12,
  370. 12, 12, 12, 12,
  371. 12, 12, 12, 12,
  372.  
  373. 12, 12, 12, 12,
  374. 12, 12, 12, 12,
  375. 12, 12, 12, 12,
  376. 12, 12, 12, 12,
  377.  
  378. };
  379.  
  380.  
  381. #include <Keypad.h>
  382.  
  383.  
  384. const int piezzo=13;
  385. int ReadValue;
  386. int toneval;
  387.  
  388. const byte ROWS = 4;
  389. const byte COLS = 4;
  390. char hexaKeys[ROWS][COLS] = {
  391. {'D','C','B','A'},
  392. {'#','9','6','3'},
  393. {'0','8','5','2'},
  394. {'*','7','4','1'}
  395. };
  396. byte rowPins[ROWS] = {9, 8, 7, 6};
  397. byte colPins[COLS] = {5, 4, 3, 2};
  398.  
  399. //initialize an instance of class NewKeypad
  400. Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  401.  
  402. void setup()
  403. {
  404. Serial.begin(9600);
  405. pinMode (piezzo,OUTPUT);
  406. Serial.begin (9600);
  407.  
  408. lcd.begin (16,2);
  409. lcd.setCursor (1,0);
  410. lcd.print ("Press a button");
  411. lcd.setCursor (1,1);
  412. lcd.print ("to play music");
  413.  
  414. }
  415.  
  416. void loop()
  417. {
  418. toneval=0;
  419. char customKey = customKeypad.getKey();
  420. Serial.println(customKey);
  421. delay (80);
  422. if (customKey)
  423. {
  424. Serial.println(customKey);
  425. }
  426. if (customKey == '1')
  427. {
  428. lcd.clear();
  429. lcd.setCursor (7,0);
  430. lcd.print("C");
  431. toneval=262;
  432. }
  433. if (customKey == '2')
  434. {
  435. lcd.clear();
  436. lcd.setCursor (7,0);
  437. lcd.print("D");
  438. toneval=294;
  439. }
  440. if (customKey == '3')
  441. {
  442. lcd.clear();
  443. lcd.setCursor (7,0);
  444. lcd.print("E");
  445. toneval=330;
  446. }
  447. if (customKey == 'A')
  448. {
  449. lcd.clear();
  450. lcd.setCursor (7,0);
  451. lcd.print("F");
  452. toneval=349;
  453. }
  454. if (customKey == '4')
  455. {
  456. lcd.clear();
  457. lcd.setCursor (7,0);
  458. lcd.print("G");
  459. toneval=392;
  460. }
  461. if (customKey == '5')
  462. {
  463. lcd.clear();
  464. lcd.setCursor (7,0);
  465. lcd.print("A");
  466. toneval=440;
  467. }
  468. if (customKey == '6')
  469. {
  470. lcd.clear();
  471. lcd.setCursor (7,0);
  472. lcd.print("B");
  473. toneval=494;
  474. }
  475. if (customKey == 'B')
  476. {
  477. lcd.clear();
  478. lcd.setCursor (7,0);
  479. lcd.print("C");
  480. toneval=523;
  481. }
  482. if (customKey == '7')
  483. {
  484. lcd.clear();
  485. lcd.setCursor (3,0);
  486. lcd.print("Fur Elise");
  487. lcd.setCursor (4,1);
  488. lcd.print ("Beethoven");
  489. delay(100);
  490. sing7(1);
  491. sing7(1);
  492. lcd.clear();
  493. lcd.setCursor (1,0);
  494. lcd.print ("Press a button");
  495. lcd.setCursor (1,1);
  496. lcd.print ("to play music");
  497. }
  498. if (customKey == '8')
  499. {
  500. lcd.clear();
  501. lcd.setCursor (2,0);
  502. lcd.print("Minuet in G");
  503. lcd.setCursor (6,1);
  504. lcd.print ("Bach");
  505. delay(100);
  506. sing8(1);
  507. lcd.clear();
  508. lcd.setCursor (1,0);
  509. lcd.print ("Press a button");
  510. lcd.setCursor (1,1);
  511. lcd.print ("to play music");
  512. }
  513. if (customKey == '9')
  514. {
  515. lcd.clear();
  516. lcd.setCursor (2,0);
  517. lcd.print("In the Hall");
  518. lcd.setCursor (6,1);
  519. lcd.print ("Grieg");
  520. delay(600);
  521. lcd.clear();
  522. lcd.setCursor (4,0);
  523. lcd.print("of the");
  524. lcd.setCursor (6,1);
  525. lcd.print ("Grieg");
  526. delay(600);
  527. lcd.clear();
  528. lcd.setCursor (1,0);
  529. lcd.print("Mountain King");
  530. lcd.setCursor (6,1);
  531. lcd.print ("Grieg");
  532. delay(100);
  533. sing9(1);
  534. lcd.clear();
  535. lcd.setCursor (1,0);
  536. lcd.print ("Press a button");
  537. lcd.setCursor (1,1);
  538. lcd.print ("to play music");
  539. }
  540. if (customKey == '*')
  541. {
  542. lcd.clear();
  543. lcd.setCursor (0,0);
  544. lcd.print("Rondo Alla Turka");
  545. lcd.setCursor (4,1);
  546. lcd.print ("Mozart");
  547. delay(100);
  548. singmozart(1);
  549. singmozart(1);
  550. lcd.clear();
  551. lcd.setCursor (1,0);
  552. lcd.print ("Press a button");
  553. lcd.setCursor (1,1);
  554. lcd.print ("to play music");
  555. }
  556. if (customKey == 'C')
  557. {
  558. lcd.clear();
  559. lcd.setCursor (0,0);
  560. lcd.print("Nocturne Op9 No2");
  561. lcd.setCursor (6,1);
  562. lcd.print ("Chopin");
  563. delay(100);
  564. singC(1);
  565. lcd.clear();
  566. lcd.setCursor (1,0);
  567. lcd.print ("Press a button");
  568. lcd.setCursor (1,1);
  569. lcd.print ("to play music");
  570. }
  571. if (customKey == '0')
  572. {
  573. lcd.clear();
  574. lcd.setCursor (3,0);
  575. lcd.print("Traumerei");
  576. lcd.setCursor (3,1);
  577. lcd.print ("Schumann");
  578. delay(100);
  579. lcd.setCursor (1,0);
  580. lcd.print ("Press a button");
  581. lcd.setCursor (1,1);
  582. lcd.print ("to play music");
  583. }
  584. if (customKey == '#')
  585. {
  586. toneval=440;
  587. lcd.setCursor (1,0);
  588. lcd.print ("Press a button");
  589. lcd.setCursor (1,1);
  590. lcd.print ("to play music");
  591. }
  592. if (customKey == 'D')
  593. {
  594. toneval=440;
  595. lcd.setCursor (1,0);
  596. lcd.print ("Press a button");
  597. lcd.setCursor (1,1);
  598. lcd.print ("to play music");
  599. }
  600. tone (piezzo,toneval,400);
  601.  
  602. }
  603.  
  604. //Fur elise beethoven
  605. int song7 = 0;
  606.  
  607. void sing7(int s7)
  608. {
  609. song7 = s7;
  610. if (song7 == 1)
  611. {
  612. Serial.println("'____'");
  613. int size = sizeof(melody7) / sizeof(int);
  614. for (int thisNote = 0; thisNote < size; thisNote++)
  615. {
  616. int noteDuration = 1000 / tempo7[thisNote];
  617. buzz(melodyPin, melody7[thisNote], noteDuration);
  618. int pauseBetweenNotes = noteDuration * 1.30;
  619. delay(pauseBetweenNotes);
  620. buzz(melodyPin, 0, noteDuration);
  621. }
  622. }
  623. }
  624.  
  625.  
  626. //minuet in G bach
  627. int song8 = 0;
  628.  
  629. void sing8(int s8)
  630. {
  631. song8 = s8;
  632. if (song8 == 1)
  633. {
  634. Serial.println("'____'");
  635. int size = sizeof(melody8) / sizeof(int);
  636. for (int thisNote = 0; thisNote < size; thisNote++)
  637. {
  638. int noteDuration = 1200 / tempo8[thisNote];
  639. buzz(melodyPin, melody8[thisNote], noteDuration);
  640. int pauseBetweenNotes = noteDuration * 1.50;
  641. delay(pauseBetweenNotes);
  642. buzz(melodyPin, 0, noteDuration);
  643. }
  644. }
  645. }
  646.  
  647.  
  648. //rondo alla turka
  649. int songmozart = 0;
  650.  
  651. void singmozart(int smozart)
  652. {
  653. songmozart = smozart;
  654. if (songmozart == 1)
  655. {
  656. Serial.println("'____'");
  657. int size = sizeof(melodymozart) / sizeof(int);
  658. for (int thisNote = 0; thisNote < size; thisNote++)
  659. {
  660. int noteDuration = 900 / tempomozart[thisNote];
  661. buzz(melodyPin, melodymozart[thisNote], noteDuration);
  662. int pauseBetweenNotes = noteDuration * 1.20;
  663. delay(pauseBetweenNotes);
  664. buzz(melodyPin, 0, noteDuration);
  665. }
  666. }
  667. }
  668.  
  669. //hall of the mountain king
  670. int song9 = 0;
  671.  
  672. void sing9(int s9)
  673. {
  674. song9 = s9;
  675. if (song9 == 1)
  676. {
  677. Serial.println("'____'");
  678. int size = sizeof(melody9) / sizeof(int);
  679. for (int thisNote = 0; thisNote < size; thisNote++)
  680. {
  681. int noteDuration = 1100 / tempo9[thisNote];
  682. buzz(melodyPin, melody9[thisNote], noteDuration);
  683. int pauseBetweenNotes = noteDuration * 1.40;
  684. delay(pauseBetweenNotes);
  685. buzz(melodyPin, 0, noteDuration);
  686. }
  687. }
  688. }
  689.  
  690. //nocturne opus 9 no 2
  691.  
  692. int songC = 0;
  693.  
  694. void singC(int sC)
  695. {
  696. songC = sC;
  697. if (songC == 1)
  698. {
  699. Serial.println("'____'");
  700. int size = sizeof(melodyC) / sizeof(int);
  701. for (int thisNote = 0; thisNote < size; thisNote++)
  702. {
  703. int noteDuration = 1600 / tempoC[thisNote];
  704. buzz(melodyPin, melodyC[thisNote], noteDuration);
  705. int pauseBetweenNotes = noteDuration * 1.90;
  706. delay(pauseBetweenNotes);
  707. buzz(melodyPin, 0, noteDuration);
  708. }
  709. }
  710. }
  711.  
  712. //traumerei
  713.  
  714. int song0 = 0;
  715.  
  716. void sing0(int s0)
  717. {
  718. song0 = s0;
  719. if (song0 == 1)
  720. {
  721. Serial.println("'____'");
  722. int size = sizeof(melody0) / sizeof(int);
  723. for (int thisNote = 0; thisNote < size; thisNote++)
  724. {
  725. // to calculate the note duration, take one second
  726. // divided by the note type.
  727. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  728. int noteDuration = 1000 / tempo0[thisNote];
  729.  
  730. buzz(melodyPin, melody0[thisNote], noteDuration);
  731.  
  732. // to distinguish the notes, set a minimum time between them.
  733. // the note's duration + 30% seems to work well:
  734. int pauseBetweenNotes = noteDuration * 1.30;
  735. delay(pauseBetweenNotes);
  736.  
  737. // stop the tone playing:
  738. buzz(melodyPin, 0, noteDuration);
  739. }
  740. }
  741. }
  742.  
  743. void buzz(int targetPin, long frequency, long length)
  744. {
  745. digitalWrite(13, HIGH);
  746. long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  747. //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  748. //// there are two phases to each cycle
  749. long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  750. //// multiply frequency, which is really cycles per second, by the number of seconds to
  751. //// get the total number of cycles to produce
  752. for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  753. digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  754. delayMicroseconds(delayValue); // wait for the calculated delay value
  755. digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  756. delayMicroseconds(delayValue); // wait again or the calculated delay value
  757. }
  758. digitalWrite(13, LOW);
  759.  
  760. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement