-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalogSensor.cpp
62 lines (51 loc) · 1.34 KB
/
analogSensor.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
60
61
62
#include <Arduino.h>
#include "analogSensor.h"
#include "common.h"
analogSensor::analogSensor(uint8_t pin, uint8_t capabilities) {
pinMode(pin, INPUT);
this->pin = pin;
this->min_value = 0;
this->max_value = 1023;
this->capabilities = capabilities;
}
int analogSensor::get_value() {
return analogRead(this->pin);
}
void analogSensor::calibrate(unsigned long delay, int tolerance) {
unsigned long start;
int value;
this->min_value = 1023;
this->max_value = 0;
start = millis();
do {
value = get_value();
if(value > this->max_value) {
this->max_value = value;
}
if(value < this->min_value) {
this->min_value = value;
}
} while(start + delay > millis());
if(tolerance > 0) {
min_value -= (min_value * tolerance) / 100;
if(min_value < 0) {
min_value = 0;
}
max_value += (max_value * tolerance) / 100;
if(max_value > 1023) {
max_value = 1023;
}
}
}
int analogSensor::get_minimal() {
return this->min_value;
}
int analogSensor::get_maximal() {
return this->max_value;
}
uint8_t analogSensor::is_sound_sensor() {
return this->capabilities & DETECTS_SOUND;
}
uint8_t analogSensor::is_light_sensor() {
return this->capabilities & DETECTS_LIGHT;
}