// MADE BY CAKRAYP
// Buzzer dengan membawakan lagu nada "Do-Re-Mi-Fa-So-La-Ti-Do"
//
// Dikutip dari sumber dokumentasi dan project.
// Dokumentasi : (https://docs.wokwi.com/parts/wokwi-buzzer)
// Mini Piano : (https://wokwi.com/projects/291958456169005577)
// Simon : (https://wokwi.com/projects/344891334169985618)
#include "pitches.h"
#define BuzzerPin 9
#define Btn_Pin A0
#define LedPin 13
// Nada untuk Buzzer
const int Tones[] = { // => Do-Re-Mi-Fa-So-La-Ti-Do
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numCountTones = sizeof(Tones) / sizeof(Tones[0]); // Jumlah Data dalam array
// Delay
int DelayAfterPlay = 0;
// Main
void setup() {
Serial.begin(9600);
Serial.println("<Arduino UNO is ready>");
Serial.println("Press the blue button to play...");
// Setup PIN
pinMode(Btn_Pin, INPUT); // Button
pinMode(BuzzerPin, OUTPUT); // Buzzer
pinMode(LedPin, OUTPUT); // LED
}
// Loop to repeated the programs.
void loop() {
int playBuzzer = digitalRead(Btn_Pin); // Membaca tombol dari PIN A0
// HIGH = 1
// LOW = 0
// Let's Play
if (playBuzzer == HIGH && DelayAfterPlay != 1) {
DelayAfterPlay = 1;
digitalWrite(LedPin, HIGH); // LED ON
// Mulai 0 (index awal) sampai 8 (index akhir)
for (int i = 0; i <= numCountTones; i++) {
if (i+1 > numCountTones) continue; // Menghindari Bug
Serial.print("[SPEAKER]: ");
Serial.println("Voice number " + String(Tones[i]));
tone(BuzzerPin, Tones[i]);
delay(500);
}
delay(2000); // Lanjutkan setelah 2 detik.
noTone(BuzzerPin); // Delay sebentar...
delay(750);
// Mulai dari 8 (index akhir) sampai 0 (index awal)
for (int i = numCountTones; i >= 0; i--) {
if (i+1 > numCountTones) continue; // Menghindari Bug
Serial.print("[SPEAKER]: ");
Serial.println("Voice number " + String(Tones[i]));
tone(BuzzerPin, Tones[i]);
delay(500);
}
delay(3000); // Berhenti setelah 3 detik
noTone(BuzzerPin);
Serial.print("[SPEAKER]: ");
Serial.println("Stopped!");
digitalWrite(LedPin, LOW); // LED OFF
delay(1200); // Delay sebentar sebelum memulai.
DelayAfterPlay = 0;
}
}