// LAB 1; ADRIANA SARAH BINTI AHMADA FAIZA ; 52224123439 L01-B01
int buzzerPin = 8; // digital pin connected to the buzzer
int switchPin = 11; // digital pin connected to the switch
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600); // initialize serial communication for debugging
Serial.println("Setup complete, ready to play melody");
}
void loop() {
int switchState = digitalRead(switchPin);
if (switchState == 1) { // button pressed
delay(50); // Debounce delay
if (digitalRead(switchState) == 1, HIGH) { // to confirm button is still pressed
Serial.println("Button is pressed."); }
playMelody();
}
}
void playMelody() {
Serial.println("Playing Gravity Falls Theme Song...");
// plays the Gravity Falls theme snippet directly using tone()
tone(buzzerPin, 587, 900); // d5
delay(950); // duration + delay
tone(buzzerPin, 659, 300); // e5
delay(350);
tone(buzzerPin, 698, 600); // f5
delay(650);
delay(500); // pause
tone(buzzerPin, 880, 400); // a5
delay(450);
tone(buzzerPin, 783, 450); // g5
delay(500);
tone(buzzerPin, 880, 300); // a5
delay(350);
tone(buzzerPin, 523, 800); // c5
delay(850);
delay(300); // pause
tone(buzzerPin, 587, 900); // d5
delay(950);
tone(buzzerPin, 659, 300); // e5
delay(350);
tone(buzzerPin, 698, 600); // f5
delay(650);
tone(buzzerPin, 659, 700); // e5
delay(750);
tone(buzzerPin, 783, 500); // g5
delay(550);
tone(buzzerPin, 880, 600); // a5
delay(650);
tone(buzzerPin, 783, 600); // g5
delay(650);
tone(buzzerPin, 698, 700); // f5
delay(750);
delay(400); // pause
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 783, 350); // g5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
delay(400); // pause
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 783, 350); // g5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 783, 350); // g5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
delay(400); // pause
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 783, 350); // g5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
delay(400); // pause
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
delay(400); // pause
tone(buzzerPin, 1108, 350); // c6#
delay(400);
tone(buzzerPin, 1108, 350); // c6#
delay(400);
tone(buzzerPin, 1108, 350); // c6#
delay(400);
delay(400); // pause
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 880, 350); // a5
delay(400);
tone(buzzerPin, 783, 350); // g5
delay(400);
tone(buzzerPin, 698, 350); // f5
delay(400);
delay(400); // pause
tone(buzzerPin, 932, 350); // a5#
delay(400);
tone(buzzerPin, 932, 350); // a5#
delay(400);
tone(buzzerPin, 932, 350); // a5#
delay(400);
tone(buzzerPin, 783, 700); // g5
delay(750);
tone(buzzerPin, 1046, 700); // c6
delay(750);
tone(buzzerPin, 880, 700); // a5
delay(750);
tone(buzzerPin, 1108, 700); // c6#
delay(750);
tone(buzzerPin, 1174, 1200); // d6
delay(1250);
delay(1000); // Pause before repeating
Serial.println("End of Gravity Falls Theme Song.");
}