#include "try.h"
#define buzzerPin 8
#define songPin 2
const uint8_t buttonPins[] = {12, 11, 10, 9, 7, 6, 5, 4}; // Button pins
const int buttonTones[] = {N1, N2, N3, N4, N5, N6, N7, N8}; // Define frequencies for button tones
const int notes[] = {
N5, N3, N3, N4, N5, N5, N5, // Satu satu aku sayang ibu
N5, N3, N3, N4, N5, N5, N5, // Dua dua juga sayang ayah
N5, N6, N6, N5, N4, N3, N4, // Tiga tiga sayang adik kakak
N5, N3, N3, N4, N5, N5, N5 // Satu dua tiga sayang semuanya
};
const int durations[] = {250, 250, 250, 250, 250, 250, 500, // Adjusted durations
250, 250, 250, 250, 250, 250, 500,
250, 250, 250, 250, 250, 250, 500,
250, 250, 250, 250, 250, 250, 500};
void setup() {
for (uint8_t pin : buttonPins) {
pinMode(pin, INPUT_PULLUP); // Set button pins as input with pull-up resistors
}
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
pinMode(songPin, INPUT_PULLUP); // Set song pin as input with pull-up
}
void loop() {
if (digitalRead(songPin) == LOW) { // Play song if songPin is pressed
playSong();
} else { // Check button states to play tones
for (uint8_t i = 0; i < sizeof(buttonPins); i++) {
if (digitalRead(buttonPins[i]) == LOW) {
tone(buzzerPin, buttonTones[i]); // Play tone
delay(200); // Play tone for a short duration
noTone(buzzerPin); // Stop tone
break; // Exit after first button pressed
}
}
}
delay(50); // Short delay to avoid overwhelming the loop
}
void playSong() {
for (int i = 0; i < sizeof(notes) / sizeof(notes[0]); i++) {
if (notes[i] != 0) {
tone(buzzerPin, notes[i]); // Play note
delay(durations[i]); // Wait for note duration
} else {
noTone(buzzerPin); // Rest
delay(50); // Short pause between notes
}
}
noTone(buzzerPin); // Stop sound after song finishes
}