-
Notifications
You must be signed in to change notification settings - Fork 42
Examples
Getting started with FlowMeter is easy. Take a look at this simple example:
void setup() {
// prepare serial communication
Serial.begin(9600);
// enable a call to a helper function on every rising edge
attachInterrupt(INT0, MeterISR, RISING);
}
void loop() {
// wait between output updates
delay(period);
// process the (possibly) counted ticks
Meter.tick(period);
// output some measurement result
Serial.print("Currently ");
Serial.print(Meter.getCurrentFlowrate());
Serial.print(" l/min, ");
Serial.print(Meter.getTotalVolume());
Serial.println(" l total.");
//
// any other code can go here
//
}In the above example, a flow sensor is assumed to be connected to the INT0 pin. The corresponding object Meter is updated every period (in milliseconds, e.g. 1000ms).
So every measurement period, the current flow rate and the total volume are printed out.
See the complete source code of the Simple example (included with the library) for more.
In this only slightly more complex example, two flow sensors are assumed to be connected to INT0 and INT1, respectively. The corresponding objects are called Meter1 and Meter2 here.
void setup() {
// prepare serial communication
Serial.begin(9600);
// enable a call to the 'interrupt service handler' (ISR) on every rising edge at the interrupt pin
// do this setup step for every ISR you have defined, depending on how many interrupts you use
attachInterrupt(INT0, Meter1ISR, RISING);
attachInterrupt(INT1, Meter2ISR, RISING);
// sometimes initializing the gear generates some pulses that we should ignore
Meter1.reset();
Meter2.reset();
}
void loop() {
// wait between output updates
delay(period);
// process the (possibly) counted ticks
Meter1.tick(period);
Meter2.tick(period);
// output some measurement result
Serial.println("Meter 1 currently " + String(Meter1.getCurrentFlowrate()) + " l/min, " + String(Meter1.getTotalVolume())+ " l total.");
Serial.println("Meter 2 currently " + String(Meter2.getCurrentFlowrate()) + " l/min, " + String(Meter2.getTotalVolume())+ " l total.");
//
// any other code can go here
//
}It's easy to extend this approach to as many sensors as your board will provide interrupts for.
See the complete source code of the Multi example (included with the library) for more.
Thanks to @highergroundstudio for inspiring this example.
// let's provide our own sensor properties, including calibration points for error correction
FlowSensorProperties MySensor = {50.0f, 4.8f, {1.2, 1.1, 1.05, 1, 1, 1, 1, 0.95, 0.9, 0.8}};
// let's pretend there's a flow sensor connected to pin 3
FlowMeter Meter = FlowMeter(3, MySensor);In this example, the FlowMeter object Meter is initialized to pin 3 using a custom FlowSensorProperties structure MySensor.
This structure holds the following values (written in a compact inline notation):
MySensor.capacity = 50.0f;
MySensor.kFactor = 4.8f;
MySensor.mFactor = {1.2, 1.1, 1.05, 1, 1, 1, 1, 0.95, 0.9, 0.8};The mFactor array provides a non-linear, flow dependent correction to the otherwise flow independent kFactor.
The ten data points in the array correspond to flow rates within the centiles (0%, 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80% and 90%) of the capacity value, respectively.
The example then randomly simulates a sensor frequency and measurement period.
The flow rate varies between 0 and capacity with updates after up to five seconds.
After running the simulation for a while, the average flow rate settles around capacity/2 and the average error converges towards 0 (since the mFactor array has a mean value of 1).
You can of course play with the values in MySensor to see how this changes the behaviour.
See the description of flow sensor properties for the math and the calibration how-to for the motivation.
The complete source code of the Simulation example is included with this library.