play notes error

so I think I've been trying to do something simple I've been trying to use an ultrasonic sensor to detect distance and play a song from a piezo buzzer when something comes close enough but I keep getting the error:

sketch_nov14a:17: error: expected declaration before '}' token
expected declaration before '}' token

Code:

long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}

}

void setup()
{
if (0.006783 * readUltrasonicDistance(2, 4) < 2) {
#define N_F3 175
#define N_FS3 185
#define N_G3 196
#define N_GS3 208
#define N_A3 220
#define N_AS3 233
#define N_B3 247
#define N_C4 262
#define N_CS4 277
#define N_D4 294
#define N_DS4 311
#define N_E4 330
#define N_F4 349
#define N_FS4 370
#define N_G4 392
#define N_GS4 415
#define N_A4 440
#define N_AS4 466
#define N_B4 494
#define N_C5 523
#define N_CS5 554
#define N_D5 587
#define N_DS5 622
#define N_E5 659
#define N_F5 698
#define N_FS5 740
#define N_G5 784
#define N_GS5 831
#define N_A5 880
#define N_AS5 932
#define N_B5 988
#define N_C6 1047
#define N_CS6 1109

// notes in the melody:
int melody[] = {
N_AS4, 0, 0, N_AS4, N_AS4, N_AS4, N_AS4, N_AS4, 0, N_GS4, N_AS4, 0, 0, N_AS4, N_AS4, N_AS4, N_AS4, N_AS4, 0, N_GS4, N_AS4, 0, 0, N_AS4, N_AS4, N_AS4, N_AS4, N_AS4, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_AS4, N_F3, N_F3, 0, N_AS4, N_AS4, N_C5, N_D5, N_DS5, N_F5, 0, N_F5, N_F5, N_FS5, N_GS5, N_AS5, 0, N_AS5, N_AS5, N_AS5, N_GS5, N_FS5, N_GS5, 0, N_FS5, N_F5, N_F5, N_DS5, N_DS5, N_F5, N_FS5, N_F5, N_DS5, N_CS5, N_CS5, N_DS5, N_F5, N_DS5, N_CS5, N_C5, N_C5, N_D5, N_E5, N_G5, N_F5, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_AS4, N_F3, N_F3, 0, N_AS4, N_AS4, N_C5, N_D5, N_DS5, N_F5, 0, N_F5, N_F5, N_FS5, N_GS5, N_AS5, 0, N_CS6, N_C6, N_A5, 0, N_F5, N_FS5, 0, N_AS5, N_A5, N_F5, 0, N_F5, N_FS5, 0, N_AS5, N_A5, N_F5, 0, N_D5, N_DS5, 0, N_FS5, N_F5, N_CS5, 0, N_AS4, N_C5, N_C5, N_D5, N_E5, 0, N_G5, N_F5, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_AS4, N_F3, N_F3, 0, N_AS4, N_AS4, N_C5, N_D5, N_DS5, N_F5, 0, N_F5, N_F5, N_FS5, N_GS5, N_AS5, 0, N_AS5, N_AS5, N_AS5, N_GS5, N_FS5, N_GS5, 0, N_FS5, N_F5, N_F5, N_DS5, N_DS5, N_F5, N_FS5, N_F5, N_DS5, N_CS5, N_CS5, N_DS5, N_F5, N_DS5, N_CS5, N_C5, N_C5, N_D5, N_E5, N_G5, N_F5, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_AS4, N_F3, N_F3, 0, N_AS4, N_AS4, N_C5, N_D5, N_DS5, N_F5, 0, N_F5, N_F5, N_FS5, N_GS5, N_AS5, 0, N_CS6, N_C6, N_A5, 0, N_F5, N_FS5, 0, N_AS5, N_A5, N_F5, 0, N_F5, N_FS5, 0, N_AS5, N_A5, N_F5, 0, N_D5, N_DS5, 0, N_FS5, N_F5, N_CS5, 0, N_AS4, N_C5, N_C5, N_D5, N_E5, 0, N_G5, N_F5, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3, N_F3
};

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

void setup()
int melody_len = sizeof(melody)/sizeof(melody[0]);

for (int thisNote = 0; thisNote < melody_len; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.45;
delay(pauseBetweenNotes);
noTone(8);
}
}

void loop() {
delay(20000);
} }
delay(10); // Delay a little bit to improve simulation performance

