I need assistance in troubleshooting my music player

This is my code.

// notes in the melody:

int melody[] = {


  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4

};
int melody2[] = {

  NOTE_C4, NOTE_G4, NOTE_G3, NOTE_A4, NOTE_G3, 0, NOTE_B4, NOTE_C4
};
int melody3[] = {

  NOTE_C4, NOTE_G4, NOTE_G3, NOTE_A4, NOTE_G3, 0, NOTE_B4, NOTE_C4
};
int melody4[] = {

  NOTE_C4, NOTE_G4, NOTE_G3, NOTE_A4, NOTE_G3, 0, NOTE_B4, NOTE_C4
};
int melody5[] = {

  NOTE_C4, NOTE_G4, NOTE_G3, NOTE_A4, NOTE_G3, 0, NOTE_B4, NOTE_C4
};
int melody6[] = {

  NOTE_C4, NOTE_G4, NOTE_G3, NOTE_A4, NOTE_G3, 0, NOTE_B4, NOTE_C4
};
int melody7[] = {

  NOTE_C4, NOTE_G4, NOTE_G3, NOTE_A4, NOTE_G3, 0, NOTE_B4, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {


  4, 8, 8, 4, 4, 4, 4, 4

};
const int BUTTON_PIN7 = 2;
const int BUTTON_PIN6 = 3;
const int BUTTON_PIN5 = 4;
const int BUTTON_PIN4 = 10;
const int BUTTON_PIN3 = 13;
const int BUTTON_PIN2 = 12;
const int BUTTON_PIN = 11; // Arduino pin connected to button's pin
const int BUZZER_PIN = 8; // Arduino pin connected to Buzzer's pin
 
void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(8, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(8, OUTPUT);       // set arduino pin to output mode
};
 

 





void loop() {


int buttonState = digitalRead(BUTTON_PIN); // read new state
 
  if (buttonState == LOW) {
   // iterate over the notes of the melody:


  for (int thisNote = 0; thisNote < 8; thisNote++) {


    // to calculate the note duration, take one second divided by the note type.


    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.


    int noteDuration = 1000 / noteDurations[thisNote];


    tone(8, melody[thisNote], noteDuration);


    // to distinguish the notes, set a minimum time between them.


    // the note's duration + 30% seems to work well:


    int pauseBetweenNotes = noteDuration * 1.30;


    delay(pauseBetweenNotes);


    // stop the tone playing:


    noTone(8);

 
}
   
  }
  else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(8, LOW);  // turn off
  }
  }


  int buttonState = digitalRead(BUTTON_PIN2); // read new state
 
  {if (buttonState == LOW) {
   // iterate over the notes of the melody:


  for (int thisNote = 0; thisNote < 8; thisNote++) {


    // to calculate the note duration, take one second divided by the note type.


    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.


    int noteDuration = 1000 / noteDurations[thisNote];


    tone(8, melody2[thisNote], noteDuration);


    // to distinguish the notes, set a minimum time between them.


    // the note's duration + 30% seems to work well:


    int pauseBetweenNotes = noteDuration * 1.30;


    delay(pauseBetweenNotes);


    // stop the tone playing:


    noTone(8);

 
}
   
  }
  {else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(8, LOW);  // turn off
  }
  } 
  int buttonState = digitalRead(BUTTON_PIN3); // read new state
 
  if (buttonState == LOW) {
   // iterate over the notes of the melody:


  for (int thisNote = 0; thisNote < 8; thisNote++) {


    // to calculate the note duration, take one second divided by the note type.


    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.


    int noteDuration = 1000 / noteDurations[thisNote];


    tone(8, melody3[thisNote], noteDuration);


    // to distinguish the notes, set a minimum time between them.


    // the note's duration + 30% seems to work well:


    int pauseBetweenNotes = noteDuration * 1.30;


    delay(pauseBetweenNotes);


    // stop the tone playing:


    noTone(8);

 
}
   
  }
  else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(8, LOW);  // turn off
  }
  }
  int buttonState = digitalRead(BUTTON_PIN4); // read new state
 
  {if (buttonState == LOW) {
   // iterate over the notes of the melody:


  for (int thisNote = 0; thisNote < 8; thisNote++) {


    // to calculate the note duration, take one second divided by the note type.


    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.


    int noteDuration = 1000 / noteDurations[thisNote];


    tone(8, melody4[thisNote], noteDuration);


    // to distinguish the notes, set a minimum time between them.


    // the note's duration + 30% seems to work well:


    int pauseBetweenNotes = noteDuration * 1.30;


    delay(pauseBetweenNotes);


    // stop the tone playing:


    noTone(8);

 
}
   
  }
  {else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(8, LOW);  // turn off
  }
  }
  int buttonState = digitalRead(BUTTON_PIN5); // read new state
 
  if (buttonState == LOW) {
   // iterate over the notes of the melody:


  for (int thisNote = 0; thisNote < 8; thisNote++) {


    // to calculate the note duration, take one second divided by the note type.


    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.


    int noteDuration = 1000 / noteDurations[thisNote];


    tone(8, melody5[thisNote], noteDuration);


    // to distinguish the notes, set a minimum time between them.


    // the note's duration + 30% seems to work well:


    int pauseBetweenNotes = noteDuration * 1.30;


    delay(pauseBetweenNotes);


    // stop the tone playing:


    noTone(8);

 
}
   
  }
  else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(8, LOW);  // turn off
  }
  }
  int buttonState = digitalRead(BUTTON_PIN6); // read new state
 
  if (buttonState == LOW) {
   // iterate over the notes of the melody:


  for (int thisNote = 0; thisNote < 8; thisNote++) {


    // to calculate the note duration, take one second divided by the note type.


    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.


    int noteDuration = 1000 / noteDurations[thisNote];


    tone(8, melody6[thisNote], noteDuration);


    // to distinguish the notes, set a minimum time between them.


    // the note's duration + 30% seems to work well:


    int pauseBetweenNotes = noteDuration * 1.30;


    delay(pauseBetweenNotes);


    // stop the tone playing:


    noTone(8);

 
}
   
  }
  else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(8, LOW);  
  }
  }
  int buttonState = digitalRead(BUTTON_PIN7); 
 
  if (buttonState == LOW) {


  for (int thisNote = 0; thisNote < 8; thisNote++) {


    int noteDuration = 1000 / noteDurations[thisNote];


    tone(8, melody7[thisNote], noteDuration);


    int pauseBetweenNotes = noteDuration * 1.30;


    delay(pauseBetweenNotes);



    noTone(8);

 
}
   
  }
  else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(8, LOW); 
  }
  }

