Advertisement
LeventeDaradici

Piezo Christmas Songs - Arduino

Jul 21st, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.17 KB | None | 0 0
  1.  
  2. #include "pitches.h"
  3.  
  4. #define melodyPin 8
  5.  
  6. // Jingle Bells
  7.  
  8. int melody[] = {
  9.   NOTE_E5, NOTE_E5, NOTE_E5,
  10.   NOTE_E5, NOTE_E5, NOTE_E5,
  11.   NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
  12.   NOTE_E5,
  13.   NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
  14.   NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
  15.   NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
  16.   NOTE_D5, NOTE_G5
  17. };
  18.  
  19. int tempo[] = {
  20.   8, 8, 4,
  21.   8, 8, 4,
  22.   8, 8, 8, 8,
  23.   2,
  24.   8, 8, 8, 8,
  25.   8, 8, 8, 16, 16,
  26.   8, 8, 8, 8,
  27.   4, 4
  28. };
  29.  
  30. // We wish you a merry Christmas
  31.  
  32. int wish_melody[] = {
  33.   NOTE_B3,
  34.   NOTE_F4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4,
  35.   NOTE_D4, NOTE_D4, NOTE_D4,
  36.   NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4,
  37.   NOTE_E4, NOTE_E4, NOTE_E4,
  38.   NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4,
  39.   NOTE_F4, NOTE_D4, NOTE_B3, NOTE_B3,
  40.   NOTE_D4, NOTE_G4, NOTE_E4,
  41.   NOTE_F4
  42. };
  43.  
  44. int wish_tempo[] = {
  45.   4,
  46.   4, 8, 8, 8, 8,
  47.   4, 4, 4,
  48.   4, 8, 8, 8, 8,
  49.   4, 4, 4,
  50.   4, 8, 8, 8, 8,
  51.   4, 4, 8, 8,
  52.   4, 4, 4,
  53.   2
  54. };
  55.  
  56. // Santa Claus is coming to town
  57.  
  58. int santa_melody[] = {
  59.   NOTE_G4,
  60.   NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
  61.   NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, NOTE_C5,
  62.   NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
  63.   NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4,
  64.   NOTE_E4, NOTE_G4, NOTE_C4, NOTE_E4,
  65.   NOTE_D4, NOTE_F4, NOTE_B3,
  66.   NOTE_C4
  67. };
  68.  
  69. int santa_tempo[] = {
  70.   8,
  71.   8, 8, 4, 4, 4,
  72.   8, 8, 4, 4, 4,
  73.   8, 8, 4, 4, 4,
  74.   8, 8, 4, 2,
  75.   4, 4, 4, 4,
  76.   4, 2, 4,
  77.   1
  78. };
  79.  
  80. int switchOne = 0;
  81. int switchTwo = 0;
  82. int switchThree = 0;
  83.  
  84. void setup(void) {
  85.   pinMode(8, OUTPUT); // Buzzer
  86.   pinMode(13, OUTPUT); // Led indicator when singing a note
  87.   pinMode(2, INPUT);
  88.   pinMode(3, INPUT);
  89.   pinMode(4, INPUT);
  90. }
  91.  
  92. void loop() {
  93.   switchOne = digitalRead(2);
  94.   switchTwo = digitalRead(3);
  95.   switchThree = digitalRead(4);
  96.  
  97.     sing(1);
  98.     delay(3000);
  99.     sing(2);
  100.     delay(3000);
  101.     sing(3);
  102.     delay(3000);
  103. }
  104.  
  105. int song = 0;
  106.  
  107. void sing(int s) {
  108.   // iterate over the notes of the melody:
  109.   song = s;
  110.   if (song == 3) {
  111.     Serial.println(" 'We wish you a Merry Christmas'");
  112.     int size = sizeof(wish_melody) / sizeof(int);
  113.     for (int thisNote = 0; thisNote < size; thisNote++) {
  114.  
  115.       // to calculate the note duration, take one second
  116.       // divided by the note type.
  117.       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  118.       int noteDuration = 1000 / wish_tempo[thisNote];
  119.  
  120.       buzz(melodyPin, wish_melody[thisNote], noteDuration);
  121.  
  122.       // to distinguish the notes, set a minimum time between them.
  123.       // the note's duration + 30% seems to work well:
  124.       int pauseBetweenNotes = noteDuration * 1.30;
  125.       delay(pauseBetweenNotes);
  126.  
  127.       // stop the tone playing:
  128.       buzz(melodyPin, 0, noteDuration);
  129.  
  130.     }
  131.   } else if (song == 2) {
  132.     Serial.println(" 'Santa Claus is coming to town'");
  133.     int size = sizeof(santa_melody) / sizeof(int);
  134.     for (int thisNote = 0; thisNote < size; thisNote++) {
  135.  
  136.       // to calculate the note duration, take one second
  137.       // divided by the note type.
  138.       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  139.       int noteDuration = 900 / santa_tempo[thisNote];
  140.  
  141.       buzz(melodyPin, santa_melody[thisNote], noteDuration);
  142.  
  143.       // to distinguish the notes, set a minimum time between them.
  144.       // the note's duration + 30% seems to work well:
  145.       int pauseBetweenNotes = noteDuration * 1.30;
  146.       delay(pauseBetweenNotes);
  147.  
  148.       // stop the tone playing:
  149.       buzz(melodyPin, 0, noteDuration);
  150.  
  151.     }
  152.   } else {
  153.  
  154.     Serial.println(" 'Jingle Bells'");
  155.     int size = sizeof(melody) / sizeof(int);
  156.     for (int thisNote = 0; thisNote < size; thisNote++) {
  157.  
  158.       // to calculate the note duration, take one second
  159.       // divided by the note type.
  160.       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  161.       int noteDuration = 1000 / tempo[thisNote];
  162.  
  163.       buzz(melodyPin, melody[thisNote], noteDuration);
  164.  
  165.       // to distinguish the notes, set a minimum time between them.
  166.       // the note's duration + 30% seems to work well:
  167.       int pauseBetweenNotes = noteDuration * 1.30;
  168.       delay(pauseBetweenNotes);
  169.  
  170.       // stop the tone playing:
  171.       buzz(melodyPin, 0, noteDuration);
  172.  
  173.     }
  174.   }
  175. }
  176.  
  177. void buzz(int targetPin, long frequency, long length) {
  178.   digitalWrite(13, HIGH);
  179.   long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  180.   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  181.   //// there are two phases to each cycle
  182.   long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  183.   //// multiply frequency, which is really cycles per second, by the number of seconds to
  184.   //// get the total number of cycles to produce
  185.   for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  186.     digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  187.     delayMicroseconds(delayValue); // wait for the calculated delay value
  188.     digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  189.     delayMicroseconds(delayValue); // wait again or the calculated delay value
  190.   }
  191.   digitalWrite(13, LOW);
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement