Detect human presence, touch, and pickup events.
See Example Public Methods Release NotesDetect and interact with humans through change in capacitance. You can calibrate this library for your sensor very quickly to detect onNear(), onTouch(), and onPickup() events. 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_HumanSensor.h> RBD::HumanSensor human_sensor(2, 3); // send, receive pin void setup() { Serial.begin(115200); human_sensor.setNearValue(100); human_sensor.setTouchValue(200); human_sensor.setPickupValue(300); } void loop() { human_sensor.update(); if(human_sensor.onGone()) { Serial.print("0. GONE "); } else if(human_sensor.onNear()) { Serial.print("1. NEAR "); } else if(human_sensor.onTouch()) { Serial.print("2. TOUCH "); } else if(human_sensor.onPickup()) { Serial.print("3. PICKUP "); } if(human_sensor.onRawValueChange()) { Serial.println(human_sensor.getRawValue()); } }
Follow these steps to calibrate each human sensor before use:
You must continuously call update() inside of loop() in order to use theReal-Time methods tagged below.
RBD::HumanSensor
Pass in integers for the send and receive pins to create a new instance of this class.
RBD::HumanSensor human_sensor(2, 3); 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() { human_sensor.setSampleSize(1000); }
Provide an integer from getRawValue() to calibrate the lowest threshold for when a person is near the sensor.
void setup() { human_sensor.setNearValue(100); }
Provide an integer from getRawValue() to calibrate the lowest threshold for when a person is touching the sensor.
void setup() { human_sensor.setTouchValue(150); }
Provide an integer from getRawValue() to calibrate the lowest threshold for when a person is picking up the sensor.
void setup() { human_sensor.setPickupValue(200); }
Keep processing the readings and move this library along in real-time.
void loop() { human_sensor.update(); }
Returns true if no human is present and the sensor is reading a value below the setNearValue(). This will always return true if no thresholds (near, touch, pickup) are set.
void loop() { human_sensor.update(); if(human_sensor.isGone()) { ... } }
Returns true if a human is present and the sensor is reading a value at or above the setNearValue().
Returns false if the sensor is reading a value below the setNearValue(), or if a setTouchValue() has been assigned and the sensor is reading a value at or above the isTouch() value.
void loop() { human_sensor.update(); if(human_sensor.isNear()) { ... } }
Returns true if a human is touching the sensor and it is reading a value at or above the setTouchValue().
Returns false if the sensor is reading a value below the setTouchValue(), or if a setPickupValue() has been assigned and the sensor is reading a value at or above the isPickup() value.
void loop() { human_sensor.update(); if(human_sensor.isTouch()) { ... } }
Returns true if a human is picking up the sensor and it is reading a value at or above the setPickupValue().
Returns false if the sensor is reading a value below the setPickupValue().
void loop() { human_sensor.update(); if(human_sensor.isPickup()) { ... } }
This method will return true once the sensor registers a value below the setNearValue(). The active level must change away from and then come back to isGone() in order for this method to return true again.
void loop() { human_sensor.update(); if(human_sensor.onGone()) { // code only runs once per event Serial.println("Gone"); } }
This method will return true once the sensor registers a value at or above the setNearValue() and below the setTouchValue(). The active level must change away from and then come back to isNear() in order for this method to return true again.
void loop() { human_sensor.update(); if(human_sensor.onNear()) { // code only runs once per event Serial.println("Near"); } }
This method will return true once the sensor registers a value at or above the setTouchValue() and below the setPickupValue(). The active level must change away from and then come back to isTouch() in order for this method to return true again.
void loop() { human_sensor.update(); if(human_sensor.onTouch()) { // code only runs once per event Serial.println("Touch"); } }
This method will return true once the sensor registers a value at or above the setPickupValue(). The active level must change away from and then come back to isPickup() in order for this method to return true again.
void loop() { human_sensor.update(); if(human_sensor.onPickup()) { // code only runs once per event Serial.println("Pickup"); } }
Returns the capacitance sensor reading after being adjusted with the value given to setModifier().
This will return the raw capacitance sensor reading if you have not used setModifier() yet, or if you have called resetModifier().
void loop() { human_sensor.update(); if(human_sensor.onValueChange()) { Serial.println(human_sensor.getValue()); } }
Returns the raw capacitance sensor reading and ignores any value that has been passed to setModifier().
void loop() { human_sensor.update(); Serial.println(human_sensor.getRawValue()); }
Returns the current sensor level after being adjusted by setModifier().
void loop() { human_sensor.update(); Serial.println(human_sensor.getActiveLevel()); }
This method will return true once the getValue() changes.
void loop() { human_sensor.update(); if(human_sensor.onValueChange()) { // code only runs once per event Serial.println(human_sensor.getValue()); } }
This method will return true once the getRawValue() changes.
void loop() { human_sensor.update(); if(human_sensor.onRawValueChange()) { // code only runs once per event Serial.println(human_sensor.getRawValue()); } }
This method will return true once the getActiveLevel() changes.
void loop() { human_sensor.update(); if(human_sensor.onActiveLevelChange()) { // code only runs once per event Serial.println(human_sensor.getActiveLevel()); } }
Provide a positive or negative integer to temporarily adjust the human 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() { human_sensor.update(); if(...) { // motor is on human_sensor.setModifier(-200); } else { human_sensor.resetModifier(); } // when the motor is on; require +200 more from // the raw sensor reading to trigger all events if(human_sensor.isNear()) { ... } else if(...) { ... } }
Changes the setModifier() back to 0 and resets calibration of the human 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() { human_sensor.update(); if(...) { human_sensor.setModifier(-200); } else { // motor is off human_sensor.resetModifier(); } // when the motor is off; reset the // threshold values for all events if(human_sensor.isNear()) { ... } else if(...) { ... } }