ASensor
 All Classes Functions Pages
DustSensor.h
1 #pragma once
2 /*
3  ASensor - Sensor library. Can be used standalone or in conjunction with AWind library
4  Copyright (C)2015 Andrei Degtiarev. All right reserved
5 
6  You can always find the latest version of the library at
7  https://github.com/AndreiDegtiarev/ASensor
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the MIT license.
11  Please see the included documents for further information.
12 */
14 class DustSensor : public ISensor
15 {
16  int _port;
17  unsigned long _starttime;
18  unsigned long _sampletime_ms;
19  unsigned long _lowpulseoccupancy;
20  float _concentration;
21 public:
22  DustSensor(int port,unsigned long sample_time_ms)
23  {
24  _port=port;
25  _sampletime_ms=sample_time_ms;
26  _lowpulseoccupancy=0;
27  _concentration=0;
28  pinMode(_port,INPUT);
29  _starttime = millis();//get the current time;
30  }
31  const __FlashStringHelper* Name()
32  {
33  return F("Dust");
34  }
35  float LowMeasurementLimit()
36  {
37  return 0;
38  }
39  float HighMeasurementLimit()
40  {
41  return 100000;
42  }
43  int Precission()
44  {
45  return 0;
46  }
47  bool Measure(float &data)
48  {
49  unsigned long duration = pulseIn(_port, LOW);
50  _lowpulseoccupancy += duration;
51  unsigned long cur_sample_time=millis()-_starttime;
52  if ((cur_sample_time > _sampletime_ms && _lowpulseoccupancy>0) || (cur_sample_time/_sampletime_ms)>10)//if the sampel time == 30s
53  {
54  float ratio = _lowpulseoccupancy/(cur_sample_time*10.0); // Integer percentage 0=>100
55  _concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
56  _lowpulseoccupancy = 0;
57  _starttime = millis();
58  }
59  data=_concentration;
60  return true;
61  }
62 };
Dust sensor from seeedstudio. No any external libraries are neccesary. Details to member functions se...
Definition: DustSensor.h:14