const int buttonPin1 = 34; // Ganti D7 dengan pin yang sesuai untuk koneksi tombol
const int buttonPin2 = 35; // Ganti D6 dengan pin yang sesuai untuk koneksi tombol
const int buttonPinplaypause = 32; // Ganti D5 dengan pin yang sesuai untuk koneksi tombol
const int BUZZER_PIN = 18; // Ganti D1 dengan pin yang sesuai untuk koneksi buzzer
bool musicState = false;
int button1State = 0;
int button2State = 0;
int button3State = 0;
void playMelody1() {
// Definisikan ritme lagu 1
tone(BUZZER_PIN, 659, 100); // Contoh: not A selama 500 ms
delay(50); // Jeda antara not-not
tone(BUZZER_PIN, 587, 100);
delay(50);
tone(BUZZER_PIN, 440,100);
delay(50);
tone(BUZZER_PIN, 659, 100);
delay(50);
tone(BUZZER_PIN, 587, 100);
delay(100);
noTone(BUZZER_PIN);
}
void playMelody2() {
// Definisikan ritme lagu 2
tone(BUZZER_PIN, 587, 100); // Contoh: not D selama 500 ms
delay(50); // Jeda antara not-not
tone(BUZZER_PIN, 640, 100);
delay(50);
tone(BUZZER_PIN, 729, 100);
delay(50);
tone(BUZZER_PIN, 440, 100);
delay(50);
tone(BUZZER_PIN, 659, 100);
delay(50);
tone(BUZZER_PIN, 729,100);
delay(100);
noTone(BUZZER_PIN);
}
void playMelody3() {
// Definisikan ritme lagu 3
tone(BUZZER_PIN, 592, 100); // Contoh: not E selama 500 ms
delay(50); // Jeda antara not-not
tone(BUZZER_PIN, 440, 100);
delay(50);
tone(BUZZER_PIN, 330, 100);
delay(50);
tone(BUZZER_PIN, 659, 100);
delay(50);
tone(BUZZER_PIN, 729, 100);
delay(50);
tone(BUZZER_PIN, 529, 100);
delay(100);
noTone(BUZZER_PIN);
}
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPinplaypause, INPUT);
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
int newButton1State = digitalRead(buttonPin1);
int newButton2State = digitalRead(buttonPin2);
int newButton3State = digitalRead(buttonPinplaypause);
if (newButton1State == HIGH && button1State == LOW) {
// Tombol 1 - Memainkan ritme lagu 1
playMelody1();
analogWrite(5, 2000);
delay(500);
analogWrite(5, LOW);
}
if (newButton2State == HIGH && button2State == LOW) {
// Tombol 2 - Memainkan ritme lagu 2
playMelody2();
analogWrite(4, 2000);
delay(1000);
analogWrite(4, LOW);
}
if (newButton3State == HIGH && button3State == LOW) {
// Tombol 3 - Memainkan ritme lagu 3
playMelody3();
analogWrite(2, 2000);
delay(1000);
analogWrite(2, LOW);
}
// Perbarui status tombol untuk iterasi berikutnya
button1State = newButton1State;
button2State = newButton2State;
button3State = newButton3State;
}