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

Hello there, i keep getting the same error again and again. I have read other forums but none of them seem to resolve my problem. I have an arduino uno .
here is my code


#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
int BuzzerPin = 9; // Buzzer pin.

int Control_RX = 6; // RX and TX pin for the bluetooth.
int Control_TX = 7;
#define echoPin 13  // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 10 //attach pin D3 Arduino to pin Trig of HC-SR04

// 13 12 11 10 9 8 7 6 5 4 3 2


int LDR_1 = A0; // Analog pins for LDRs.
int LaserPin_1 = 8;

int LDR_1_Read ; // Define the value of LDRs as global variables.
long duration;
int distance;
int Counter = 0; // Set the default value of the counter as zero.
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

volatile boolean Alarm_is_Activated = false; // Choose whether the alarm is on or not.
volatile boolean Alarm_Initial = false;
volatile boolean laserOn = true;
volatile boolean Counter_Detect = false; // It is a variable to give delay time to Arduino.

SoftwareSerial Control(Control_RX, Control_TX); // Define the Rx and the Tx pins to communicate with Bluetooth Module.

String Name = "Control"; // Name your module and set the password for it.
int Password = 1111;
String Uart = "9600,0,0";
volatile boolean warning = false;

void setup() {
  lcd.begin(16, 2);


  Serial.begin(9600);
  Control.begin(9600); // Begin HC-06 Bluetooth module to communicate.

  // Change_BluetoothModule_Defaults(); // You can activate it if you want to change the defaults of the Bluetooth module.

  pinMode(LaserPin_1, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT);



}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite (trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  void get_Data_From_LDR() { // Get the data of LDR sensors.
    LDR_1_Read = analogRead(LDR_1);


  }


  get_Data_From_LDR(); // Get the data from LDR sensors.

  if (Control.available()) { // If HC-06 Bluetooth module is available, Commands() has proceeded.

    char c = Control.read();
    Serial.println(c); // Control the characters that are set by the app using the terminal.

    Commands(c);
  }

  Set_Alarm(); // Initial the alarm function.

  Set_Counter(); // Begin the people counter.


  void Commands(char i) { // Choose which events happen when the specific character is sent from the app to Arduino.

    switch (i) {
      case '1' :
        Control.print(Counter);
        break;
      case '2' :
        Alarm_is_Activated = true;
        break;
      case '3' :
        Alarm_is_Activated = false;
        break;
      case '4' :
        digitalWrite(LaserPin_1, HIGH);
        laserOn = true;
        break;
      case '5' :
        digitalWrite(LaserPin_1, LOW);
        laserOn = false;
        break;
      case '6':
        tone(BuzzerPin, 700);
        warning = true;
        break;
      case'7':
        noTone(BuzzerPin);
        warning = false;
        break;
      case'8':
        noTone(BuzzerPin);
        break;
      case'9'://ON goes first
        if (distance <= 50 && distance >= 0) {
          // Buzz
          tone(BuzzerPin, 800);
        } else {
          // Don't buzz
          noTone(BuzzerPin);
        }
      case 'r' :
        Counter = 0;
        break;
    }
  }



  void Set_Counter() { // Set a people counter.

    if (LDR_1_Read < 15) { // If a motion detected.

      Counter_Detect = true;

    }
    else {

      Counter_Detect = false;

    }
    if (Counter_Detect == true) {
      Counter = Counter + 1;
      delay(500); // Give some time to get the number of people who enter the room accurately.
    }
    if (Counter_Detect == false) {
      Counter = Counter;
    }
  }

  void Set_Alarm() { // Set an adjustable alarm system.

    if (Alarm_is_Activated == true) {

      if (LDR_1_Read < 15) { // If a motion is detected.

        Alarm_Initial = true;

      }
      else {

        Alarm_Initial = false;

      }
      if (Alarm_Initial == true) {
        tone(BuzzerPin, 500);

      }


      if (Alarm_is_Activated == false || Alarm_Initial == false ) {
        noTone(BuzzerPin);
      }

    }
  }


z  void Change_BluetoothModule_Defaults() { // Change the default values of the Bluetooth module whatever values you choose.

    Control.print("AT+NAME"); // Change the name.
    Control.println(Name);
    Serial.print("Name is changed: ");
    Serial.println(Name);
    delay(2000);
    Control.print("AT+PSWD"); // Change the password.
    Control.println(Password);
    Serial.print("Password is changed: ");
    Serial.println(Password);
    delay(2000);
    Control.print("AT+UART"); // Change the baud rate. If the Bluetooth module is a HC-05, the default value of the baud rate is 38400.
    Control.println(Uart);
    Serial.print("Baud rate is set: ");
    Serial.println(Uart);
    delay(2000);
    Serial.println("Task is completed."); // You can see whether the task is completed correctly or not using the terminal.
  }
  
  Control.print("Alarm is ");
  Control.print(Alarm_is_Activated);
  Control.print(" and warning is ");
  Control.print(warning);
  Control.print(". Laser1 is ");
  Control.print(laserOn);
  Control.print(" and ");
  Control.print(Counter);
  Control.print(" people are here.");
  lcd.print(Counter);
  lcd.print(" people");
  lcd.setCursor(0, 1);
  lcd.print("Alarm is ");
  lcd.print(Alarm_is_Activated);
}

