-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD-Keypad-Shield_DS1307_DHT22_BMP180_GY302-BH1750.ino
203 lines (198 loc) · 4.95 KB
/
LCD-Keypad-Shield_DS1307_DHT22_BMP180_GY302-BH1750.ino
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
DateTime now;
RTC_DS3231 rtc;
#include <BH1750.h>
BH1750 lightMeter;
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
#include <SimpleDHT.h>
int pinDHT22 = 2;
SimpleDHT22 dht22(pinDHT22);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
unsigned long lastButtonPressTime = 0;
unsigned long lastAnalogReadTime = 0;
unsigned long buttonPressInterval = 250;
unsigned long analogReadInterval = 1000;
bool backlightOn = true;
float temperature = 0;
float humidity = 0;
float lumiere = 0;
float pressure = 0;
int lcd_key = 0;
int lcd_key2 = 0;
int adc_key_in = 0;
int menuIndex = 0;
int menuIndex2 = 0;
int read_LCD_buttons() {
adc_key_in = analogRead(0);
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE;
}
void toggleBacklight() {
backlightOn = !backlightOn;
if (backlightOn) {
lcd.display();
digitalWrite(10, HIGH);
} else {
lcd.noDisplay();
digitalWrite(10, LOW);
}
}
void updateTemperatureAndHumidity() {
int err = SimpleDHTErrSuccess;
if((err=dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess){
delay(200);
return;
}
}
void displayMenuVertical() {
switch(menuIndex2) {
case 0:
lcd.setCursor(0, 0);
lcd.print("Pression: ");
lcd.setCursor(0, 1);
if (millis() - lastAnalogReadTime > analogReadInterval) {
pressure = bmp.readPressure();
lastAnalogReadTime = millis();
}
lcd.print(pressure);
lcd.print(" Pa. ");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("Altitude: ");
lcd.setCursor(0, 1);
if (millis() - lastAnalogReadTime > analogReadInterval) {
lcd.print(bmp.readAltitude());
lcd.print(" m. ");
lastAnalogReadTime = millis();
}
break;
}
}
void displayMenu() {
switch(menuIndex) {
case 0:
lcd.setCursor(0, 0);
now = rtc.now();
lcd.setCursor(0,0);
lcd.print("Date:");
lcd.print(now.day());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.year());
lcd.setCursor(0, 1);
now = rtc.now();
lcd.print("Temps:");
lcd.print(now.hour());
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.print(now.second());
lcd.print(" ");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.setCursor(0, 1);
lcd.print(temperature);
lcd.print(" C. ");
break;
case 2:
lcd.setCursor(0, 0);
lcd.print("Humidite: ");
lcd.setCursor(0, 1);
lcd.print(humidity);
lcd.print("% ");
break;
case 3:
lcd.setCursor(0, 0);
lcd.print("Lux: ");
lcd.setCursor(0, 1);
if (millis() - lastAnalogReadTime > analogReadInterval) {
lumiere = lightMeter.readLightLevel();
lastAnalogReadTime = millis();
}
lcd.print(lumiere);
lcd.print(" Lx. ");
break;
case 4:
lcd_key2 = read_LCD_buttons();
if(lcd_key2 == btnUP) {
if (millis() - lastButtonPressTime > buttonPressInterval) {
menuIndex2++;
if (menuIndex2 > 1) {
menuIndex2 = 0;
}
lastButtonPressTime = millis();
}
}
if(lcd_key2 == btnDOWN) {
if (millis() - lastButtonPressTime > buttonPressInterval) {
menuIndex2--;
if (menuIndex2 < 0) {
menuIndex2 = 1;
}
lastButtonPressTime = millis();
}
}
displayMenuVertical();
break;
}
}
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
lcd.begin(16, 2);
lcd.display();
Wire.begin();
lightMeter.begin();
bmp.begin();
rtc.begin();
}
void loop() {
if (millis() - lastAnalogReadTime > analogReadInterval) {
updateTemperatureAndHumidity();
lastAnalogReadTime = millis();
}
lcd_key = read_LCD_buttons();
if(lcd_key == btnRIGHT) {
if (millis() - lastButtonPressTime > buttonPressInterval) {
menuIndex++;
if (menuIndex > 4) {
menuIndex = 0;
}
lastButtonPressTime = millis();
}
}
if(lcd_key == btnLEFT) {
if (millis() - lastButtonPressTime > buttonPressInterval) {
menuIndex--;
if (menuIndex < 0) {
menuIndex = 4;
}
lastButtonPressTime = millis();
}
}
if(lcd_key == btnSELECT) {
if (millis() - lastButtonPressTime > buttonPressInterval) {
toggleBacklight();
}
lastButtonPressTime = millis();
}
displayMenu();
}