Mario Piezo Speaker Theme Song
// Define the notes in the melody#define NOTE_E7 2637#define NOTE_C7 2093#define NOTE_G7 3136#define NOTE_G6 1568#define NOTE_E6 1319#define NOTE_A6 1760#define NOTE_B6 1976#define NOTE_AS6 1865#define NOTE_A6 1760#define NOTE_G6 1568#define NOTE_E7 2637#define NOTE_G7 3136#define NOTE_A7 3520#define NOTE_F7 2794#define NOTE_G7 3136#define NOTE_E7 2637#define NOTE_C7 2093#define NOTE_D7 2349#define NOTE_B6 1976#define NOTE_C7 2093
const int buttonPin = 2; // the number of the pushbutton pinconst int speakerPin = 8; // the number of the speaker pinconst int ledPin1 = 13; // the number of the first LED pinconst int ledPin2 = 12; // the number of the second LED pin
int buttonState = 0; // variable for reading the pushbutton statusint lastButtonState = HIGH; // previous state of the buttonunsigned long lastDebounceTime = 0; // the last time the output pin was toggledunsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int melody[] = { NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0,
NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0};
int noteDurations[] = { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12};
void setup() { pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input with pull-up resistor pinMode(speakerPin, OUTPUT); // initialize the speaker pin as an output pinMode(ledPin1, OUTPUT); // initialize the first LED pin as an output pinMode(ledPin2, OUTPUT); // initialize the second LED pin as an output}
void loop() { int reading = digitalRead(buttonPin); // read the state of the pushbutton
// If the switch changed, due to noise or pressing if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); }
if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state:
// if the button state has changed if (reading != buttonState) { buttonState = reading;
// only turn on the LED if the new button state is LOW if (buttonState == LOW) { playMarioTheme(); // play Mario Bros theme song } } }
// save the reading. Next time through the loop, it'll be the lastButtonState: lastButtonState = reading;}
void playMarioTheme() { for (int thisNote = 0; thisNote < sizeof(melody)/sizeof(melody[0]); thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; if (melody[thisNote] != 0) { tone(speakerPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
// Blink LEDs alternately if (thisNote % 2 == 0) { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); } else { digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); }
delay(pauseBetweenNotes); noTone(speakerPin); } else { delay(noteDuration); } }
// Turn off LEDs after the song is done digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW);}
const int buttonPin = 2; // the number of the pushbutton pinconst int speakerPin = 8; // the number of the speaker pinconst int ledPin1 = 13; // the number of the first LED pinconst int ledPin2 = 12; // the number of the second LED pin
int buttonState = 0; // variable for reading the pushbutton statusint lastButtonState = HIGH; // previous state of the buttonunsigned long lastDebounceTime = 0; // the last time the output pin was toggledunsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int melody[] = { NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0,
NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0};
int noteDurations[] = { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12};
void setup() { pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input with pull-up resistor pinMode(speakerPin, OUTPUT); // initialize the speaker pin as an output pinMode(ledPin1, OUTPUT); // initialize the first LED pin as an output pinMode(ledPin2, OUTPUT); // initialize the second LED pin as an output}
void loop() { int reading = digitalRead(buttonPin); // read the state of the pushbutton
// If the switch changed, due to noise or pressing if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); }
if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state:
// if the button state has changed if (reading != buttonState) { buttonState = reading;
// only turn on the LED if the new button state is LOW if (buttonState == LOW) { playMarioTheme(); // play Mario Bros theme song } } }
// save the reading. Next time through the loop, it'll be the lastButtonState: lastButtonState = reading;}
void playMarioTheme() { for (int thisNote = 0; thisNote < sizeof(melody)/sizeof(melody[0]); thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; if (melody[thisNote] != 0) { tone(speakerPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
// Blink LEDs alternately if (thisNote % 2 == 0) { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); } else { digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); }
delay(pauseBetweenNotes); noTone(speakerPin); } else { delay(noteDuration); } }
// Turn off LEDs after the song is done digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW);}
Donate
If you've enjoyed exploring my Arduino projects and want to see more amazing creations, your support can make a big difference! By contributing, you're helping me continue to innovate and bring even more exciting projects to life. Together, we can explore the endless possibilities of DIY electronics! Don't forget to like, subscribe, and follow for updates on the latest developments. Thank you for being a part of this journey!
Click here to make a difference with your donation today!