#include "tutornyanyi.h"
#define buZZER 8
const uint8_t pinTombol[] = { 12, 11, 10, 9, 7, 6, 5, 4, 3, 1 };
const int nadaTombol[] = {
NADA_G3, NADA_A3, NADA_B3, NADA_C4,
NADA_D4, NADA_E4, NADA_F4, NADA_G4, NADA_A4,
};
// Definisikan pin
const int tombol1 = 2;
const int tombol2 = 1;
// Variabel untuk menyimpan status tombol
int buttonState1 = 0;
int buttonState2 = 0;
// Nada-nada lagu
int melody1[] = {
220, 349, 0, 349, 0, 349, 0, 330, 294, 262, 220, 262, 349, 330, 349, 392, 294, 294, 294, // Apa sudah ada kabar lain yang kautunggu?
196, 196, 196, 262, 294, 262, 247, 262, 294, 294, 294, 294, 294, // Sudah adakah yang gantikanku
196, 196, 196, 262, 294, 262, 247, 262, 330, 330, 330, 330, 330, // Yang khawatirkanmu setiap waktu
196, 220, 220, 220, 262, 247, 247, 262, 294, 330, 349, 294, 330, 294, 262, 330, // Yang cerita tentang apa pun sampai hal-hal tak perlu
330, 349, 349, 349, 392, 440, 392, 392, 330, 392, 392, 392, 392, 392, // Kalau bisa, jangan buru-buru
330, 349, 349, 349, 392, 330, 294, 294, 262, 262, 262, 262, 0 // Kalau bisa, jangan ada dulu
};
int melody2[] = {
196, 262, 294, 262, 294, 262, 196, 262, 196, 262, 294, 262, 294, 262, 196, 262, // Intro
247, 262, 0, 262, 0, 262, 262, 262, 0, 262, 0, 262, 0, 262, 0, 247, 262, 262, 294, 294, 262, 262, 262, // We were both young when I first saw you
247, 262, 262, 262, 0, 262, 262, 262, 0, 262, 0, 262, 0, 262, 0, 247, 262, 262, 294, 294, // I close my eyes and the flashback starts
262, 0, 262, 247, 220, 220, 220, 220, 220, 220, 220, 220, 220, 220, 220, // I'm standin' there
262, 0, 262, 0, 247, 0, 247, 0, 196, 0, 196, 0, 220, 0, 220, 220, 220, 0,// On a balcony in summer air
247, 0, 247, 0, 262, 262, 0, 262, 0, 262, 0, 247, 262, 262, 294, 294, 262, 262, 262, // That you were Romeo, you were throwin' pebbles
247, 262, 294, 262, 330, 262, 294, 262, 392, 349, 349, 330, 330, 0, // And my daddy said, "Stay away from Juliet"
330, 0, 330, 0, 330, 0, 330, 0, 330, 0, 294, 0, 294, 0, 294, 0, 262, 0, 349, // And I was cryin' on the staircase, Beggin' you, "Please don't go, "
330, 294, 294, 262, 0 // And I said
};
// Durasi nada
int noteDurations1[] = {
9, 18, 18, 18, 18, 18, 25, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
};
int noteDurations2[] = {
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 9, 9, 9, 9, 9, 9, 9, 9,
9, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 9, 9, 9, 9, 9,
18, 18, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 11, 30, 9, 1, 2, 9,
11, 30, 11, 30, 9, 9, 9, 9, 9, 9 ,9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1,
9, 9, 9, 9, 0
};
const int numTones = sizeof(pinTombol) / sizeof(pinTombol[0]);
bool isPlaying = false; // Variabel untuk melacak apakah lagu sedang dimainkan
void setup() {
for (uint8_t i = 0; i < numTones; i++) {
pinMode(pinTombol[i], INPUT_PULLUP);
}
pinMode(tombol1, INPUT_PULLUP);
pinMode(tombol2, INPUT_PULLUP);
pinMode(buZZER, OUTPUT);
}
void loop() {
int nada = 0;
for (uint8_t i = 0; i < numTones; i++) {
if (digitalRead(pinTombol[i]) == LOW) {
nada = nadaTombol[i];
}
}
if (nada) {
tone(buZZER, nada);
} else {
noTone(buZZER);
}
buttonState1 = digitalRead(tombol1);
// Jika tombol ditekan (status LOW)
if (buttonState1 == LOW && !isPlaying) {
isPlaying = true; // Setel flag ke true
// Mainkan melodi
for (int thisNote1 = 0; thisNote1 < 250; thisNote1++) {
// Menghitung durasi nada
int duration1 = 1000 / noteDurations1[thisNote1];
tone(buZZER, melody1[thisNote1], duration1); // Mainkan nada
int pauseBetweenNotes1 = duration1 * 1.30; // Pause antara nada
delay(pauseBetweenNotes1); // Tunggu sebelum nada berikutnya
noTone(buZZER); // Matikan nada
}
isPlaying = false; // Setel flag kembali ke false setelah selesai
delay(1000); // Tunggu 1 detik sebelum dapat menekan lagi
}
//tombol 2
buttonState2 = digitalRead(tombol2);
// Jika tombol ditekan (status LOW)
if (buttonState2 == LOW && !isPlaying) {
isPlaying = true; // Setel flag ke true
// Mainkan melodi
for (int thisNote2 = 0; thisNote2 < 250; thisNote2++) {
// Menghitung durasi nada
int duration2 = 1000 / noteDurations2[thisNote2];
tone(buZZER, melody2[thisNote2], duration2); // Mainkan nada
int pauseBetweenNotes2 = duration2 * 1.30; // Pause antara nada
delay(pauseBetweenNotes2); // Tunggu sebelum nada berikutnya
noTone(buZZER); // Matikan nada
}
isPlaying = false; // Setel flag kembali ke false setelah selesai
delay(1000); // Tunggu 1 detik sebelum dapat menekan lagi
}
}