Advertisement
pleasedontcode

"Buzzer Melody" rev_18

Dec 19th, 2023
110
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:30:37
  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 <toneAC.h>
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void playJingleBellsMelody(void);
  31.  
  32. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  33. const uint8_t alarm_ActiveBuzzer_output_PIN_D2 = 2;
  34.  
  35. /***** DEFINITION OF MELODY NOTES *****/
  36. #define NOTE_E4  330
  37. #define NOTE_G4  392
  38. #define NOTE_C4  261
  39. #define NOTE_D4  294
  40. #define NOTE_F4  349
  41.  
  42. void setup(void)
  43. {
  44.   // Set the buzzer pin as output
  45.   pinMode(alarm_ActiveBuzzer_output_PIN_D2, OUTPUT);
  46. }
  47.  
  48. void loop(void)
  49. {
  50.   // Play the jingle bells melody once
  51.   playJingleBellsMelody();
  52.  
  53.   // Wait for a few seconds before playing again
  54.   delay(5000);
  55. }
  56.  
  57. void playJingleBellsMelody(void)
  58. {
  59.   // Melody notes and durations
  60.   const int melodyNotes[] = {NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4, 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, NOTE_D4, NOTE_G4};
  61.   const int melodyDurations[] = {8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4};
  62.  
  63.   // Play the melody
  64.   for (int i = 0; i < sizeof(melodyNotes) / sizeof(melodyNotes[0]); i++)
  65.   {
  66.     toneAC(alarm_ActiveBuzzer_output_PIN_D2, melodyNotes[i], melodyDurations[i] * 100);
  67.     delay(melodyDurations[i] * 100);
  68.     noTone(alarm_ActiveBuzzer_output_PIN_D2);
  69.     delay(50);
  70.   }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement