a function-definition is not allowed here before '{' token

I get the error code: a function-definition is not allowed here before '{' token. how can I fix this?

#include <SoftwareSerial.h>

#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX

////////////////////////////////////////////////////////////////////////////////////
//all the commands needed in the datasheet(http://geekmatic.in.ua/pdf/Catalex_MP3_board.pdf)
static int8_t Send_buf[8] = {0} ;//The MP3 player undestands orders in a 8 int string
                                 //0X7E FF 06 command 00 00 00 EF;(if command =01 next song order) 
int SENSOR = 8 ; // define the Hall magnetic sensor
int val ; // define numeric variables

#define NEXT_SONG 0X01 
#define PREV_SONG 0X02 

#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)

#define VOLUME_UP_ONE 0X04
#define VOLUME_DOWN_ONE 0X05
#define CMD_SET_VOLUME 0X06//DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X22 //data is needed  0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)

#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
                #define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED
                
#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B

#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED

#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3

#define STOP_PLAY 0X16

#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)

#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close

#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output
////////////////////////////////////////////////////////////////////////////////////

 
void setup ()
{
  Serial.begin(9600);
  mySerial.begin(9600);//Start our Serial coms for THE MP3
delay(500);//Wait chip initialization is complete
   CMD_SEL_DEV, DEV_TF;//select the TF card  
delay(200);//wait for 200ms
  pinMode (SENSOR, INPUT) ;  // define the Hall magnetic sensor line as input
  
}
 
void loop ()


{
  val = digitalRead (SENSOR) ; // read sensor line
  if (val == LOW) // when the Hall sensor detects a magnetic field, Arduino LED lights up
  {
    {
CMD_PLAY_WITHVOLUME, 0x1E01;//play the first song with volume 30 class
}
  void sendCommand(int8_t command, int16_t dat)
{
 Send_buf[0] = 0x7e; //starting byte
 Send_buf[1] = 0xff; //version
 Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
 Send_buf[3] = command; //
 Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
 Send_buf[5] = (int8_t)(dat >> 8);//datah
 Send_buf[6] = (int8_t)(dat); //datal
 Send_buf[7] = 0xef; //ending byte
 for(uint8_t i=0; i<8; i++)//
 {
   mySerial.write(Send_buf[i]) ;//send bit to serial mp3
   Serial.print(Send_buf[i],HEX);//send bit to serial monitor in pc
 }
 Serial.println();
}
    Serial.println("Magnetic field detected");
  }
  else
  {
    Serial.println("No magnetic field detected");
  }
  delay(1000);
}

Match your braces { }

.

You have misplaced brackets {}. The best way to avoid that problem is to use proper C formatting style and use ctrl-T in the IDE to automatically indent it for you. Once you do that, you will probably see the error.

I'm sorry but I can't find the misplaced brackets.

I used ctrl+t but it gave me this

{
  val = digitalRead (SENSOR) ; // read sensor line
  if (val == LOW) // when the Hall sensor detects a magnetic field, Arduino LED lights up
  {
    {
      CMD_PLAY_WITHVOLUME, 0x1E01;//play the first song with volume 30 class
    }

    {
      void sendCommand(int8_t command, int16_t dat)
    }

    {
      Send_buf[0] = 0x7e; //starting byte
      Send_buf[1] = 0xff; //version
      Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
      Send_buf[3] = command; //
      Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
      Send_buf[5] = (int8_t)(dat >> 8);//datah
      Send_buf[6] = (int8_t)(dat); //datal
      Send_buf[7] = 0xef; //ending byte
      for (uint8_t i = 0; i < 8; i++) //

      {
        mySerial.write(Send_buf[i]) ;//send bit to serial mp3
        Serial.print(Send_buf[i], HEX); //send bit to serial monitor in pc
      }
      Serial.println();
    }
    Serial.println("Magnetic field detected");
  }

with this error:expected initializer before '}' token

You can't have this on the middle of another function:

void sendCommand(int8_t command, int16_t dat)
}

sterretje:
You can't have this on the middle of another function:

void sendCommand(int8_t command, int16_t dat)
}

What is the best place to put it otherwise?

ItsDavid2:
What is the best place to put it otherwise?

Sorry, but this is getting silly. There is no best place to put it, it totally depends on what you intend it to do. Just understand what it does and it will become obvious where to put it. It defines the block of code that a condition statement operates on.

ItsDavid2:
I used ctrl+t but it gave me this

{

val = digitalRead (SENSOR) ; // read sensor line
  if (val == LOW) // when the Hall sensor detects a magnetic field, Arduino LED lights up
  {
    {
      CMD_PLAY_WITHVOLUME, 0x1E01;//play the first song with volume 30 class
    }

{
      void sendCommand(int8_t command, int16_t dat)
    }

{
      Send_buf[0] = 0x7e; //starting byte
      Send_buf[1] = 0xff; //version
      Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
      Send_buf[3] = command; //
      Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
      Send_buf[5] = (int8_t)(dat >> 8);//datah
      Send_buf[6] = (int8_t)(dat); //datal
      Send_buf[7] = 0xef; //ending byte
      for (uint8_t i = 0; i < 8; i++) //

{
        mySerial.write(Send_buf[i]) ;//send bit to serial mp3
        Serial.print(Send_buf[i], HEX); //send bit to serial monitor in pc
      }
      Serial.println();
    }
    Serial.println("Magnetic field detected");
  }

YES! AND HOW MANY OPENING BRACES DO YOU HAVE? 6
HOW MANY CLOSING BRACES? 5

Those numbers should be the same.

You also are using braces around a few pieces of code for no apparent reason.

You cannot define a function inside another function. You must define it outside of all functions - you can still freely call it from within another function, just not define it.

I smell a coding troll.

.

After an auto-format, I get this for loop(); note that my placement of { is different from the default but that is a personal preference.

void loop ()
{
  val = digitalRead (SENSOR) ; // read sensor line
  if (val == LOW) // when the Hall sensor detects a magnetic field, Arduino LED lights up
  {
    {
      CMD_PLAY_WITHVOLUME, 0x1E01;//play the first song with volume 30 class
    }
    Serial.println("Magnetic field detected");
  }
  else
  {
    Serial.println("No magnetic field detected");
  }
  delay(1000);
}

If you take out the below

    void sendCommand(int8_t command, int16_t dat)
    {
      Send_buf[0] = 0x7e; //starting byte
      Send_buf[1] = 0xff; //version
      Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
      Send_buf[3] = command; //
      Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
      Send_buf[5] = (int8_t)(dat >> 8);//datah
      Send_buf[6] = (int8_t)(dat); //datal
      Send_buf[7] = 0xef; //ending byte
      for (uint8_t i = 0; i < 8; i++) //
      {
        mySerial.write(Send_buf[i]) ;//send bit to serial mp3
        Serial.print(Send_buf[i], HEX); //send bit to serial monitor in pc
      }
      Serial.println();
    }

and place it on its own

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

void sendCommand(int8_t command, int16_t dat)
{
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for (uint8_t i = 0; i < 8; i++) //
  {
    mySerial.write(Send_buf[i]) ;//send bit to serial mp3
    Serial.print(Send_buf[i], HEX); //send bit to serial monitor in pc
  }
  Serial.println();
}

Your compile error will disappear. As said before, you can not define a function inside another function. Now you just need to call that function somewhere.

Next, in the remainder of loop(), I have no idea what this does

      CMD_PLAY_WITHVOLUME, 0x1E01;//play the first song with volume 30 class

Although the compiler does not complain, I doubt very much that that line will do what you expect it to do. Did you want to pass these values to sendCommand? If so

void loop ()
{
  val = digitalRead (SENSOR) ; // read sensor line
  if (val == LOW) // when the Hall sensor detects a magnetic field, Arduino LED lights up
  {
    sendCommand(CMD_PLAY_WITHVOLUME, 0x1E01);//play the first song with volume 30 class
    Serial.println("Magnetic field detected");
  }
  else
  {
    Serial.println("No magnetic field detected");
  }
  delay(1000);
}

thx everyone for the help. I figured it out. This is my first Arduino project and I'm still learning the language.