TOPIC SPLIT
DO NOT HIJACK !

Could you take a few moments to READ THIS.
It will help you get the best out of the forum.

Bob.

Learn to interpret the results of an auto-format in the IDE (tools -> auto format)

Further
you can not have two setups (complete or partial)
you can not have function definitions inside functions
you can not have function calls outside functions

And why do you ahve #defines inside a function?

I'll be honest I've been trying to Frankenstein code together into something quick I'm not very good at C++ I've only really learned the basics of Javascript. I'd really appreciate it if someone would help reconfigure the code or help me to understand a little bit more about what I did wrong like taking sections out and showing me what's wrong but thanks in advance.

Note in advance
I've used tools -> auto format in the IDE on regular basis so the code snippets below are properly indented and one can see what goes wrong.

An Arduino sketch has 2 functions that need to be available

void setup()
{
}

void loop()
{
}

A function definition starts with a return type (void means that the function returns nothing), next the name of the function (e.g. setup), next a set of () that is used to pass parameters (in this case none) and the body between {}. For simplicity, fuction names need to be unique.

You can not define a function inside another function, so e.g. below is wrong

void setup()
{
  void loop()
  {
  }
}

A { must always have a matching } and vice versa. This also applies to for, while, if and else statements.

Your code starts with

long readUltrasonicDistance(int triggerPin, int echoPin)
{
  ...
  ...
}

}

You have two } at the end. One of them has no matching { somewhere before it so needs to be removed.

Although #defines can be placed anywhere in the code (to my knowledge), they actually should be near the beginning of your code. Further #define are preprocessor statements and can not be changed at run time as you try to do below.

  if (0.006783 * readUltrasonicDistance(2, 4) < 2) {
#define N_F3  175
...
...

With this information you can modify your code to below

#define N_F3  175
// other notes here
#define N_CS6 1109

long readUltrasonicDistance(int triggerPin, int echoPin)
{
  ...
  ...
}

void setup()
{
  ...
  ...
}

void loop()
{
  ...
  ...
}

Now you setup looks like

void setup()
{
  if (0.006783 * readUltrasonicDistance(2, 4) < 2) {

    // notes in the melody:
    int melody[] = {
      ...
      ...
    };

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

    void setup()
    int melody_len = sizeof(melody) / sizeof(melody[0]);

    for (int thisNote = 0; thisNote < melody_len; thisNote++) {
      ...
      ...
    }
  }

The last } belongs to the if. Further I have no idea what you intended to do with the line void setup() just before the int melody_len = ...; remove it, it does not belong there.

The next line is the start of the loop() definition.

  void loop() {
    delay(20000);
  }
}

After an tools -> auto format, that function definition should start at the beginning of the line. This indicates that the loop() definition is inside setup() function and as explained in the beginning, that can't be done.

At the end of loop(), you have two }. That last one belongs to

void setup()
{

If you move that last } to before loop(), that problem is sorted.

Lastly, after the loop() function, you have a call to delay(). Function calls should always be inside functions (with one or two exceptions).

Notes:
1)
You will need to read up on variable scope. There is basically global scope and local scope. Variables declared/defined inside a function are only known inside that function. Variables declared/defined inside if/while/for are only known inside the if/while/for. Variables declared outside any function are known everywhere in your sketch.

So your melody and noteDuration are only known inside the preceeding if. You might want to move those to after the #defines.

Code in setup() is only executed once; you might want to move it inside loop().

pinMode is usually a once-off thing and in that case is placed in setup(). There is no need for you to put it in readUltrasonicDistance().