Advertisement
pleasedontcode

"Buzzer Melody" rev_16

Dec 19th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Buzzer Melody"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-19 22:06:30
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* play melody of jingle bells one time. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Arduino.h>
  25. #include <ezBuzzer.h>
  26.  
  27. /****** FUNCTION PROTOTYPES ******/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  32. const int BUZZER_PIN = 2;
  33.  
  34. // Create an instance of ezBuzzer class with the buzzer pin
  35. ezBuzzer buzzer(BUZZER_PIN);
  36.  
  37. /*********** SYSTEM REQUIREMENTS ***********/
  38. /*********** SYSTEM REQUIREMENT 1 ***********/
  39. // Play melody of jingle bells one time
  40. int melody[] = {
  41.   NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4,
  42.   NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4,
  43.   NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
  44.   NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4,
  45.   NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4
  46. };
  47. int noteDurations[] = {
  48.   8, 8, 4, 8, 8, 32,
  49.   8, 8, 8, 8, 4,
  50.   8, 8, 8, 8, 8,
  51.   4, 4, 4, 4, 2,
  52.   4, 4, 2, 4, 4, 2
  53. };
  54.  
  55. void setup(void) {
  56.   // Initialize the Serial communication
  57.   Serial.begin(9600);
  58.  
  59.   // Set buzzer pin as OUTPUT
  60.   pinMode(BUZZER_PIN, OUTPUT);
  61. }
  62.  
  63. void loop(void) {
  64.   // Update the buzzer state
  65.   buzzer.loop();
  66.  
  67.   // Play jingle bells melody once
  68.   static bool jingleBellsPlayed = false;
  69.  
  70.   if (!jingleBellsPlayed && buzzer.getState() == BUZZER_IDLE) {
  71.     int length = sizeof(noteDurations) / sizeof(int);
  72.  
  73.     // Play the melody
  74.     buzzer.playMelody(melody, noteDurations, length);
  75.     jingleBellsPlayed = true;
  76.   }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement