MacGyver Soundbox

Manchmal fehlt beim Tüfteln nur ein kleiner Schub Motivation – und was könnte da besser helfen als die legendäre MacGyver-Titelmelodie?
Genau dafür habe ich dieses Projekt gebaut: Ein einziger Knopfdruck genügt, und der Piezo-Lautsprecher spielt die komplette Hymne meines Bastel-Idols. Ob als Gag, Motivation oder einfach zum Spass – MacGyver ist nur einen Knopfdruck entfernt.

Natürlich in schlechter Qualität, aber es war eine kleine Qual, die Notenblätter manuel in Frequenz und Dauer umzuschreiben und dass will ich selber einfach würdigen. Aber eine schöne Hülle fehlt noch.

Bauteile

Folgende Bauteile wurden verwendet: Kleinkram:

Skizzen, Fotos und Dateien

// ====================================================================================90
// goal:   press button to play music (MacGyver theme song)
// author: Simon 'Saasi' Burgener
// board:  DFRobot Beetle ESP32-C6
// ====================================================================================90

// libraries --------------------------------------------------------------------------90
#include 

// pin settings -----------------------------------------------------------------------90
#define pin_btn 7 // button pin
#define pin_speaker 6  // piezo speaker

// objects & variables ----------------------------------------------------------------90
ezButton btn(pin_btn); // create ezButton objects
bool ButtonPressed = false; // Variable to track if the button was pressed

// setup loop =========================================================================90
void setup() {
  btn.setDebounceTime(50);  // set debounce time for button
  pinMode(pin_speaker, OUTPUT); // button pin setup
} // end of setup loop

// running loop =======================================================================90
void loop() {
  // button loops
  btn.loop();  // MUST be called in the loop() function first

  // check if the red button is pressed
  if (btn.isPressed() && !ButtonPressed) {
    ButtonPressed = true;

    // song sources: https://musescore.com/user/1003251/scores/1140736 (15.08.2024)
    // MacGyver Theme Song by Randy Edelmann / Emma Grigg
    // notes frequencies (in Hz)
    int noten[] = {
      247, 330, 440, 494, 440, 330, 247, 247, 330, 440, 494, 440, 330, 247, 247, 330, 440, 494, 440, 330, 247, 0, 440, 587, 523, 587, 523, 494, 247, 330, 440, 494, 440, 330, 247, 247, 330, 440, 494, 440, 330, 247, 247, 330, 440, 494, 440, 330, 247, 0, 440, 587, 523, 587, 523, 494, 440,
      494, 0, 494, 494, 494, 0, 440, 440, 587, 494, 0, 494, 494, 494, 440,
      523, 523, 523, 523, 523, 523, 523, 523, 494, 370, 440, 392, 392, 523, 523, 494, 494, 440, 494, 440, 392, 659,
      440, 523, 523, 494, 494, 370, 440, 392, 392, 523, 523, 494, 494, 440, 494, 440, 392, 659,
      440, 494, 523, 523, 494, 440, 523, 523, 494, 440, 587, 587, 523, 494, 587, 587, 523, 494, 659, 659, 587, 659, 740, 740, 494,
      784, 740, 740, 494, 784, 659, 494, 740, 587, 440, 659, 523, 392, 587, 494, 392, 523, 330, 494, 294, 523, 494, 440, 392, 466, 440,
      392, 523, 523, 523, 523, 523, 523, 494, 370, 440, 392, 392, 523, 523, 494, 494, 440, 494, 440, 392, 659, 440, 523, 523, 494, 494, 370, 440, 392, 392, 523, 523, 494, 494, 440, 494, 440, 392, 659, 440, 494, 523, 523, 494, 440, 523, 523, 494, 440, 587, 587, 523, 494, 587, 587, 523, 494, 659, 659, 587, 659, 740, 740, 494, 784, 740, 740, 494, 784, 659, 494, 740, 587, 440, 659, 523, 392, 587, 494, 392, 523, 330, 494, 294, 523, 494, 440, 392, 466, 440, 392, 523, 523, 523, 523, 523, 523, 494, 370, 440, 392, 392, 247, 294, 392, 523, 523, 494,
      494, 440, 494, 440, 392, 659, 440, 523, 523, 494, 494, 370, 440, 392, 392, 247, 294, 392, 523, 523, 494,
      494, 440, 494, 440, 392, 659, 440, 494, 523, 587, 659, 784,
      1568, 784, 1175, 523, 1319, 659, 1047, 988, 784, 392, 587, 392, 311, 392, 466, 392, 392, 392, 392, 392, 392, 392, 392, 0
    };

    // play duration of each note (in ms) / 4 strokes = 1200 ms
    int time[] = {
      150, 150, 150, 150, 300, 150, 150, 150, 150, 150, 150, 150, 150, 300, 150, 150, 150, 150, 300, 150, 150, 150, 150, 150, 150, 150, 150, 300, 150, 150, 150, 150, 300, 150, 150, 150, 150, 150, 150, 150, 150, 300, 150, 150, 150, 150, 300, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      300, 150, 150, 600, 300, 150, 150, 150, 450, 300, 150, 150, 600, 300, 750,
      150, 150, 150, 150, 150, 150, 150, 150, 600, 150, 300, 150, 600, 150, 300, 150, 150, 150, 150, 150, 300, 300,
      600, 150, 300, 150, 600, 150, 300, 150, 600, 150, 300, 150, 150, 150, 150, 150, 300, 300,
      600, 450, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 300, 300,
      300, 300, 300, 300, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 300, 300,
      300, 150, 150, 150, 150, 150, 150, 600, 150, 300, 150, 600, 150, 300, 150, 150, 150, 150, 150, 300, 300, 600, 150, 300, 150, 600, 150, 300, 150, 600, 150, 300, 150, 150, 150, 150, 150, 300, 300, 600, 300, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 300, 300, 300, 300, 300, 300, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 300, 300, 300, 150, 150, 150, 150, 150, 150, 600, 150, 300, 150, 150, 150, 150, 150, 150, 300, 150,
      150, 150, 150, 150, 300, 300, 600, 150, 300, 150, 600, 150, 300, 150, 150, 150, 150, 150, 150, 300, 150,
      150, 150, 150, 150, 300, 300, 600, 600, 600, 600, 600, 600,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150

    };

    // iterate through song and play note
    for (int i = 0; i < sizeof(noten) / sizeof(noten[0]); i++) {
      playTone(noten[i], time[i]);
    }

  } else if (!btn.isPressed()) {
    ButtonPressed = false;  // reset the flag when the button is released
  }

} // end of running loop

// functions ==========================================================================90
// create tone (ESP32 compatible)
void playTone(int frequency, int duration) {
  if (frequency == 0) {
    delay(duration);  // Pause
  } else {
    int period = 1000000 / frequency;  // calculate period in micro-seconds
    int halfPeriod = period / 2;       // half time for HIGH/LOW-level

    for (int i = 0; i < duration * 1000L / period; i++) {
      digitalWrite(pin_speaker, HIGH);
      delayMicroseconds(halfPeriod);
      digitalWrite(pin_speaker, LOW);
      delayMicroseconds(halfPeriod);
    }
  }
  delay(20);  // short waiting time between notes
}