Advertisement
pleasedontcode

"Melody Display" rev_01

Jan 5th, 2025
283
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: "Melody Display"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-01-05 17:55:14
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* plays crazy story by king von */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Arduino.h> // Ensure to include the Arduino library for tone and digitalWrite functions
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  33. const uint8_t buzz_ActiveBuzzer_output_PIN_D2 = 2;
  34. #define BUZZER_PIN 8 // Using a different pin for the buzzer
  35. #define LED_PIN 13 // Define LED pin
  36.  
  37. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  38. /***** used to store raw data *****/
  39. bool buzz_ActiveBuzzer_output_PIN_D2_rawData = 0;
  40.  
  41. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  42. /***** used to store data after characteristic curve transformation *****/
  43. float buzz_ActiveBuzzer_output_PIN_D2_phyData = 0.0;
  44.  
  45. // Define the notes and durations for the melody
  46. int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
  47. int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
  48.  
  49. // Define the lyrics for the story
  50. String lyrics[] = {"I was in the trenches, yeah, I was in the field",
  51.                    "I was in the trenches, yeah, I was in the field",
  52.                    "I was in the trenches, yeah, I was in the field",
  53.                    "I was in the trenches, yeah, I was in the field",
  54.                    "I was in the trenches, yeah, I was in the field",
  55.                    "I was in the trenches, yeah, I was in the field",
  56.                    "I was in the trenches, yeah, I was in the field",
  57.                    "I was in the trenches, yeah, I was in the field"};
  58.  
  59. // Define the tempo for the melody
  60. int tempo = 120;
  61.  
  62. // Define the current note and duration index
  63. int noteIndex = 0;
  64. int durationIndex = 0;
  65.  
  66. // Define the current lyric index
  67. int lyricIndex = 0;
  68.  
  69. void setup(void)
  70. {
  71.     // put your setup code here, to run once:
  72.     pinMode(buzz_ActiveBuzzer_output_PIN_D2, OUTPUT);
  73.     pinMode(BUZZER_PIN, OUTPUT); // Set the buzzer pin as output
  74.     pinMode(LED_PIN, OUTPUT); // Set the LED pin as output
  75.     Serial.begin(9600); // Initialize serial communication for printing lyrics
  76. }
  77.  
  78. void loop(void)
  79. {
  80.     // put your main code here, to run repeatedly:
  81.     updateOutputs(); // Refresh output data
  82.  
  83.     // Calculate the duration of the current note
  84.     int noteDuration = 1000 / noteDurations[durationIndex];
  85.  
  86.     // Play the current note on the buzzer
  87.     tone(BUZZER_PIN, melody[noteIndex], noteDuration);
  88.  
  89.     // Turn on the LED to indicate the current lyric
  90.     digitalWrite(LED_PIN, HIGH);
  91.  
  92.     // Print the current lyric to the serial monitor
  93.     Serial.println(lyrics[lyricIndex]);
  94.  
  95.     // Wait for the duration of the note
  96.     delay(noteDuration);
  97.  
  98.     // Turn off the LED
  99.     digitalWrite(LED_PIN, LOW);
  100.  
  101.     // Stop the buzzer
  102.     noTone(BUZZER_PIN);
  103.  
  104.     // Increase the note and duration index
  105.     noteIndex++;
  106.     durationIndex++;
  107.  
  108.     // Check if the end of the melody has been reached
  109.     if (noteIndex >= sizeof(melody) / sizeof(int)) {
  110.         // Reset the note and duration index
  111.         noteIndex = 0;
  112.         durationIndex = 0;
  113.     }
  114.  
  115.     // Check if the end of the lyrics has been reached
  116.     if (lyricIndex >= sizeof(lyrics) / sizeof(String)) {
  117.         // Reset the lyric index
  118.         lyricIndex = 0;
  119.     }
  120.  
  121.     // Wait for the duration of a quarter note
  122.     delay(60000 / tempo);
  123. }
  124.  
  125. void updateOutputs()
  126. {
  127.     digitalWrite(buzz_ActiveBuzzer_output_PIN_D2, buzz_ActiveBuzzer_output_PIN_D2_rawData);
  128. }
  129.  
  130. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement