#include "pitches.h" // Includes a file that defines musical note frequencies like NOTE_C4, NOTE_D4, etc.
#define REST 0 // Defines REST as 0 to represent silence between notes
const int buttonPin = 7;
int buzzer = 6;
int buttonState = 0;
int lastButtonState = 0;
bool buzzerState = false
// Tempo of the melody (beats per minute)
int tempo = 120;
// Pins connected to LEDs for different notes
int C = 11;
int D = 9;
int E = 10;
int FS = 8;
int G = 13;
int B = 12;
// Melody: each note is followed by a duration value (e.g. 8 = eighth note)
int melody[] = {
NOTE_B4, 8,
NOTE_C4, 8,
NOTE_D4, 8,
NOTE_FS5, 8,
NOTE_G5, 8,
NOTE_E5, 8,
NOTE_C4, 8,
NOTE_D4, 8,
NOTE_E5, 8,
NOTE_G5, 8,
NOTE_B5, 8,
NOTE_FS5, 8,
NOTE_D4, 8, //last 4th octave before the end
NOTE_E5, 8,
NOTE_FS5, 8,
NOTE_B5, 8,
NOTE_C5, 8,
NOTE_G5, 8,
NOTE_E5, 8,
NOTE_FS5, 8,
NOTE_G5, 8,
NOTE_C5, 8,
NOTE_D5, 8,
NOTE_B5, 8,
NOTE_FS5, 8,
NOTE_G5, 8,
NOTE_B5, 8,
NOTE_D5, 8,
NOTE_E6, 8, //first 6th octave
NOTE_C5, 8,
NOTE_B5, 8,
NOTE_C5, 8,
NOTE_D5, 8,
NOTE_FS6, 8,
NOTE_G6, 8,
NOTE_E6, 8,
NOTE_C5, 8,
NOTE_D5, 8,
NOTE_E6, 8,
NOTE_G6, 8,
NOTE_B6, 8,
NOTE_FS6, 8,
NOTE_D5, 8,
NOTE_E6, 8,
NOTE_FS6, 8,
NOTE_B6, 8,
NOTE_C6, 8,
NOTE_G6, 8,
NOTE_D6, 16,
NOTE_C6, 16,
NOTE_B6, 16,
NOTE_FS6, 16,
NOTE_E6, 16,
NOTE_G6, 16,
NOTE_C6, 16,
NOTE_B6, 16,
NOTE_G6, 16,
NOTE_E6, 16,
NOTE_D5, 16,
NOTE_FS6, 16,
NOTE_B6, 16,
NOTE_G6, 16,
NOTE_FS6, 16,
NOTE_D5, 16,
NOTE_C5, 16,
NOTE_E6, 16,
NOTE_G6, 16,
NOTE_FS6, 16,
NOTE_E6, 16,
NOTE_C5, 16,
NOTE_B5, 16,
NOTE_D5, 16,
NOTE_FS6, 16,
NOTE_E6, 16,
NOTE_D5, 16,
NOTE_B5, 16,
NOTE_G5, 16,
NOTE_C5, 16,
NOTE_E6, 16,
NOTE_D5, 16,
NOTE_C5, 16,
NOTE_G5, 16,
NOTE_FS5, 16,
NOTE_B5, 16,
NOTE_D5, 16,
NOTE_C5, 16,
NOTE_B5, 16,
NOTE_FS5, 16,
NOTE_E5, 16,
NOTE_G5, 16,
NOTE_C5, 16,
NOTE_B5, 16,
NOTE_G5, 16,
NOTE_E5, 16,
NOTE_D4, 16,
NOTE_FS5, 16, //end of song
};
// Calculates number of notes in the melody
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
// Flag to ensure the melody only plays once
bool played = false;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
// Set each LED pin as an OUTPUT
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(FS, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT); //connecting the led's to pins
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastbuttonState) {
if (buttonState == HIGH) {
buzzerState = ! buzzerState;
digitalWrite(buzzerPin, buzzerState ? HIGH: LOW);
} else {
digitalWrite(buzzerPin, LOW);
}
// Check if melody has already been played
if (!played) {
// Calculate the duration of a whole note in milliseconds
int wholenote = (60000 * 4) / tempo;
int noteDuration, divider;
// Loop through the melody array two items at a time (note and duration)
for (int i = 0; i < notes * 2; i += 2) {
int pitch = melody[i]; // Get the note pitch (e.g. NOTE_C4)
divider = melody[i + 1]; // Get the note duration divider (e.g. 4 = quarter note)
// Calculate the note duration in milliseconds
if (divider > 0) {
noteDuration = wholenote / divider;
} else {
noteDuration = (wholenote / abs(divider)) * 1.5; // Dotted notes (not used in your current melody)
}
turnOnLED(pitch); // Turn on the correct LED for this pitch
// Play the note for 90% of its duration to leave a small gap
if (pitch != REST) {
tone(buzzer, pitch, noteDuration * 0.9); // Start the buzzer at the specified frequency
}
delay(noteDuration); // Wait for the full note duration
noTone(buzzer); // Stop the buzzer
turnOffAllLEDs(); // Turn off all LEDs
}
played = true; // Set flag so the melody doesn't play again
}
}
// Turns on the LED that corresponds to the given pitch
void turnOnLED(int pitch) {
turnOffAllLEDs(); // Make sure all LEDs are off before lighting the right one
if (pitch == NOTE_C4) digitalWrite(C, HIGH); //puts each individual note into a checklist as its being played
else if (pitch == NOTE_C5) digitalWrite(C, HIGH); //the else if statement says when the note is getting played it either is assioated with one of the LEDS
else if (pitch == NOTE_C6) digitalWrite(C, HIGH);
else if (pitch == NOTE_D4) digitalWrite(D, HIGH);
else if (pitch == NOTE_D5) digitalWrite(D, HIGH);
else if (pitch == NOTE_D6) digitalWrite(D, HIGH);
else if (pitch == NOTE_E5) digitalWrite(E, HIGH);
else if (pitch == NOTE_E6) digitalWrite(E, HIGH);
else if (pitch == NOTE_FS5) digitalWrite(FS, HIGH);
else if (pitch == NOTE_FS6) digitalWrite(FS, HIGH);
else if (pitch == NOTE_G5) digitalWrite(G, HIGH);
else if (pitch == NOTE_G6) digitalWrite(G, HIGH);
else if (pitch == NOTE_B4) digitalWrite(B, HIGH);
else if (pitch == NOTE_B5) digitalWrite(B, HIGH);
else if (pitch == NOTE_B6) digitalWrite(B, HIGH);
}
// Turns off all LEDs to reset before turning one on
void turnOffAllLEDs() {
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(FS, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
}