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