Please help! Error: a function-definition is not allowed here before '{' token

Please help me! I keep getting the error: a function-definition is not allowed here before '{' token

#include <ArduinoBLE.h>
// digital pin 2 is the hall pin

int hall_pin = 2;

// set number of hall trips for RPM reading (higher improves accuracy)

float hall_thresh = 1000000000.0;
void setup() {

  // initialize serial communication at 9600 bits per second:


  // make the hall pin an input:

  pinMode(hall_pin, INPUT);


// the loop routine runs over and over again forever:

void loop() {

  // preallocate values for tach

  float hall_count = 1.0;

  float start = micros();

  bool on_state = false;

  // counting number of times the hall sensor is tripped

  // but without double counting during the same trip

  while (true) {

    if (digitalRead(hall_pin) == 0) {

      if (on_state == false) {

        on_state = true;

        hall_count += 1.0;


    } else {

      on_state = false;
      float Vol_val = (hall_count - 1) * 20.0; // this is the liters per tip


      Serial.println(" Volume"); {
        // BLE Battery Service
        BLEService Vol_valServ("180F");

        // BLE Battery Level Characteristic
        BLEUnsignedCharCharacteristic VolumeChar("2A19",  // standard 16-bit characteristic UUID
            BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes

        int oldVolume = 0; // last battery level reading from analog input
        long previousMillis = 0;  // last time the volume level was checked, in ms

        void setup() {

          while (!Serial);

          pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected

          // begin initialization
          if (!BLE.begin()) {
            Serial.println("starting BLE failed!");

            while (1);

          /* Set a local name for the BLE device
             This name will appear in advertising packets
             and can be used by remote devices to identify this BLE device
             The name can be changed but maybe be truncated based on space left in advertisement packet
          BLE.setAdvertisedService(Vol_valServ); // add the service UUID
          Vol_valServ.addCharacteristic(VolumeChar); // add the battery level characteristic
          BLE.addService(Vol_valServ); // Add the battery service
          VolumeChar.writeValue(oldVolume); // set initial value for this characteristic

          /* Start advertising BLE.  It will start continuously transmitting BLE
             advertising packets and will be visible to remote BLE central devices
             until it receives a new connection */

          // start advertising

          Serial.println("Bluetooth device active, waiting for connections...");

      void loop() {
        // wait for a BLE central
        BLEDevice central = BLE.central();

        // if a central is connected to the peripheral:
        if (central) {
          Serial.print("Connected to central: ");
          // print the central's BT address:
          // turn on the LED to indicate the connection:
          digitalWrite(LED_BUILTIN, HIGH);

          while (central.connected()) {
            long currentMillis = millis();

            if (currentMillis - previousMillis >= 200) {
              previousMillis = currentMillis;

          digitalWrite(LED_BUILTIN, LOW);
          Serial.print("Disconnected from central: ");

          void updateVolume()

          if (Volume != oldVolume) {
            Serial.print("Volume is now: ");
            oldVolume = Volume;


          if (hall_count >= hall_thresh) {




        // print information about Time and RPM

        float end_time = micros();

        float time_passed = ((end_time - start) / 1000000.0);

        Serial.print("Time Passed: ");



        // delay in between reads for stability
  }type or paste code here

You have put the setup function inside the loop function. C++ does not permit that.

So what can I do to have my code work then?

You have void setup() in void loop() and another void loop() in a void setup. You can only have one void setup() and one void loop(). I'm not sure why you want multiple setups, but if you are using the multiple loops because want to loop through some code, then look into for loops and while loops. These can be used within void setup() and void loop().

You can't embed a.function inside another function.

Edit: too late. I'll duck out

I'm combining two sets of code I created separately. So, I assume the repeated element is because each separate code has this line in it. Which one should I delete?

I'm not sure why you're putting one program inside another... just separate the void setup() stuff and the void loop() stuff and put them in a single void setup() and void loop() in one sketch.

@murray9094, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project.

The fast and dirty way to merge two (or more) working sketches into one (possibly working) sketch is to create this main sketch:

void setup()
  // setup3();
  // setup4();
void loop()
  // loop3();
  // loop4{};

Then, put the two (or more) sketches into 'tabs'. You can add, remove, and rename tabs with the tab menu: the little triangle at the right end of the tab bar (just above the sketch txt).

Rename the setup() and loop() in each tab to setup1()/loop1() in the first tab, setup2()/loop2() in the second, etc.

If you are lucky there will be no problems and both sketches will take turns so fast they will appear to be running 'at the same time'. If you are unlucky, you will have to resolve conflicts and rewrite each sketch that hogs the CPU.

Problems to look for:

Name Conflicts: Two or more sketches defining global variables or functions of the same name. You will have to change the names until there are no conflicts.

Pin conflicts: Two or more sketches using the same pin number for different purposes. You will have to change pin assignments to avoid conflicts.

Hardware Usage Conflicts: Two or more sketches that use the same hardware feature in different ways. For example, using the Serial port at different data rates. If you can get them all to agree on a data rate you can replace the several Serial.begin(rate) calls with one at the top of setup() in the 'main' sketch.

Library Conflicts: Some libraries don't work together because they use the the same hardware for different purposes. For example the Servo library uses Timer1 and so does the IRremote library. If you are lucky you will get an error message when they both try to use the same hardware interrupt. If you are not lucky, things just won't work.