Melody

Plays a melody

circuit:

  • 8 ohm speaker on digital pin 8

created 21 Jan 2010

modified 30 Aug 2011

by Tom Igoe

This example code is in the public domain.

https://www.arduino.cc/en/Tutorial/Tone

*/

These, however are my errors, which I do not fully grasp how to fix.
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:341:3: error: expected unqualified-id before '{' token
{if (buttonState == LOW) {
^
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:435:7: error: redefinition of 'int buttonState'
int buttonState = digitalRead(BUTTON_PIN4); // read new state
^~~~~~~~~~~
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:339:7: note: 'int buttonState' previously declared here
int buttonState = digitalRead(BUTTON_PIN2); // read new state
^~~~~~~~~~~
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:437:3: error: expected unqualified-id before '{' token
{if (buttonState == LOW) {
^
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:531:7: error: redefinition of 'int buttonState'
int buttonState = digitalRead(BUTTON_PIN6); // read new state
^~~~~~~~~~~
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:339:7: note: 'int buttonState' previously declared here
int buttonState = digitalRead(BUTTON_PIN2); // read new state
^~~~~~~~~~~
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:533:3: error: expected unqualified-id before 'if'
if (buttonState == LOW) {
^~
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:573:3: error: expected unqualified-id before 'else'
else
^~~~
C:\Users\repea\Documents\Arduino\sketch_oct21a\sketch_oct21a.ino:578:3: error: expected declaration before '}' token
}
^

exit status 1

Compilation error: expected unqualified-id before '{' token

I do apologize for the unoptimized code, this is my first proper project. Thank you in advance.

I see you tried to make multiple buttons and more melodies, but you did not follow programming rules of "C"... you must learn how to program in C.

Here is the original code:

/*
  Melody
  Plays a melody
  circuit:
  - 8 ohm speaker on digital pin 8
  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe
  This example code is in the public domain.
  https://www.arduino.cc/en/Tutorial/Tone
*/

#include "pitches.h"

// notes in the melody:
int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);
    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}
void loop() {
  // no need to repeat the melody.
}

What do you want to do with this sketch?

Very helpful. I want advice on how to fix my errors, and I know I need to learn the language, but being told so does not exactly help in any way.

Hey... we all need to keep learning... as I started working on your program, I learned that "pitches.h" is contained in "PlayRtttl.h" which allows non-blocking melody playing... but that is for later in the learning path... so, we're even? : )

Your The code I posted in Post #3 "compiles" meaning: When I paste your the code I posted in Post #3 into my IDE, and I add "pitches.h" to my project (new tab >> pitches.h), no errors are reported, and when I upload, my buzzer makes a melody. All that is needed is hooking up your buzzer to pin 8 (see your code).

How would you like to proceed? I'm all ears.