Advertisement
JonD1988

Stargate_Song_No_Delay_Rev0

May 19th, 2023
1,727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int buzzerPin = 8;
  2. const int ledPin1 = 12;
  3. const int ledPin2 = 13;
  4.  
  5. int counter = 0;
  6. unsigned long previousMillis = 0;
  7. unsigned long noteDuration = 0;
  8. float tempoFactor = 1.75; // Adjust the tempo factor as needed
  9.  
  10. void setup()
  11. {
  12.   pinMode(buzzerPin, OUTPUT);
  13.   pinMode(ledPin1, OUTPUT);
  14.   pinMode(ledPin2, OUTPUT);
  15. }
  16.  
  17. void loop()
  18. {
  19.   // Play first section
  20.   firstSectionSG();
  21.  
  22.   // Play second section
  23. //  secondSectionSG();
  24.  
  25.   // Play third section
  26. //  thirdSectionSG();
  27.  
  28.   // Play fourth section
  29. //  fourthSectionSG();
  30.  
  31.   // Play fifth section
  32. //  fifthSectionSG();
  33. }
  34.  
  35. void beep(int note, int duration)
  36. {
  37.   unsigned long currentMillis = millis();
  38.  
  39.   if (currentMillis - previousMillis >= noteDuration)
  40.   {
  41.     tone(buzzerPin, note, duration);
  42.     previousMillis = currentMillis;
  43.     noteDuration = duration;
  44.  
  45.     if (counter % 2 == 0)
  46.     {
  47.       digitalWrite(ledPin1, HIGH);
  48.       delay(duration);
  49.       digitalWrite(ledPin1, LOW);
  50.     }
  51.     else
  52.     {
  53.       digitalWrite(ledPin2, HIGH);
  54.       delay(duration);
  55.       digitalWrite(ledPin2, LOW);
  56.     }
  57.  
  58.     noTone(buzzerPin);
  59.     delay(50);
  60.  
  61.     counter++;
  62.   }
  63. }
  64.  
  65. // Implement the sections using the same approach as beep() function
  66.  
  67. void firstSectionSG()
  68. {
  69.   beep(261, (int)(250 * tempoFactor));
  70.   beep(391, (int)(750 * tempoFactor));
  71.   beep(391, (int)(250 * tempoFactor));
  72.   beep(523, (int)(250 * tempoFactor));
  73.   beep(466, (int)(500 * tempoFactor));
  74.   beep(415, (int)(125 * tempoFactor));
  75.   beep(391, (int)(125 * tempoFactor));
  76.   beep(415, (int)(125 * tempoFactor));
  77.   beep(391, (int)(750 * tempoFactor));
  78.   delay((int)(125 * tempoFactor)); // Eighth note rest
  79.   beep(391, (int)(62 * tempoFactor));
  80.   beep(440, (int)(62 * tempoFactor));
  81.   beep(466, (int)(62 * tempoFactor));
  82.   beep(493, (int)(62 * tempoFactor));
  83.   beep(523, (int)(125 * tempoFactor));
  84.   delay((int)(125 * tempoFactor)); // Eighth note rest
  85.   delay((int)(250 * tempoFactor)); // Quarter note rest
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement