Advertisement
Guest User

Piezo Music with Arduino

a guest
Jun 12th, 2013
1,766
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Périodes de chaque note (*0.0000001)
  2. #define tone_C 1911
  3. #define tone_C1 1804
  4. #define tone_D 1703
  5. #define tone_Eb 1607
  6. #define tone_E 1517
  7. #define tone_F 1432
  8. #define tone_F1 1352
  9. #define tone_G 1276
  10. #define tone_Ab 1204
  11. #define tone_A 1136
  12. #define tone_Bb 1073
  13. #define tone_B 1012
  14. #define tone_c 955
  15. #define tone_c1 902
  16. #define tone_d 851
  17. #define tone_eb 803
  18. #define tone_e 758
  19. #define tone_f 716
  20. #define tone_f1 676
  21. #define tone_g 638
  22. #define tone_ab 602
  23. #define tone_a 568
  24. #define tone_bb 536
  25. #define tone_b 506
  26.  
  27. #define tone_p 0  //pause
  28.  
  29. const int PIEZO = 85;
  30.  
  31. long vel = 20000;
  32.  
  33. // mario
  34. int mario_m[] = {tone_e, tone_e, tone_e, tone_c, tone_e, tone_g, tone_G, tone_c, tone_G, tone_E, tone_A, tone_B, tone_Bb, tone_A, tone_G, tone_e, tone_g, tone_a, tone_f, tone_g, tone_e, tone_c, tone_d, tone_B, tone_c};
  35. int mario_r[] = {6, 12, 12, 6, 12, 24, 24, 18, 18, 18, 12, 12, 6, 12, 8, 8, 8, 12, 6, 12, 12, 6, 6, 6, 12};
  36.  
  37. // Looks like zelda
  38. int peergynt_m[] = {tone_G, tone_E, tone_D, tone_C, tone_D, tone_E, tone_G, tone_E, tone_D, tone_C, tone_D, tone_E, tone_D, tone_E,tone_G, tone_E, tone_G, tone_A, tone_E, tone_A, tone_G, tone_E, tone_D, tone_C};
  39. int peergynt_r[] = {8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 16};
  40.  
  41. // Some electric guitar...
  42. int smoke_m[] = {tone_E, tone_G, tone_A, tone_E, tone_G, tone_Bb, tone_A, tone_E, tone_G, tone_A, tone_G, tone_E};
  43. int smoke_r[] = {12, 12, 18, 12, 12, 6, 18, 12, 12, 18, 12, 24};
  44.  
  45. // Start like something similar to le reine au nez rouge...
  46. int natal_m[] = {tone_G, tone_A, tone_G, tone_E, tone_G, tone_A, tone_G, tone_E, tone_c, tone_c, tone_A, tone_B, tone_B, tone_G, tone_A, tone_G, tone_A, tone_c, tone_B, tone_A, tone_G, tone_A, tone_G, tone_E};
  47. int natal_r[] = {12, 4, 8, 16, 12, 4, 8, 16, 12, 4, 16, 12, 4, 16, 12, 4, 8, 8, 8, 8, 12, 4, 8, 16};
  48.  
  49. // Seems like some electric guitar solo...
  50. int LTS_m[] = {tone_Bb, tone_G, tone_G, tone_Bb, tone_G, tone_G, tone_Bb, tone_G, tone_G, tone_Bb, tone_G, tone_G, tone_Bb, tone_G, tone_C, tone_G, tone_Bb, tone_G, tone_G, tone_Bb, tone_G, tone_G, tone_Bb, tone_G, tone_G, tone_Bb, tone_G, tone_G, tone_Bb, tone_G, tone_F, tone_D, tone_F, tone_D, tone_G, tone_F, tone_D, tone_C, tone_Bb, tone_G, tone_Bb, tone_C, tone_C1, tone_C, tone_Bb, tone_F, tone_D, tone_Bb, tone_G, tone_F, tone_D, tone_C, tone_Bb, tone_D, tone_C, tone_Bb, tone_G} ;
  51. int LTS_r[] = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
  52.  
  53. void setup()
  54. {
  55.     pinMode(PIEZO, OUTPUT);
  56.     delay(2000);
  57.    
  58.     Serial.begin(115200);
  59. }
  60.  
  61. void loop()
  62. {
  63.     const int LEN = (sizeof(smoke_r) + 1) / 4;
  64.     Serial.println(LEN);
  65.     for (int i=0; i < LEN; i++)
  66.     {
  67.         int tom = smoke_m[i];
  68.         int tempo = smoke_r[i];
  69.  
  70.         long tvalue = tempo * vel;
  71.  
  72.         tocar(tom, tvalue);
  73.  
  74.         delayMicroseconds(1000); //pause between notes
  75.     }
  76. }
  77.  
  78. void tocar(int tom, long tempo_value)
  79. {
  80.      long tempo_gasto = 0;
  81.      while (tempo_gasto < tempo_value && tempo_value < 640000) // enters an infinite loop when tempo_value is a big value
  82.      {  
  83.         digitalWrite(PIEZO, HIGH);
  84.         delayMicroseconds(tom / 2);
  85.  
  86.         digitalWrite(PIEZO, LOW);
  87.         delayMicroseconds(tom/2);
  88.  
  89.         tempo_gasto += tom;
  90.     }
  91. }
Advertisement
RAW Paste Data Copied
Advertisement