-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySensor_LDR.cpp
59 lines (50 loc) · 1.52 KB
/
MySensor_LDR.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*------------------------------------------- Sensor LDR Class --------------------------------------------*/
/* This define MySensor_LM35 class function
*
* Created 2017
* by Nguyen Thanh Long
* Modified 13 April 2017 - version 1
* by Nguyen Thanh Long
*
* Reference:
--> https://github.com/QuentinCG/Arduino-Light-Dependent-Resistor-Library/blob/master/LightDependentResistor/LightDependentResistor.h
--> https://www.allaboutcircuits.com/projects/design-a-luxmeter-using-a-light-dependent-resistor/
*
* This code is released in public domains
*/
#include "MySensor.h"
MySensor_LDR::MySensor_LDR() {
// Do nothing
}
MySensor_LDR::MySensor_LDR(uint8_t pin) : pinData(pin) {
pinMode(this->pinData, INPUT);
}
bool MySensor_LDR::begin(uint8_t pin) {
this->pinData = pin;
pinMode(this->pinData, INPUT);
return true;
}
bool MySensor_LDR::config(int8_t arefVol, uint32_t otherRes) {
this->refVol = arefVol;
this->refRes = otherRes;
return true;
}
float MySensor_LDR::getlastLux(void) {
return this->lastLux;
}
float MySensor_LDR::readLux(void) {
float val = analogRead(this->pinData);
float resVol = (val / 1024.0) * this->refVol;
float ldrVol = this->refVol - resVol;
float ldrRes = (ldrVol / resVol) * this->refRes;
float result = 12518931.0 / (float)pow(ldrRes, 1.405);
return lastLux = result;
}
float MySensor_LDR::readLux(uint8_t times) {
if (times <= 0) times = 1; // Repair error input
float sum = 0;
for (int i = 0; i < times; i++) {
sum += readLux();
}
return sum / times;
}