Advertisement
pleasedontcode

"Buzzer Melody" rev_14

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 21:51:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* play melody of jingle bells. */
  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 uint8_t START_BUTTON_PIN = 7;
  33. const uint8_t STOP_BUTTON_PIN = 8;
  34. const uint8_t buzzer_PassiveBuzzer_Signal_PIN = 3;
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. ezBuzzer buzzer(buzzer_PassiveBuzzer_Signal_PIN);
  38.  
  39. // Melody for Jingle Bells
  40. int melody[] = {
  41.   NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4,
  42.   NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4,
  43.   NOTE_D4, NOTE_G4
  44. };
  45.  
  46. int noteDurations[] = {
  47.   4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 1,
  48.   4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2,
  49.   4, 4
  50. };
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(START_BUTTON_PIN, INPUT_PULLUP);
  56.     pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
  57. }
  58.  
  59. void loop(void)
  60. {
  61.     // put your main code here, to run repeatedly:
  62.     buzzer.loop();
  63.  
  64.     int startButtonState = digitalRead(START_BUTTON_PIN);
  65.     int stopButtonState = digitalRead(STOP_BUTTON_PIN);
  66.  
  67.     if (startButtonState == LOW)
  68.     {
  69.         if (buzzer.getState() == BUZZER_IDLE)
  70.         {
  71.             int length = sizeof(noteDurations) / sizeof(int);
  72.             buzzer.playMelody(melody, noteDurations, length);
  73.         }
  74.     }
  75.  
  76.     if (stopButtonState == LOW)
  77.     {
  78.         if (buzzer.getState() != BUZZER_IDLE)
  79.         {
  80.             buzzer.stop();
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement