ZPH02 is a simple Arduino library for Reading AQI moving averages Or Duty Cycle from the sensor ZPH02.
This sensor operates as a pwm source with duty cycle as a output variable in the default mode.
The user specifies the interval (number of data points) for the moving average in the constructor. When the begin(pin)
function is called, an array is dynamically allocated to hold the number of data points in the interval. This array is never deallocated, and the user should call begin()
only once (for a given zph02
instance) in setup or other initialization code. Dynamic allocation is used strictly with the intent of creating the proper size array for the user's purposes, and not to free up the memory at a later point. It is strongly recommended that zph02
objects remain allocated as long as the code is running. Failure to observe these guidelines can result in heap fragmentation, crashes and other undesired behavior.
Defines a zph02
object where the average is calculated using interval data points.
zph02(interval);
interval: The number of data points to use when calculating the moving average. (float)
None.
zph02 aqi_sensor(10); // use 10 data points for the moving average
Initializes a zph02
object. Call begin(pin)
once and only once for any given zph02
instance. See comments in the Description section above.
begin(5);
GPIO Pin
None.
aqi_sensor.begin(5);
Adds a new data point to the moving average. Returns the new moving average value. Until the interval array is filled, the average is calculated from those data points from AQI of the sensor output pulses, i.e. a fewer number of points than defined by the constructor.
###Read AQI
getAqi(false);
###Read Duty Cycle
getAqi(true);
True = DutyCycle, No = AQI Moving Average
The new moving average value OR DutyCycle
// Get Duty Cycle from Sensor
float dutycycle = aqi_sensor.getAqi(true);
// Get AQI Moving Average from Sensor
float AQI = aqi_sensor.getAqi(false);
Returns the current moving average value without adding a new reading.
getAvg();
None.
The moving average value. (float)
float formulaResultAvg = aqi_sensor.getAvg();
Restarts the moving average. Zeros the interval array and associated data.
reset();
None.
None.
aqi_sensor.reset();
##This library is written with the help of :
https://github.com/ThingEngineer/movingAvg
Written by Jack Christensen
Mar 2012