#include "pitches.h"
#define SPEAKER_PIN 8
#define CHANGE_SONG_BUTTON_PIN 13
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int numButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);
const int notes[] = { NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5 };
// Define songs as sequences of button presses
const char* songs[] = {
"13581234682357835", // Tauba Tauba (From "Bad Newz")
"3587325687325687", // Apna Bana Le
"1356124671235678", // Tum Hi Ho
"1357126835783567", // Raataan Lambiyan
"246824682357835", // O Maahi
"1234567834125678", // Shree Hanuman Chalisa
"135712683578", // Aasa Kooda
"123456783412", // Shery Shery Lade
};
const int numSongs = sizeof(songs) / sizeof(songs[0]);
int currentSongIndex = 0;
bool isPlaying = false;
int songIndex = 0;
unsigned long songStartTime = 0;
void setup() {
for (uint8_t i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(CHANGE_SONG_BUTTON_PIN, INPUT_PULLUP);
pinMode(SPEAKER_PIN, OUTPUT);
}
void playNoteForButton(int button) {
if (button >= 1 && button <= 8) {
tone(SPEAKER_PIN, notes[button - 1], 500); // Play note for 500ms
}
}
void playSong(const char* song) {
unsigned long currentTime = millis();
if (currentTime - songStartTime >= 500) { // Adjust note duration if needed
char note = song[songIndex];
if (note >= '1' && note <= '8') {
playNoteForButton(note - '0');
} else {
noTone(SPEAKER_PIN);
}
songIndex = (songIndex + 1) % strlen(song);
songStartTime = currentTime;
}
}
void loop() {
// Check if the song change button is pressed
if (digitalRead(CHANGE_SONG_BUTTON_PIN) == LOW) {
delay(50); // Debounce delay
if (digitalRead(CHANGE_SONG_BUTTON_PIN) == LOW) {
currentSongIndex = (currentSongIndex + 1) % numSongs;
songIndex = 0;
songStartTime = millis();
isPlaying = true;
delay(300); // Debounce delay
}
}
// Check if any piano button is pressed
for (uint8_t i = 0; i < numButtons; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
delay(50); // Debounce delay
if (digitalRead(buttonPins[i]) == LOW) {
playNoteForButton(i + 1);
isPlaying = false; // Stop playing the song if a button is pressed
delay(300); // Debounce delay
noTone(SPEAKER_PIN); // Stop any ongoing tone
}
}
}
// Play the current song if it's active
if (isPlaying) {
playSong(songs[currentSongIndex]);
} else {
noTone(SPEAKER_PIN);
}
}