Skip to content

Commit 6f7def1

Browse files
committed
Update 1602_keypad_shield.ino
1 parent 9c4e867 commit 6f7def1

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

1602_keypad_shield.ino

+39-32
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
77

88
// initialize a few values
99
int last_panel = 0;
10-
int press_count = 0;
1110
int timer = 0;
1211
int rtemp = 0;
1312
int system_mode = 0;
1413
int fan_mode = 0;
14+
int ctemp = 0;
15+
int humidity_lvl = 0;
1516

1617
int max_panels = 4;
1718
int panel_num = 1;
18-
int ctemp = 77;
1919

2020
// address of where were going to store the value in eeprom
2121
int rtemp_address = 0;
@@ -85,7 +85,7 @@ void loop() {
8585
lcd_key = read_LCD_buttons(); // read the buttons
8686

8787
// check for button activity, if not, turn off backlight
88-
if (lcd_key < 5) {
88+
if (lcd_key < btnNONE) {
8989
digitalWrite(BACKLIGHT, HIGH);
9090
timer = 0;
9191
// if its been awhile since anyone touched a button, lets turn off the backlight
@@ -96,16 +96,16 @@ void loop() {
9696
lcd.setCursor(0,0);
9797

9898
// Check values from LCD Keypad
99-
if (lcd_key == 3) {
99+
if (lcd_key == btnLEFT) {
100100
//Left
101101
state = 1;
102-
} else if (lcd_key == 0){
102+
} else if (lcd_key == btnRIGHT){
103103
//Right
104104
state = 2;
105-
} else if (lcd_key == 1){
105+
} else if (lcd_key == btnUP){
106106
//Up
107107
state = 3;
108-
} else if (lcd_key == 2){
108+
} else if (lcd_key == btnDOWN){
109109
//Down
110110
state = 4;
111111
}
@@ -141,7 +141,7 @@ void loop() {
141141
}
142142

143143
//display panel, but dont keep recreating it if no button is pressed
144-
if (panel_num == 1 || lcd_key != 5) {
144+
if (panel_num == 1 || lcd_key != btnNONE) {
145145
displayPanel(panel_num,lcd_key);
146146
}
147147

@@ -155,13 +155,14 @@ void loop() {
155155

156156
void humidity() {
157157
// code to get DHT reading coming soon
158-
int humidity_lvl = 44;
158+
humidity_lvl = 44;
159159
lcd.print("Humidity: ");
160160
lcd.print(humidity_lvl);
161161
lcd.print("%");
162162
}
163163

164164
void dht_temperature() {
165+
ctemp = 77;
165166
lcd.setCursor(0,0);
166167
lcd.print("Cur Temp: ");
167168
lcd.print(ctemp);
@@ -174,9 +175,9 @@ void requested_temperature(int lcd_key) {
174175
//rtemp
175176

176177
// add or increase temp based on up/down arrow
177-
if (lcd_key == 1) {
178+
if (lcd_key == btnUP) {
178179
rtemp = rtemp + 1;
179-
}else if (lcd_key == 2) {
180+
}else if (lcd_key == btnDOWN) {
180181
rtemp = rtemp - 1;
181182
}
182183

@@ -186,42 +187,45 @@ void requested_temperature(int lcd_key) {
186187
lcd.print((char)223);
187188

188189
// whene select button pressed, we save the data to eeprom and print SAVED to lcd
189-
if (lcd_key == 4) {
190+
if (lcd_key == btnSELECT) {
190191
lcd.clear();
191192
eeprom_write(rtemp_address, rtemp);
192193
}
193194
}
194195

195196
void mode(int lcd_key) {
196197

197-
//example system_mode value from eeprom
198-
//system_mode = 2;
199-
200198
lcd.print("Mode: ");
201-
202-
// press_count used for cycling through values below
203-
press_count++;
204199
lcd.setCursor(6,0);
205-
switch (press_count % 3) {
200+
201+
// Now change the unsaved mode
202+
if (lcd_key == btnUP) {
203+
if (system_mode < 2) system_mode++;
204+
}
205+
else if (lcd_key == btnDOWN) {
206+
if (system_mode > 0) system_mode--;
207+
}
208+
switch(system_mode) {
206209
case 0:
207210
lcd.print("Off ");
208-
system_mode = 0;
209211
break;
210212
case 1:
211213
lcd.print("Cool");
212-
system_mode = 1;
213214
break;
214215
case 2:
215216
lcd.print("Heat");
216-
system_mode = 2;
217+
break;
218+
default:
219+
// if somehow, mode gets out of range, do something
220+
// about it here. Sensible fail-safe default may be OFF?
221+
system_mode = 0;
217222
break;
218223
}
219-
220224
// whene select button pressed, we save the data to eeprom and print SAVED to lcd
221-
if (lcd_key == 4) {
225+
if (lcd_key == btnSELECT) {
222226
lcd.clear();
223227
eeprom_write(mode_address, system_mode);
224-
}
228+
}
225229
}
226230

227231
void fan(int lcd_key) {
@@ -230,11 +234,16 @@ void fan(int lcd_key) {
230234
//fan_mode = 1;
231235

232236
lcd.print("Fan: ");
233-
234-
// press_count used for cycling through values below
235-
press_count++;
236237
lcd.setCursor(5,0);
237-
switch (press_count % 2) {
238+
239+
// Now change the unsaved mode
240+
if (lcd_key == btnUP) {
241+
if (fan_mode < 1) fan_mode++;
242+
}
243+
else if (lcd_key == btnDOWN) {
244+
if (fan_mode > 0) fan_mode--;
245+
}
246+
switch (fan_mode) {
238247
case 0:
239248
lcd.print("Auto");
240249
fan_mode = 0;
@@ -245,10 +254,8 @@ void fan(int lcd_key) {
245254
break;
246255
}
247256

248-
Serial.println(press_count);
249-
250257
// whene select button pressed, we save the data to eeprom and print SAVED to lcd
251-
if (lcd_key == 4) {
258+
if (lcd_key == btnSELECT) {
252259
lcd.clear();
253260
eeprom_write(fan_address, fan_mode);
254261
}

0 commit comments

Comments
 (0)