Measure and calibrate water level sensors.
See Example Public Methods Release NotesMeasure and calibrate capacitance sensors to detect change in water levels from the outside of a plastic container. The example below uses a piece of aluminum foil, but you can attach the wire to anything conductive and calibrate the sensor accordingly.
This library measures relative capacitance so it is prone to interference. You can temporarily adjust the entire scale and then reset it when the capacitance disruption has stopped by using setModifier() and resetModifier(). Look at the Capacitance Library if you would like to know more about how capacitance is calculated.
#include <RBD_Capacitance.h> #include <RBD_Threshold.h> #include <RBD_WaterSensor.h> RBD::WaterSensor water_sensor(2, 3, 3); // send, receive pin, levels void setup() { Serial.begin(115200); water_sensor.setLevel(1, 120); water_sensor.setLevel(2, 154); water_sensor.setLevel(3, 187); water_sensor.setMaxLevel(220); } void loop() { water_sensor.update(); if(water_sensor.onRawValueChange()) { Serial.print("Active Level: "); Serial.print(water_sensor.getActiveLevel()); Serial.print(" --- "); Serial.print("Raw Value: "); Serial.println(water_sensor.getRawValue()); } }
Follow these steps to calibrate each water level sensor before use:
You must continuously call update() inside of loop() in order to use theReal-Time methods tagged below.
RBD::WaterSensor
Pass in integers for the send and receive pins to create a new instance of this class, along with an integer for the total number of levels the water sensor will detect. Example: if you want to detect low, medium, and high levels then level_count should equal 3.
RBD::WaterSensor water_sensor(2, 3, 3); // send, receive pin, levels void setup() { ... }
Pass in an integer to change the number of readings taken to calculate the moving average getRawValue(). This can be called inside of setup() or also safely at runtime inside of loop().
void setup() { water_sensor.setSampleSize(250); }
Provide an integer for the level index and a raw value from getRawValue(). The level index starts at one (it is not zero-based). You must also call setMaxLevel() at the end to set an upper bounds of the last level.
void setup() { water_sensor.setLevel(1, 120); water_sensor.setLevel(2, 154); water_sensor.setLevel(3, 187); }
Provide an integer from getRawValue() to set the upper-bounds threshold of the last level.
void setup() { water_sensor.setMaxLevel(220); }
Keep processing the readings and move this library along in real-time.
void loop() { water_sensor.update(); }
Returns the capacitance sensor reading after being adjusted with the value given to setModifier().
This will return getRawValue() if you have not used setModifier() yet, or if you have called resetModifier().
void loop() { water_sensor.update(); if(water_sensor.onValueChange()) { Serial.println(water_sensor.getValue()); } }
Returns the raw capacitance sensor reading and ignores any value that has been passed to setModifier().
void loop() { water_sensor.update(); if(water_sensor.onRawValueChange()) { Serial.println(water_sensor.getRawValue()); } }
Returns the current water level.
void loop() { water_sensor.update(); if(water_sensor.onActiveLevelChange()) { Serial.println(water_sensor.getActiveLevel()); } }
This method will return true once the sensor getValue() changes. It will then return false until the reading changes to a different value again.
void loop() { water_sensor.update(); if(water_sensor.onValueChange()) { // code only runs once per event Serial.println(water_sensor.getValue()); } }
This method will return true once the sensor getRawValue() changes. It will then return false until the reading changes to a different value again.
void loop() { water_sensor.update(); if(water_sensor.onRawValueChange()) { // code only runs once per event Serial.println(water_sensor.getRawValue()); } }
This method will return true once the sensor getActiveLevel() changes. It will then return false until the level changes to a different value again.
void loop() { water_sensor.update(); if(water_sensor.onActiveLevelChange()) { // code only runs once per event Serial.println(water_sensor.getActiveLevel()); } }
Provide a positive or negative integer to temporarily adjust the water sensor threshold scale. The default value is 0.
For example; if you have calibrated the scale and turn on a motor near the sensor: all readings will need to be adjusted for the increased capacitance from the motor.
void loop() { if(...) { // adjust when motor is on water_sensor.setModifier(-200); } else { water_sensor.resetModifier(); } water_sensor.update(); // still works as expected with interference if(water_sensor.onActiveLevelChange()) { ... } }
Changes the setModifier() back to 0 and resets calibration of the water sensor threshold scale.
For example; if you have calibrated the scale and turn on a motor near the sensor: all readings will need to be adjusted for the increased capacitance from the motor.
void loop() { if(...) { water_sensor.setModifier(-200); } else { // reset when motor is off water_sensor.resetModifier(); } water_sensor.update(); // still works as expected with interference if(water_sensor.onActiveLevelChange()) { ... } }