-
Notifications
You must be signed in to change notification settings - Fork 10
general usage
Stefan Armborst edited this page Sep 2, 2022
·
11 revisions
A measurement is done by:
- start the measurement
- read the result
a blocking example:
#include <hp_BH1750.h> //include the library
hp_BH1750 sensor; //define the sensor
void setup()
{
sensor.begin(BH1750_TO_GROUND); // set address and initialize sensor
sensor.calibrateTiming(); // calibrate the timings, about 855ms with a bad chip
}
void loop()
{
sensor.start(); // start measurement with default settings
float lux = sensor.readLux(); // read the result
}a non-blocking example:
#include <hp_BH1750.h> //include the library
hp_BH1750 sensor; //define the sensor
void setup()
{
sensor.begin(BH1750_TO_GROUND); // set address and initialize the sensor
sensor.calibrateTiming(); // calibrate the timings, about 855ms with a bad chip
sensor.start(); // start measurement first time in setup
}
void loop()
{
if (sensor.hasValue()) { // most important function of this library!
float lux = sensor.getLux(); // read the result
sensor.start(); // only start the next measurement after getting the result
//do time consuming calculations here AFTER a new measurement was started.
}
// do a lot of other stuff here
}If you always use blocking reads or the non-blocking hasValue() with forceSensor = true, you don't need a calibration.
If your application reads the value at several places, it might be useful to know if the last result was already read.
This can be done with bool processed().
If you want to know if the sensor is busy at the moment, you may ask for getTime().
If the time is zero (0), the sensor is still collecting data.
If the time is higher then zero (0), then the sensor is idle and in low power mode.
Before the first measurement, the time is always zero (0).
However, if you design your applications like above, there is no reason to use these functions.
For further details, please refer to working principles.