Please Help! How to input delay and increase volume measurements with hall sensor detection

I'm writing code so that, every time a hall monitor senses, my code additively measures 5 liters of volume as it prints each detection. Then, this data is sent via Bluetooth to a phone. (This is for a tipping bucket device that distributes 5 liters of water every time it tips). However, my code keeps the volume measurement at 0.00 no matter how many times the hall effect is sensed, and I also can't figure out where to insert my delay statement so it does not double count the tips. Any suggestions?

int hall_pin = 2;

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

float hall_thresh = 1000000000.0;

// 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

     
        long previousMillis = 0;  // last time the volume level was checked, in ms
void setup() {

  // initialize serial communication at 9600 bits per second:

  Serial.begin(9600);

 
   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.setLocalName("VolumeMonitor");
          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
 
          /* 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
          BLE.advertise();

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




}

// 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 Volume = (hall_count - 1) * 5.0; // this is the liters per tip

      Serial.print(Volume);

      Serial.println(" Volume");
       

 
        // 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:
          Serial.println(central.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;
              Serial.print("Volume is now: ");
              Serial.println(Volume);;
            }
          }

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



          }

          if (hall_count >= hall_thresh) {

            break;

          }



        // print information about Time and RPM

        float end_time = micros();

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

        Serial.print("Time Passed: ");

        Serial.print(time_passed);

        Serial.println("s");



        delay(10000);
        // delay in between reads for stability
      }
    }
  }

in Main:

While (true)
void loop()

In your loop()

While(true)

nested while (true), why?

so for 10 seconds your code does NOTHING and you have an issue with not being able to count the bucket tips?

This may be a good reason to use an interrupt but not nesessary.

I'm not sure why I have those nested. Maybe that's something I should look to fix; I'm pretty new at coding Arduino.

As for the delay, I want to include this so, if the sensor is flipped twice by one bucket tip, it does not add 10 liters of volume instead of 5. The delay for just a few seconds helps ensure that no double counting occurs.

Can you explain the thought process that brings you to imagine this is an installation issue, please?

That's an easy one to code, it's just the "register on first event" switch debounce.

You should never use floating point variables just to count. That is what integer types are for (amongst other 1,000,000 uses). I stopped reading after seeing that. Also your non-standard formatting makes it painful to read.

You do understand that

and

do not mix well?

Imagine if you will the CPU is counting millis(), then you tell the computer to go into NOP mode for 10000 milli seconds, how many millis() will the code be counting when in NOP mode?

@murray9094 your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project. See About the Installation & Troubleshooting category.

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