-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsharp_sensor.h
45 lines (28 loc) · 1.05 KB
/
sharp_sensor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "esphome.h"
class My_dust_Sensor : public PollingComponent, public Sensor {
public:
My_dust_Sensor() : PollingComponent(15000) {} // period between updates, here 10s
void setup() override {
pinMode(D6, OUTPUT); // sensor led pin
pinMode(A0, INPUT); // output form sensor
}
void update() override {
int o;
int value = 0;
int numReadings = 10;
float dustDensity = 0;
float voMeasured = 0;
for (o = 0; o < numReadings; o++){
digitalWrite(D6,LOW); // power on the LED
delayMicroseconds(280); // delay
value = value + analogRead(A0); // read ADC (sensor)
delayMicroseconds(40); // delay
digitalWrite(D6,HIGH); // turn the LED off
delayMicroseconds(9680); // delay
//delay(1000);
}
value = value / numReadings; // smoothing
value = 0.17 * value - 0.1; // calculate value (for GP2Y1010AU0F reads)
publish_state(value); // publish
}
};