and here are my errors











C:\Users\hp\Downloads\Documents\Arduino\lakshya_alarm\lakshya_alarm.ino: In function 'void loop()':
lakshya_alarm:68:28: error: a function-definition is not allowed here before '{' token
   void get_Data_From_LDR() { // Get the data of LDR sensors.
                            ^
lakshya_alarm:75:3: error: 'get_Data_From_LDR' was not declared in this scope
   get_Data_From_LDR(); // Get the data from LDR sensors.
   ^~~~~~~~~~~~~~~~~
lakshya_alarm:82:5: error: 'Commands' was not declared in this scope
     Commands(c);
     ^~~~~~~~
lakshya_alarm:85:3: error: 'Set_Alarm' was not declared in this scope
   Set_Alarm(); // Initial the alarm function.
   ^~~~~~~~~
lakshya_alarm:87:3: error: 'Set_Counter' was not declared in this scope
   Set_Counter(); // Begin the people counter.
   ^~~~~~~~~~~
C:\Users\hp\Downloads\Documents\Arduino\lakshya_alarm\lakshya_alarm.ino:87:3: note: suggested alternative: 'Counter'
   Set_Counter(); // Begin the people counter.
   ^~~~~~~~~~~
   Counter
lakshya_alarm:90:25: error: a function-definition is not allowed here before '{' token
   void Commands(char i) { // Choose which events happen when the specific character is sent from the app to Arduino.
                         ^
lakshya_alarm:137:22: error: a function-definition is not allowed here before '{' token
   void Set_Counter() { // Set a people counter.
                      ^
lakshya_alarm:158:20: error: a function-definition is not allowed here before '{' token
   void Set_Alarm() { // Set an adjustable alarm system.
                    ^
lakshya_alarm:186:42: error: a function-definition is not allowed here before '{' token
   void Change_BluetoothModule_Defaults() { // Change the default values of the Bluetooth module whatever values you choose.
                                          ^
exit status 1
a function-definition is not allowed here before '{' token

it would be nice if you solve my problem at the earliest..
thanks in advance :grinning:

You cannot define a function within another function. This is defined within loop. Put any function definition outside of any other function.
Whether this function makes really sense is another question ... :wink:

1 Like

This "z" is lost in time and space...

If you use the IDE autoformat tool (ctrl-t or Tools, Auto format) you will soon see where it goes wrong. The loop() function is not closed with a } in the right place so the function definitions are in loop(), and the stray z.

   duration = pulseIn(echoPin, HIGH);
   distance = duration * 0.034 / 2;
   void get_Data_From_LDR()   // Get the data of LDR sensors.
   {
      LDR_1_Read = analogRead(LDR_1);

Thanks a lot, guys. my problem is finally SOLVED. I sat pondering over it for an hour, then IT HIT ME. I realized that the function definitions HAVE to be outside the loop(). I am new though. So, thanks a lot, and you've been lots of help. small question though..... i get this error after uploading. does it mean anything, or should i just ignore it?????

C:\Users\hp\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.4\cores\arduino\new.cpp: In function 'void operator delete [](void*, const std::nothrow_t&)':
C:\Users\hp\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.4\cores\arduino\new.cpp:106:57: warning: unused parameter 'tag' [-Wunused-parameter]
 void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept {
                                                         ^~~

Hi
please, using tags </>, post the current code fixed from the previous errors.

Sure, this is the code that worked for me.

[code]
/*
                              ////////////////////////////
                             //     People Counter     //
                            //                        //
                           //  Made by Lakshya Kumar //
                          ////////////////////////////
*/
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
int BuzzerPin = 9; // Buzzer pin.

int Control_RX = 6; // RX and TX pin for the bluetooth.
int Control_TX = 7;
#define echoPin 13  // attach pin D2 Arduino to pin Echo of HC-SR 4
#define trigPin 10 //attach pin D3 Arduino to pin Trig of HC-SR04

// 13 12 11 10 9 8 7 6 5 4 3 2


int LDR_1 = A0; // Analog pins for LDRs.
int LaserPin_1 = 8;

int LDR_1_Read ; // Define the value of LDRs as global variables.
long duration;
int distance;
int Counter = 0; // Set the default value of the counter as zero.
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

volatile boolean Alarm_is_Activated = false; // Choose whether the alarm is on or not.
volatile boolean Alarm_Initial = false;
volatile boolean laserOn = true;
volatile boolean Counter_Detect = false; // It is a variable to give delay time to Arduino.

SoftwareSerial Control(Control_RX, Control_TX); // Define the Rx and the Tx pins to communicate with Bluetooth Module.

String Name = "Control"; // Name your module and set the password for it.
int Password = 1111;
String Uart = "9600,0,0";
volatile boolean warning = false;

void setup() {
  lcd.begin(16, 2);


  Serial.begin(9600);
  Control.begin(9600); // Begin HC-06 Bluetooth module to communicate.

  // Change_BluetoothModule_Defaults(); // You can activate it if you want to change the defaults of the Bluetooth module.

  pinMode(LaserPin_1, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT);



}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite (trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  


  get_Data_From_LDR();  // Get the data from LDR sensors.

  if (Control.available()) { // If HC-06 Bluetooth module is available, Commands() has proceeded.

    char c = Control.read();
    Serial.println(c); // Control the characters that are set by the app using the terminal.

    Commands(c);
  }

  Set_Alarm(); // Initial the alarm function.

  Set_Counter(); // Begin the people counter.


  

  Control.print("Alarm is ");
  Control.print(Alarm_is_Activated);
  Control.print(" and warning is ");
  Control.print(warning);
  Control.print(". Laser1 is ");
  Control.print(laserOn);
  Control.print(" and ");
  Control.print(Counter);
  Control.print(" people are here.");
  lcd.print(Counter);
  lcd.print(" people");
  lcd.setCursor(0, 1);
  lcd.print("Alarm is ");
  lcd.print(Alarm_is_Activated);
}
void get_Data_From_LDR() { // Get the data of LDR sensors.
    LDR_1_Read = analogRead(LDR_1);


  }
void Commands(char i) { // Choose which events happen when the specific character is sent from the app to Arduino.

    switch (i) {
      case '1' :
        Control.print(Counter);
        break;
      case '2' :
        Alarm_is_Activated = true;
        break;
      case '3' :
        Alarm_is_Activated = false;
        break;
      case '4' :
        digitalWrite(LaserPin_1, HIGH);
        laserOn = true;
        break;
      case '5' :
        digitalWrite(LaserPin_1, LOW);
        laserOn = false;
        break;
      case '6':
        tone(BuzzerPin, 700);
        warning = true;
        break;
      case'7':
        noTone(BuzzerPin);
        warning = false;
        break;
      case'8':
        noTone(BuzzerPin);
        break;
      case'9'://ON goes first
        if (distance <= 50 && distance >= 0) {
          // Buzz
          tone(BuzzerPin, 800);
        } else {
          // Don't buzz
          noTone(BuzzerPin);
        }
      case 'r' :
        Counter = 0;
        break;
    }
  }



  void Set_Counter() { // Set a people counter.

    if (LDR_1_Read < 15) { // If a motion detected.

      Counter_Detect = true;

    }
    else {

      Counter_Detect = false;

    }
    if (Counter_Detect == true) {
      Counter = Counter + 1;
      delay(500); // Give some time to get the number of people who enter the room accurately.
    }
    
    if (Counter_Detect == false) {
      Counter = Counter;
    }
  }

  void Set_Alarm() { // Set an adjustable alarm system.

    if (Alarm_is_Activated == true) {

      if (LDR_1_Read < 15) { // If a motion is detected.

        Alarm_Initial = true;

      }
      else {

        Alarm_Initial = false;

      }
      if (Alarm_Initial == true) {
        tone(BuzzerPin, 500);

      }


      if (Alarm_is_Activated == false || Alarm_Initial == false ) {
        noTone(BuzzerPin);
      }

    }
  }


  void Change_BluetoothModule_Defaults() { // Change the default values of the Bluetooth module whatever values you choose.

    Control.print("AT+NAME"); // Change the name.
    Control.println(Name);
    Serial.print("Name is changed: ");
    Serial.println(Name);
    delay(2000);
    Control.print("AT+PSWD"); // Change the password.
    Control.println(Password);
    Serial.print("Password is changed: ");
    Serial.println(Password);
    delay(2000);
    Control.print("AT+UART"); // Change the baud rate. If the Bluetooth module is a HC-05, the default value of the baud rate is 38400.
    Control.println(Uart);
    Serial.print("Baud rate is set: ");
    Serial.println(Uart);
    delay(2000);
    Serial.println("Task is completed."); // You can see whether the task is completed correctly or not using the terminal.
  }
[/code]

Sure, here is my code (the working one)

[code]
/*
                              ////////////////////////////
                             //     People Counter     //
                            //                        //
                           //  Made by               //
                          ////////////////////////////
*/
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
int BuzzerPin = 9; // Buzzer pin.

int Control_RX = 6; // RX and TX pin for the bluetooth.
int Control_TX = 7;
#define echoPin 13  // attach pin D2 Arduino to pin Echo of HC-SR 4
#define trigPin 10 //attach pin D3 Arduino to pin Trig of HC-SR04

// 13 12 11 10 9 8 7 6 5 4 3 2


int LDR_1 = A0; // Analog pins for LDRs.
int LaserPin_1 = 8;

int LDR_1_Read ; // Define the value of LDRs as global variables.
long duration;
int distance;
int Counter = 0; // Set the default value of the counter as zero.
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

volatile boolean Alarm_is_Activated = false; // Choose whether the alarm is on or not.
volatile boolean Alarm_Initial = false;
volatile boolean laserOn = true;
volatile boolean Counter_Detect = false; // It is a variable to give delay time to Arduino.

SoftwareSerial Control(Control_RX, Control_TX); // Define the Rx and the Tx pins to communicate with Bluetooth Module.

String Name = "Control"; // Name your module and set the password for it.
int Password = 1111;
String Uart = "9600,0,0";
volatile boolean warning = false;

void setup() {
  lcd.begin(16, 2);


  Serial.begin(9600);
  Control.begin(9600); // Begin HC-06 Bluetooth module to communicate.

  // Change_BluetoothModule_Defaults(); // You can activate it if you want to change the defaults of the Bluetooth module.

  pinMode(LaserPin_1, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT);



}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite (trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  


  get_Data_From_LDR();  // Get the data from LDR sensors.

  if (Control.available()) { // If HC-06 Bluetooth module is available, Commands() has proceeded.

    char c = Control.read();
    Serial.println(c); // Control the characters that are set by the app using the terminal.

    Commands(c);
  }

  Set_Alarm(); // Initial the alarm function.

  Set_Counter(); // Begin the people counter.


  

  Control.print("Alarm is ");
  Control.print(Alarm_is_Activated);
  Control.print(" and warning is ");
  Control.print(warning);
  Control.print(". Laser1 is ");
  Control.print(laserOn);
  Control.print(" and ");
  Control.print(Counter);
  Control.print(" people are here.");
  lcd.print(Counter);
  lcd.print(" people");
  lcd.setCursor(0, 1);
  lcd.print("Alarm is ");
  lcd.print(Alarm_is_Activated);
}
void get_Data_From_LDR() { // Get the data of LDR sensors.
    LDR_1_Read = analogRead(LDR_1);


  }
void Commands(char i) { // Choose which events happen when the specific character is sent from the app to Arduino.

    switch (i) {
      case '1' :
        Control.print(Counter);
        break;
      case '2' :
        Alarm_is_Activated = true;
        break;
      case '3' :
        Alarm_is_Activated = false;
        break;
      case '4' :
        digitalWrite(LaserPin_1, HIGH);
        laserOn = true;
        break;
      case '5' :
        digitalWrite(LaserPin_1, LOW);
        laserOn = false;
        break;
      case '6':
        tone(BuzzerPin, 700);
        warning = true;
        break;
      case'7':
        noTone(BuzzerPin);
        warning = false;
        break;
      case'8':
        noTone(BuzzerPin);
        break;
      case'9'://ON goes first
        if (distance <= 50 && distance >= 0) {
          // Buzz
          tone(BuzzerPin, 800);
        } else {
          // Don't buzz
          noTone(BuzzerPin);
        }
      case 'r' :
        Counter = 0;
        break;
    }
  }



  void Set_Counter() { // Set a people counter.

    if (LDR_1_Read < 15) { // If a motion detected.

      Counter_Detect = true;

    }
    else {

      Counter_Detect = false;

    }
    if (Counter_Detect == true) {
      Counter = Counter + 1;
      delay(500); // Give some time to get the number of people who enter the room accurately.
    }
    
    if (Counter_Detect == false) {
      Counter = Counter;
    }
  }

  void Set_Alarm() { // Set an adjustable alarm system.

    if (Alarm_is_Activated == true) {

      if (LDR_1_Read < 15) { // If a motion is detected.

        Alarm_Initial = true;

      }
      else {

        Alarm_Initial = false;

      }
      if (Alarm_Initial == true) {
        tone(BuzzerPin, 500);

      }


      if (Alarm_is_Activated == false || Alarm_Initial == false ) {
        noTone(BuzzerPin);
      }

    }
  }


  void Change_BluetoothModule_Defaults() { // Change the default values of the Bluetooth module whatever values you choose.

    Control.print("AT+NAME"); // Change the name.
    Control.println(Name);
    Serial.print("Name is changed: ");
    Serial.println(Name);
    delay(2000);
    Control.print("AT+PSWD"); // Change the password.
    Control.println(Password);
    Serial.print("Password is changed: ");
    Serial.println(Password);
    delay(2000);
    Control.print("AT+UART"); // Change the baud rate. If the Bluetooth module is a HC-05, the default value of the baud rate is 38400.
    Control.println(Uart);
    Serial.print("Baud rate is set: ");
    Serial.println(Uart);
    delay(2000);
    Serial.println("Task is completed."); // You can see whether the task is completed correctly or not using the terminal.
  }
[/code]

It's just a warning and it's not in your code - you can just ignore it.

Thanks for letting me know!!:yum:

Hi
I compiled and uploaded to UNO.
No problem.
My IDE version is 1.8.9.

That's weird! maybe it's with my board

Hi @wildbill
for my learning, what is the reason for this message?

Tks

Normally, it simply means what it says - you have defined a parameter in a function which is not used by the function, so why pass it at all.

Here, I think that there's more to it - the reference says that std::nothrow_t is used to distinguish between constructors in memory allocation functions. From the name, I'll guess that it determines which one doesn't throw execption on failure and fails silently.

1 Like

Tks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.