// --- Indonesia Raya - Arduino UNO + Passive Buzzer ---
// Wiring: (+) buzzer ke pin 8, (-) ke GND
// Catatan: ini untuk buzzer pasif (piezo). Untuk buzzer aktif, cukup ON/OFF digitalWrite.
// === KONFIGURASI ===
#define BUZZER_PIN 23
float tempoScale = 1.0; // >1.0 = lebih lambat, <1.0 = lebih cepat (mis. 0.9)
// === MELODY & DURASI (ms) ===
// Sumber melodi diadaptasi dari proyek Wokwi berisi nada lengkap Indonesia Raya. :contentReference[oaicite:1]{index=1}
int melody[] = {
262, 277, 311, 523, 0, 523, 466, 466, 415, 311, 0, 311, 311, 349, 311, 277, 262, 233, 0, 233, 262, 277, 466, 0, 466, 415, 415, 392, 349, 0, 311, 311, 392, 349, 311, 277, 262, 0, 262, 277, 311, 523, 0, 523, 466, 466, 415, 311, 0, 311, 311, 349, 311, 415, 466, 392, 349, 349, 349, 554, 554, 523, 466, 622, 415, 392, 349, 311, 554, 523, 466, 415, 0, 311, 311, 349, 554, 554, 554, 554, 554, 523, 415, 415, 415, 392, 415, 466, 622, 622, 622, 554, 554, 523, 415, 311, 311, 349, 554, 554, 554, 554, 554, 523, 415, 415, 415, 392, 415, 466, 622, 622, 523, 466, 415, 0, 415, 415, 554, 698, 698, 698, 698, 698, 622, 523, 523, 523, 622, 622, 554, 466, 466, 466, 622, 554, 523, 415, 415, 415, 554, 698, 698, 698, 698, 698, 622, 523, 523, 523, 622, 622, 622, 554, 523, 466, 523, 466, 415, 0, 415, 415, 622, 554, 523, 466, 523, 466, 415, 0, 0
};
int noteDurations[] = {
468, 156, 624, 624, 468, 156, 468, 156, 624, 624, 624, 468, 156, 624, 624, 624, 624, 1248, 624, 468, 156, 624, 624, 468, 156, 468, 156, 624, 624, 624, 468, 156, 624, 624, 624, 624, 1248, 624, 468, 156, 624, 624, 468, 156, 468, 156, 624, 624, 624, 468, 156, 624, 624, 624, 624, 1248, 624, 468, 156, 624, 624, 624, 624, 1248, 624, 468, 156, 624, 624, 624, 624, 1248, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 1248, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 624, 624, 468, 156, 1248, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 1248, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 1248, 624, 468, 156, 624, 468, 156, 624, 468, 156, 1248, 1248, 0
};
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
// main loop dipanggil sekali di sini supaya hanya dimainkan sekali:
playIndonesiaRaya();
}
void loop() {
// kosong (lagu sudah dimainkan di setup).
// Jika ingin berulang, panggil playIndonesiaRaya() di sini.
}
void playIndonesiaRaya() {
int n = sizeof(melody) / sizeof(melody[0]);
for (int i = 0; i < n; i++) {
int freq = melody[i];
int dur = (int)(noteDurations[i] * tempoScale);
if (freq <= 0) {
noTone(BUZZER_PIN);
delay(dur);
} else {
// mainkan ~90% dari durasi agar ada jeda antar nada
int playDur = (int)(dur * 0.9);
tone(BUZZER_PIN, freq, playDur);
delay(dur);
noTone(BUZZER_PIN);
}
}
}