This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sensorHandler.h
139 lines (128 loc) · 3.05 KB
/
sensorHandler.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef SENSOR_HANDLER_H
#define SENSOR_HANDLER_H
#ifdef USE_TEMP_SENSOR
OneWire oneWire(TEMP_PIN);
DallasTemperature sensors(&oneWire);
#endif
byte bcdToDec(byte val) {
return ( (val / 16 * 10) + (val % 16) );
}
class SensorHandler {
private:
bool button1, button2, button3;
int temperature;
String hours;
String minutes;
int lsVarr[100] = {0};
byte lsVcount = 0;
long buttonPollTime;
long buttonPollDuration;
public:
SensorHandler() {
hours = "00";
minutes = "00";
temperature = 0;
button1 = button2 = button3 = false;
buttonPollTime = millis();
buttonPollDuration = 300;
};
byte readButtons()
{
#ifdef USE_BUTTONS
if (buttonPollTime + buttonPollDuration < millis()) {
if (digitalRead(BUTTON1_PIN) == LOW)
{
Serial.println("Button1 pressed");
buttonPollTime = millis();
return 1;
}
else if (digitalRead(BUTTON2_PIN) == LOW)
{
Serial.println("Button2 pressed");
buttonPollTime = millis();
return 2;
}
else if (digitalRead(BUTTON3_PIN) == LOW)
{
Serial.println("Button3 pressed");
buttonPollTime = millis();
return 3;
}
}
#endif
return 0;
}
int readTemp() {
#ifdef USE_TEMP_SENSOR
EVERY_N_SECONDS(5) {
sensors.requestTemperatures(); // Send the command to get temperatures
temperature = round(sensors.getTempCByIndex(0));
}
return temperature;
#else
return -99;
#endif
}
void controllBrightness() {
#ifdef USE_LIGHT_SENSOR
int lsV = analogRead(LS_PIN);
lsVarr[lsVcount] = lsV;
lsVcount++;
if (lsVcount >= 100)
{
lsVcount = 0;
}
lsV = 0;
for (int i = 0; i < 100; i++)
{
lsV += lsVarr[i];
}
lsV /= 100;
float targetB;
float bMin = 5;
float bMax = 70;
float minSense = 3000;
float maxSense = 1000;
targetB = ((lsV - maxSense) / (minSense - maxSense)) * (bMin - bMax) + bMax;
if (lsV > minSense) {
targetB = 5;
}
if (lsV < maxSense) {
targetB = bMax;
}
FastLED.setBrightness(targetB);
#endif
}
void getTime(String &outMinutes, String &outHours) {
readTimeRTC();
outMinutes = minutes;
outHours = hours;
}
void readTimeRTC()
{
#ifdef USE_RTC_CLOCK
Wire.beginTransmission(RTC_I2C_ADDRESS); //Aufbau der Verbindung
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(RTC_I2C_ADDRESS, 7);
bcdToDec(Wire.read());
minutes = String(bcdToDec(Wire.read()));
hours = String(bcdToDec(Wire.read()));
if (minutes.length() == 1)
{
minutes += minutes[0];
minutes[0] = '0';
}
if (hours.length() == 1)
{
hours += hours[0];
hours[0] = '0';
}
bcdToDec(Wire.read());
bcdToDec(Wire.read());
bcdToDec(Wire.read());
bcdToDec(Wire.read());
#endif
}
};
#endif