Components and supplies
Tactile Switch, Top Actuated
KEYESTUDIO 1602 LCD with I2C backpack
Arduino UNO
Project description
Code
SixButtonMenuConfiguration.ino
arduino
Program code. Modify to your needs. Have fun with it!
1 2// Program written by Dan Hostler, October 2020. 3// Feel free to use as you need to. 4// PayPal donations appreciated at danhostler1985@yahoo.com 5 6 7// LIBRARIES 8#include <Wire.h> // deals with I2C connections 9#include <LiquidCrystal_I2C.h> // activates the LCD I2C library 10 11// MENU ARRAYS 12String menuOption[] = {{"Setting 1"}, {"Setting 2"}, {"Setting 3"}, {"Setting 4"}, {"Setting 5"}}; // Text on the top line 13String measurementType[] = {{"[DEG. FAHR]"}, {"[DEG. CEL]"}, {"[POUNDS]"}, {"[INCHES]"}, {"[MPH]"}}; // Text on the bottom line 14 15// PINS ON THE ARDUINO. 16// MAKE THESE "CONST INT" VARIABLES AS THESE DO NOT CHANGE IN THE PROGRAM. 17const int leftButtonPin = 5; // Pin 5 for "Left" command 18const int rightButtonPin = 6; // Pin 6 for "Right" command 19const int upButtonPin = 7; // Pin 7 for "Up" command 20const int downButtonPin = 8; // Pin 8 for "Down" command 21const int enterButtonPin = 9; // Pin 9 for "Enter" command 22const int clearButtonPin = 10; // Pin 10 for "Clear" command 23 24// NUMBER COUNT OF BUTTON PRESSES AND COUNTER UNITS. 25// MAKE THESE A "INT" VARIABLES TO ALLOW FOR NEGATIVE INTEGERS. 26int setting1Counter = 0; // Counters for settings 1 - 5 27int setting2Counter = 0; 28int setting3Counter = 0; 29int setting4Counter = 0; 30int setting5Counter = 0; 31int directionPush = 0; // This counter changes the menu option with each "left" or "right" button push. 32int upPressCount = 0; // This counter measures the amount of times the user pushes the "up" button. 33int downPressCount = 0; // This counter measures the amount of times the user pushes the "down" button. 34 35// BUTTON PRESS STATES FOR EACH FUNCTION, ALL SET TO "LOW". 36// MAKE THESE "BOOLEAN" VARIABLES AS THESE ONLY WILL BE "HIGH" OR "LOW". 37boolean buttonStateLeft = LOW; // Button states for the "Left" command 38boolean lastButtonStateLeft = LOW; 39boolean currentButtonStateLeft = LOW; 40boolean buttonStateRight = LOW; // Button states for the "Right" command 41boolean lastButtonStateRight = LOW; 42boolean currentButtonStateRight = LOW; 43boolean buttonStateUp = LOW; // Button states for the "Up" command 44boolean lastButtonStateUp = LOW; 45boolean currentButtonStateUp = LOW; 46boolean buttonStateDown = LOW; // Button states for the "Down" command 47boolean lastButtonStateDown = LOW; 48boolean currentButtonStateDown = LOW; 49boolean buttonStateEnter = LOW; // Button states for the "Enter" command 50boolean lastButtonStateEnter = LOW; 51boolean currentButtonStateEnter = LOW; 52boolean buttonStateClear = LOW; // Button states for the "Clear" command 53boolean lastButtonStateClear = LOW; 54boolean currentButtonStateClear = LOW; 55 56// DEBOUNCE VARIABLES TO MEASURE THE DEBOUNCING TIME OF A BUTTON PUSH. 57// MAKE THESE "UNSIGNED LONG" VARIABLES AS THE NUMERICAL VALUE WILL HAVE AN EXTENDED SIZE. 58unsigned long lastDebounceTime = 0; // This variable takes a "snapshot" of time when any button is pushed. 59unsigned long debounceDelay = 50; // Delay time in milliseconds; the amount of time the button is pressed must be higher than the delay to register a push. 60 61 62// OBJECT DECLARATION 63LiquidCrystal_I2C lcd(0x27, 16, 2); // Activates the LCD "object" 64 65 66 67// SETUP 68void setup() { 69 70 lcd.backlight(); // THESE COMMANDS TURN ON AND CLEAR THE LCD SCREEN 71 lcd.init(); 72 lcd.clear(); 73 74 pinMode(leftButtonPin, INPUT); // SETS THE leftButtonPin AS AN INPUT 75 pinMode(rightButtonPin, INPUT); // SETS THE rightButtonPin AS AN INPUT 76 pinMode(upButtonPin, INPUT); // SETS THE upButtonPin AS AN INPUT 77 pinMode(downButtonPin, INPUT); // SETS THE downButtonPin AS AN INPUT 78 pinMode(enterButtonPin, INPUT); // SETS THE enterButtonPin AS AN INPUT 79 pinMode(clearButtonPin, INPUT); // SETS THE clearButtonPin AS AN INPUT 80 81 delay(1000); 82 83} 84 85 86 87// MAIN LOOP 88void loop() { 89 90 int unitSetting[] = {setting1Counter, setting2Counter, setting3Counter, setting4Counter, setting5Counter}; // This variable holds the individual counters in one array 91 92 lcd.setCursor(0,0); // Menu displayed on the LCD. 93 lcd.print(menuOption[directionPush]); // The menuOption that is displayed is determined by the left or right push. 94 lcd.setCursor(0,1); 95 lcd.print(unitSetting[directionPush]); // The setting counter that is displayed is determined by the left or right push. 96 lcd.setCursor(5,1); // This cursor setting fixes the measurementType in one place, preventing offset 97 lcd.print(measurementType[directionPush]); // by the unitSetting. 98 99 // The program at this point is waiting for a button press. 100 currentButtonStateLeft = digitalRead(leftButtonPin); 101 currentButtonStateRight = digitalRead(rightButtonPin); 102 currentButtonStateUp = digitalRead(upButtonPin); 103 currentButtonStateDown = digitalRead(downButtonPin); 104 currentButtonStateEnter = digitalRead(enterButtonPin); 105 currentButtonStateClear = digitalRead(clearButtonPin); 106 107 if (currentButtonStateLeft != lastButtonStateLeft || currentButtonStateRight != lastButtonStateRight || 108 currentButtonStateUp != lastButtonStateUp || currentButtonStateDown != lastButtonStateDown || currentButtonStateEnter != lastButtonStateEnter) 109 // If there is a button push on any of the buttons, the following routine runs to check if it was a valid press: 110 { 111 lastDebounceTime = millis(); // lastDebounceTime is set equal to the running millis() function. 112 } 113 114 if ((millis() - lastDebounceTime) > debounceDelay) 115 // If the lastDebounceTime (aka. the "snapshot" time) minus the running millis() function is higher than the set debounce delay, the following routine 116 // below runs and checks which button was pushed: 117 { 118 119 // The current state for each button is set not equal to the pressed state and when it changes, the pressed state becomes equal to the current state. 120 121 // LEFT BUTTON PRESS 122 if (currentButtonStateLeft != buttonStateLeft) // Left button scrolls the menu options to the left. 123 { 124 buttonStateLeft = currentButtonStateLeft; 125 126 if (buttonStateLeft == LOW) // Once the button is released, the push is registered and the code below runs. 127 { 128 directionPush--; // Both the up and down press counts will be reset to zero when the left button is pushed. 129 upPressCount = 0; 130 downPressCount = 0; 131 } 132 133 if (directionPush < 0) // If the user tries to scroll below the first menu option, 134 { // the program will loop back to the last menu option. 135 directionPush = 4; 136 } 137 lcd.clear(); 138 } 139 140 // RIGHT BUTTON PRESS 141 if (currentButtonStateRight != buttonStateRight) // Right button scrolls the menu options to the right. 142 { 143 buttonStateRight = currentButtonStateRight; 144 145 if (buttonStateRight == LOW) 146 { 147 directionPush++; // Both the up and down press counts will be reset to zero when the right button is pushed. 148 upPressCount = 0; 149 downPressCount = 0; 150 } 151 152 if (directionPush > 4) // If the user tries to scroll above the last menu option, 153 { // the program will loop back to the first menu option. 154 directionPush = 0; 155 } 156 lcd.clear(); 157 } 158 159 // UP BUTTON PRESS 160 if (currentButtonStateUp != buttonStateUp) // Up button scrolls the setting upward. 161 { 162 buttonStateUp = currentButtonStateUp; 163 164 if (buttonStateUp == LOW && directionPush == 0) // The first 5 times in which the "up" button is pushed, each push will add 1 increment to the setting. 165 { 166 upPressCount++; 167 downPressCount = 0; // The downPressCount is reset to zero. 168 setting1Counter++; 169 170 if (upPressCount > 5) // If the "up" button is pushed more than 5 times consecutively, the setting increment increases by 5 171 { // with every "up" button push and resets back when the down, left or right button is pushed. 172 setting1Counter = setting1Counter + 4; 173 } 174 175 if (setting1Counter > 999) // Sets the setting counter limit to 999. The user cannot increase the counter beyond 999. 176 { 177 setting1Counter = 999; 178 } 179 } 180 181 if (buttonStateUp == LOW && directionPush == 1) 182 { 183 upPressCount++; 184 downPressCount = 0; 185 setting2Counter++; 186 187 if (upPressCount > 5) 188 { 189 setting2Counter = setting2Counter + 4; 190 } 191 192 if (setting2Counter > 999) // Sets the setting counter limit to 999. The user cannot increase the counter beyond 999. 193 { 194 setting2Counter = 999; 195 } 196 } 197 198 if (buttonStateUp == LOW && directionPush == 2) 199 { 200 upPressCount++; 201 downPressCount = 0; 202 setting3Counter++; 203 204 if (upPressCount > 5) 205 { 206 setting3Counter = setting3Counter + 4; 207 } 208 209 if (setting3Counter > 999) // Sets the setting counter limit to 999. The user cannot increase the counter beyond 999. 210 { 211 setting3Counter = 999; 212 } 213 } 214 215 if (buttonStateUp == LOW && directionPush == 3) 216 { 217 upPressCount++; 218 downPressCount = 0; 219 setting4Counter++; 220 221 if (upPressCount > 5) 222 { 223 setting4Counter = setting4Counter + 4; 224 } 225 226 if (setting4Counter > 999) // Sets the setting counter limit to 999. The user cannot increase the counter beyond 999. 227 { 228 setting4Counter = 999; 229 } 230 } 231 232 if (buttonStateUp == LOW && directionPush == 4) 233 { 234 upPressCount++; 235 downPressCount = 0; 236 setting5Counter++; 237 238 if (upPressCount > 5) 239 { 240 setting5Counter = setting5Counter + 4; 241 } 242 243 if (setting5Counter > 999) // Sets the setting counter limit to 999. The user cannot increase the counter beyond 999. 244 { 245 setting5Counter = 999; 246 } 247 } 248 lcd.clear(); 249 } 250 251 252 // DOWN BUTTON PRESS 253 if (currentButtonStateDown != buttonStateDown) // Down button scrolls the setting downward. 254 { 255 buttonStateDown = currentButtonStateDown; 256 257 if (buttonStateDown == LOW && directionPush == 0) // The first 5 times in which the "down" button is pushed, each push will subtract 1 increment to the setting. 258 { 259 downPressCount++; 260 upPressCount = 0; // The upPressCount is reset to zero. 261 setting1Counter--; 262 263 if (downPressCount > 5) // If the "down" button is pushed more than 5 times consecutively, the setting increment decreases by 5 264 { // with every "down" button push and resets back when the up, left or right button is pushed. 265 setting1Counter = setting1Counter - 4; 266 } 267 268 if (setting1Counter < -999) // Sets the setting counter limit to -999. The user cannot increase the counter beyond -999. 269 { 270 setting1Counter = -999; 271 } 272 } 273 274 if (buttonStateDown == LOW && directionPush == 1) 275 { 276 downPressCount++; 277 upPressCount = 0; 278 setting2Counter--; 279 280 if (downPressCount > 5) 281 { 282 setting2Counter = setting2Counter - 4; 283 } 284 285 if (setting2Counter < -999) // Sets the setting counter limit to -999. The user cannot decrease the counter beyond -999. 286 { 287 setting2Counter = -999; 288 } 289 } 290 291 if (buttonStateDown == LOW && directionPush == 2) 292 { 293 downPressCount++; 294 upPressCount = 0; 295 setting3Counter--; 296 297 if (downPressCount > 5) 298 { 299 setting3Counter = setting3Counter - 4; 300 } 301 302 if (setting3Counter < 0) // This code prevents the user from entering 303 { // a number below "0". 304 setting3Counter = 0; // Remove this code if you want to allow in 305 } // negative numbers on a setting. 306 } 307 308 if (buttonStateDown == LOW && directionPush == 3) 309 { 310 downPressCount++; 311 upPressCount = 0; 312 setting4Counter--; 313 314 if (downPressCount > 5) 315 { 316 setting4Counter = setting4Counter - 4; 317 } 318 319 if (setting4Counter < 0) // This code prevents the user from entering 320 { // a number below "0". 321 setting4Counter = 0; // Remove this code if you want to allow in 322 } // negative numbers on a setting. 323 } 324 325 if (buttonStateDown == LOW && directionPush == 4) 326 { 327 downPressCount++; 328 upPressCount = 0; 329 setting5Counter--; 330 331 if (downPressCount > 5) 332 { 333 setting5Counter = setting5Counter - 4; 334 } 335 336 if (setting5Counter < 0) // This code prevents the user from entering 337 { // a number below "0". 338 setting5Counter = 0; // Remove this code if you want to allow in 339 } // negative numbers on a setting. 340 } 341 342 lcd.clear(); 343 } 344 345 } 346 347 348 // ENTER BUTTON PRESS 349 if (currentButtonStateEnter != buttonStateEnter) 350 { 351 buttonStateEnter = currentButtonStateEnter; 352 353 if (buttonStateEnter == LOW && directionPush == 0) // The Enter button simply enters the setting and flashes a brief message. 354 { // Please feel free to expand on this code to add more functions. 355 lcd.clear(); 356 lcd.setCursor(0,0); 357 lcd.print("DEGREES FAHREN."); 358 lcd.setCursor(0,1); 359 lcd.print("IS ENTERED"); 360 delay(2000); 361 } 362 363 if (buttonStateEnter == LOW && directionPush == 1) 364 { 365 lcd.clear(); 366 lcd.setCursor(0,0); 367 lcd.print("DEGREES CELSIUS"); 368 lcd.setCursor(0,1); 369 lcd.print("IS ENTERED"); 370 delay(2000); 371 } 372 373 if (buttonStateEnter == LOW && directionPush == 2) 374 { 375 lcd.clear(); 376 lcd.setCursor(0,0); 377 lcd.print("WEIGHT IN LBS."); 378 lcd.setCursor(0,1); 379 lcd.print("IS ENTERED"); 380 delay(2000); 381 } 382 383 if (buttonStateEnter == LOW && directionPush == 3) 384 { 385 lcd.clear(); 386 lcd.setCursor(0,0); 387 lcd.print("INCHES"); 388 lcd.setCursor(0,1); 389 lcd.print("IS ENTERED"); 390 delay(2000); 391 } 392 393 if (buttonStateEnter == LOW && directionPush == 4) 394 { 395 lcd.clear(); 396 lcd.setCursor(0,0); 397 lcd.print("MILES PER HOUR"); 398 lcd.setCursor(0,1); 399 lcd.print("IS ENTERED"); 400 delay(2000); 401 } 402 lcd.clear(); 403 } 404 405 406 // CLEAR BUTTON PRESS 407 if (currentButtonStateClear != buttonStateClear) 408 { 409 buttonStateClear = currentButtonStateClear; 410 411 if (buttonStateClear == LOW && directionPush == 0) // The Clear button clears all setting data depending on what menu option you are viewing. 412 { // It flahses a brief message stating that the data has been cleared. 413 lcd.clear(); // The press counts for both the up and down variables are also reset to zero. 414 lcd.setCursor(0,0); 415 lcd.print("DEGREES FAHREN."); 416 lcd.setCursor(0,1); 417 lcd.print("IS CLEARED"); 418 setting1Counter = 0; 419 downPressCount = 0; 420 upPressCount = 0; 421 delay(2000); 422 } 423 424 if (buttonStateClear == LOW && directionPush == 1) 425 { 426 lcd.clear(); 427 lcd.setCursor(0,0); 428 lcd.print("DEGREES CELSIUS"); 429 lcd.setCursor(0,1); 430 lcd.print("IS CLEARED"); 431 setting2Counter = 0; 432 downPressCount = 0; 433 upPressCount = 0; 434 delay(2000); 435 } 436 437 if (buttonStateClear == LOW && directionPush == 2) 438 { 439 lcd.clear(); 440 lcd.setCursor(0,0); 441 lcd.print("WEIGHT IN LBS."); 442 lcd.setCursor(0,1); 443 lcd.print("IS CLEARED"); 444 setting3Counter = 0; 445 downPressCount = 0; 446 upPressCount = 0; 447 delay(2000); 448 } 449 450 if (buttonStateClear == LOW && directionPush == 3) 451 { 452 lcd.clear(); 453 lcd.setCursor(0,0); 454 lcd.print("INCHES"); 455 lcd.setCursor(0,1); 456 lcd.print("IS CLEARED"); 457 setting4Counter = 0; 458 downPressCount = 0; 459 upPressCount = 0; 460 delay(2000); 461 } 462 463 if (buttonStateClear == LOW && directionPush == 4) 464 { 465 lcd.clear(); 466 lcd.setCursor(0,0); 467 lcd.print("MILES PER HOUR"); 468 lcd.setCursor(0,1); 469 lcd.print("IS CLEARED"); 470 setting5Counter = 0; 471 downPressCount = 0; 472 upPressCount = 0; 473 delay(2000); 474 } 475 lcd.clear(); 476 } 477 478 479 480 // After a button is pushed and the count recorded, all the states reset back to LOW for the data to be processed correctly. 481 lastButtonStateLeft = currentButtonStateLeft; // resets the left button state to LOW 482 lastButtonStateRight = currentButtonStateRight; // resets the right button state to LOW 483 lastButtonStateUp = currentButtonStateUp; // resets the up button state to LOW 484 lastButtonStateDown = currentButtonStateDown; // resets the down button state to LOW 485 lastButtonStateEnter = currentButtonStateEnter; // resets the enter button state to LOW 486 lastButtonStateClear = currentButtonStateClear; // resets the clear button state to LOW 487 488} 489
SixButtonMenuConfiguration.ino
arduino
Program code. Modify to your needs. Have fun with it!
1 2// Program written by Dan Hostler, October 2020. 3// Feel free to 4 use as you need to. 5// PayPal donations appreciated at danhostler1985@yahoo.com 6 7 8// 9 LIBRARIES 10#include <Wire.h> // deals with I2C connections 11#include 12 <LiquidCrystal_I2C.h> // activates the LCD I2C library 13 14// MENU ARRAYS 15String 16 menuOption[] = {{"Setting 1"}, {"Setting 2"}, {"Setting 3"}, {"Setting 4"}, 17 {"Setting 5"}}; // Text on the top line 18String measurementType[] = {{"[DEG. 19 FAHR]"}, {"[DEG. CEL]"}, {"[POUNDS]"}, {"[INCHES]"}, {"[MPH]"}}; // Text 20 on the bottom line 21 22// PINS ON THE ARDUINO. 23// MAKE THESE "CONST INT" 24 VARIABLES AS THESE DO NOT CHANGE IN THE PROGRAM. 25const int leftButtonPin = 5; 26 // Pin 5 for "Left" command 27const int rightButtonPin = 6; // Pin 28 6 for "Right" command 29const int upButtonPin = 7; // Pin 7 for "Up" 30 command 31const int downButtonPin = 8; // Pin 8 for "Down" command 32const 33 int enterButtonPin = 9; // Pin 9 for "Enter" command 34const int clearButtonPin 35 = 10; // Pin 10 for "Clear" command 36 37// NUMBER COUNT OF BUTTON PRESSES 38 AND COUNTER UNITS. 39// MAKE THESE A "INT" VARIABLES TO ALLOW FOR NEGATIVE INTEGERS. 40int 41 setting1Counter = 0; // Counters for settings 1 - 5 42int setting2Counter = 43 0; 44int setting3Counter = 0; 45int setting4Counter = 0; 46int setting5Counter 47 = 0; 48int directionPush = 0; // This counter changes the menu option with 49 each "left" or "right" button push. 50int upPressCount = 0; // This counter 51 measures the amount of times the user pushes the "up" button. 52int downPressCount 53 = 0; // This counter measures the amount of times the user pushes the "down" 54 button. 55 56// BUTTON PRESS STATES FOR EACH FUNCTION, ALL SET TO "LOW". 57// 58 MAKE THESE "BOOLEAN" VARIABLES AS THESE ONLY WILL BE "HIGH" OR "LOW". 59boolean 60 buttonStateLeft = LOW; // Button states for the "Left" command 61boolean 62 lastButtonStateLeft = LOW; 63boolean currentButtonStateLeft = LOW; 64boolean 65 buttonStateRight = LOW; // Button states for the "Right" command 66boolean 67 lastButtonStateRight = LOW; 68boolean currentButtonStateRight = 69 LOW; 70boolean buttonStateUp = LOW; // Button states for the "Up" 71 command 72boolean lastButtonStateUp = LOW; 73boolean currentButtonStateUp 74 = LOW; 75boolean buttonStateDown = LOW; // Button states for the "Down" 76 command 77boolean lastButtonStateDown = LOW; 78boolean currentButtonStateDown 79 = LOW; 80boolean buttonStateEnter = LOW; // Button states for the "Enter" 81 command 82boolean lastButtonStateEnter = LOW; 83boolean currentButtonStateEnter 84 = LOW; 85boolean buttonStateClear = LOW; // Button states for the "Clear" 86 command 87boolean lastButtonStateClear = LOW; 88boolean currentButtonStateClear 89 = LOW; 90 91// DEBOUNCE VARIABLES TO MEASURE THE DEBOUNCING TIME OF A BUTTON PUSH. 92// 93 MAKE THESE "UNSIGNED LONG" VARIABLES AS THE NUMERICAL VALUE WILL HAVE AN EXTENDED 94 SIZE. 95unsigned long lastDebounceTime = 0; // This variable takes a "snapshot" 96 of time when any button is pushed. 97unsigned long debounceDelay = 50; // 98 Delay time in milliseconds; the amount of time the button is pressed must be higher 99 than the delay to register a push. 100 101 102// OBJECT DECLARATION 103LiquidCrystal_I2C 104 lcd(0x27, 16, 2); // Activates the LCD "object" 105 106 107 108// SETUP 109void 110 setup() { 111 112 lcd.backlight(); // THESE COMMANDS TURN ON AND 113 CLEAR THE LCD SCREEN 114 lcd.init(); 115 lcd.clear(); 116 117 pinMode(leftButtonPin, 118 INPUT); // SETS THE leftButtonPin AS AN INPUT 119 pinMode(rightButtonPin, INPUT); 120 // SETS THE rightButtonPin AS AN INPUT 121 pinMode(upButtonPin, INPUT); // 122 SETS THE upButtonPin AS AN INPUT 123 pinMode(downButtonPin, INPUT); // SETS 124 THE downButtonPin AS AN INPUT 125 pinMode(enterButtonPin, INPUT); // SETS THE 126 enterButtonPin AS AN INPUT 127 pinMode(clearButtonPin, INPUT); // SETS THE clearButtonPin 128 AS AN INPUT 129 130 delay(1000); 131 132} 133 134 135 136// MAIN LOOP 137void 138 loop() { 139 140 int unitSetting[] = {setting1Counter, setting2Counter, setting3Counter, 141 setting4Counter, setting5Counter}; // This variable holds the individual counters 142 in one array 143 144 lcd.setCursor(0,0); // Menu displayed 145 on the LCD. 146 lcd.print(menuOption[directionPush]); // The menuOption 147 that is displayed is determined by the left or right push. 148 lcd.setCursor(0,1); 149 150 lcd.print(unitSetting[directionPush]); // The setting counter that 151 is displayed is determined by the left or right push. 152 lcd.setCursor(5,1); // 153 This cursor setting fixes the measurementType in one place, preventing offset 154 155 lcd.print(measurementType[directionPush]); // by the unitSetting. 156 157 // 158 The program at this point is waiting for a button press. 159 currentButtonStateLeft 160 = digitalRead(leftButtonPin); 161 currentButtonStateRight = digitalRead(rightButtonPin); 162 163 currentButtonStateUp = digitalRead(upButtonPin); 164 currentButtonStateDown 165 = digitalRead(downButtonPin); 166 currentButtonStateEnter = digitalRead(enterButtonPin); 167 168 currentButtonStateClear = digitalRead(clearButtonPin); 169 170 if (currentButtonStateLeft 171 != lastButtonStateLeft || currentButtonStateRight != lastButtonStateRight || 172 173 currentButtonStateUp != lastButtonStateUp || currentButtonStateDown != lastButtonStateDown 174 || currentButtonStateEnter != lastButtonStateEnter) 175 // If there is 176 a button push on any of the buttons, the following routine runs to check if it was 177 a valid press: 178 { 179 lastDebounceTime = millis(); // lastDebounceTime 180 is set equal to the running millis() function. 181 } 182 183 if ((millis() - 184 lastDebounceTime) > debounceDelay) 185 // If the lastDebounceTime (aka. the 186 "snapshot" time) minus the running millis() function is higher than the set debounce 187 delay, the following routine 188 // below runs and checks which button was pushed: 189 190 { 191 192 // The current state 193 for each button is set not equal to the pressed state and when it changes, the pressed 194 state becomes equal to the current state. 195 196 // LEFT BUTTON PRESS 197 198 if (currentButtonStateLeft != buttonStateLeft) // Left button scrolls 199 the menu options to the left. 200 { 201 buttonStateLeft = currentButtonStateLeft; 202 203 204 if (buttonStateLeft == LOW) // 205 Once the button is released, the push is registered and the code below runs. 206 207 { 208 directionPush--; 209 // Both the up and down press counts will be 210 reset to zero when the left button is pushed. 211 upPressCount = 0; 212 downPressCount 213 = 0; 214 } 215 216 if (directionPush < 0) // 217 If the user tries to scroll below the first menu option, 218 { // 219 the program will loop back to the last menu option. 220 directionPush = 4; 221 222 } 223 lcd.clear(); 224 } 225 226 // RIGHT BUTTON PRESS 227 228 if (currentButtonStateRight != buttonStateRight) // Right button scrolls 229 the menu options to the right. 230 { 231 buttonStateRight = currentButtonStateRight; 232 233 234 if (buttonStateRight == LOW) 235 236 { 237 directionPush++; 238 // Both the up and down press counts will be 239 reset to zero when the right button is pushed. 240 upPressCount = 0; 241 downPressCount 242 = 0; 243 } 244 245 if (directionPush > 4) // 246 If the user tries to scroll above the last menu option, 247 { // 248 the program will loop back to the first menu option. 249 directionPush = 250 0; 251 } 252 lcd.clear(); 253 } 254 255 // UP BUTTON PRESS 256 if 257 (currentButtonStateUp != buttonStateUp) // Up button scrolls the setting 258 upward. 259 { 260 buttonStateUp = currentButtonStateUp; 261 262 263 if (buttonStateUp == LOW && directionPush == 0) // The first 5 times in 264 which the "up" button is pushed, each push will add 1 increment to the setting. 265 266 { 267 upPressCount++; 268 269 downPressCount = 0; // The downPressCount is 270 reset to zero. 271 setting1Counter++; 272 273 if (upPressCount > 274 5) // If the "up" button is pushed more than 5 times 275 consecutively, the setting increment increases by 5 276 { // 277 with every "up" button push and resets back when the down, left or right button 278 is pushed. 279 setting1Counter = setting1Counter + 4; 280 } 281 282 283 if (setting1Counter > 999) // Sets the setting counter 284 limit to 999. The user cannot increase the counter beyond 999. 285 { 286 287 setting1Counter = 999; 288 } 289 } 290 291 if (buttonStateUp 292 == LOW && directionPush == 1) 293 { 294 295 upPressCount++; 296 downPressCount = 0; 297 298 setting2Counter++; 299 300 if (upPressCount > 5) 301 { 302 setting2Counter 303 = setting2Counter + 4; 304 } 305 306 if (setting2Counter > 999) // 307 Sets the setting counter limit to 999. The user cannot increase the counter beyond 308 999. 309 { 310 setting2Counter = 999; 311 } 312 } 313 314 315 if (buttonStateUp == LOW && directionPush == 2) 316 317 { 318 upPressCount++; 319 downPressCount = 0; 320 321 setting3Counter++; 322 323 if (upPressCount > 5) 324 { 325 setting3Counter 326 = setting3Counter + 4; 327 } 328 329 if (setting3Counter > 999) // 330 Sets the setting counter limit to 999. The user cannot increase the counter beyond 331 999. 332 { 333 setting3Counter = 999; 334 } 335 } 336 337 338 if (buttonStateUp == LOW && directionPush == 3) 339 340 { 341 upPressCount++; 342 downPressCount = 0; 343 344 setting4Counter++; 345 346 if (upPressCount > 5) 347 { 348 setting4Counter 349 = setting4Counter + 4; 350 } 351 352 if (setting4Counter > 999) // 353 Sets the setting counter limit to 999. The user cannot increase the counter beyond 354 999. 355 { 356 setting4Counter = 999; 357 } 358 } 359 360 361 if (buttonStateUp == LOW && directionPush == 4) 362 363 { 364 upPressCount++; 365 downPressCount = 0; 366 367 setting5Counter++; 368 369 if (upPressCount > 5) 370 { 371 setting5Counter 372 = setting5Counter + 4; 373 } 374 375 if (setting5Counter > 999) // 376 Sets the setting counter limit to 999. The user cannot increase the counter beyond 377 999. 378 { 379 setting5Counter = 999; 380 } 381 } 382 383 lcd.clear(); 384 } 385 386 387 // DOWN BUTTON PRESS 388 if (currentButtonStateDown 389 != buttonStateDown) // Down button scrolls the setting downward. 390 { 391 392 buttonStateDown = currentButtonStateDown; 393 394 if 395 (buttonStateDown == LOW && directionPush == 0) // The first 5 times in which the 396 "down" button is pushed, each push will subtract 1 increment to the setting. 397 398 { 399 downPressCount++; 400 upPressCount = 401 0; // The upPressCount is reset to zero. 402 setting1Counter--; 403 404 405 if (downPressCount > 5) // If the "down" button 406 is pushed more than 5 times consecutively, the setting increment decreases by 5 407 408 { // with every "down" 409 button push and resets back when the up, left or right button is pushed. 410 setting1Counter 411 = setting1Counter - 4; 412 } 413 414 if (setting1Counter < -999) // 415 Sets the setting counter limit to -999. The user cannot increase the counter beyond 416 -999. 417 { 418 setting1Counter = -999; 419 } 420 } 421 422 423 if (buttonStateDown == LOW && directionPush == 1) 424 425 { 426 downPressCount++; 427 upPressCount = 0; 428 429 setting2Counter--; 430 431 if (downPressCount > 5) 432 { 433 434 setting2Counter = setting2Counter - 4; 435 } 436 437 if (setting2Counter 438 < -999) // Sets the setting counter limit to -999. The user 439 cannot decrease the counter beyond -999. 440 { 441 setting2Counter 442 = -999; 443 } 444 } 445 446 if (buttonStateDown == LOW && directionPush 447 == 2) 448 { 449 downPressCount++; 450 upPressCount 451 = 0; 452 setting3Counter--; 453 454 455 if (downPressCount > 5) 456 { 457 setting3Counter = setting3Counter 458 - 4; 459 } 460 461 if (setting3Counter < 0) // This code 462 prevents the user from entering 463 { // a number 464 below "0". 465 setting3Counter = 0; // Remove this code if you want 466 to allow in 467 } // negative numbers on a setting. 468 469 } 470 471 if (buttonStateDown == LOW && directionPush == 3) 472 473 { 474 downPressCount++; 475 476 upPressCount = 0; 477 setting4Counter--; 478 479 480 if (downPressCount > 5) 481 { 482 setting4Counter = setting4Counter 483 - 4; 484 } 485 486 if (setting4Counter < 0) // This code 487 prevents the user from entering 488 { // a number 489 below "0". 490 setting4Counter = 0; // Remove this code if you want 491 to allow in 492 } // negative numbers on a setting. 493 494 } 495 496 if (buttonStateDown == LOW && directionPush == 4) 497 498 { 499 downPressCount++; 500 upPressCount 501 = 0; 502 setting5Counter--; 503 504 if 505 (downPressCount > 5) 506 { 507 setting5Counter = setting5Counter - 508 4; 509 } 510 511 if (setting5Counter < 0) // This code prevents 512 the user from entering 513 { // a number below 514 "0". 515 setting5Counter = 0; // Remove this code if you want to 516 allow in 517 } // negative numbers on a setting. 518 519 } 520 521 lcd.clear(); 522 } 523 524 } 525 526 527 // 528 ENTER BUTTON PRESS 529 if (currentButtonStateEnter != buttonStateEnter) 530 531 { 532 buttonStateEnter = currentButtonStateEnter; 533 534 535 if (buttonStateEnter == LOW && directionPush == 0) // The Enter button 536 simply enters the setting and flashes a brief message. 537 538 { // Please feel free 539 to expand on this code to add more functions. 540 lcd.clear(); 541 lcd.setCursor(0,0); 542 543 lcd.print("DEGREES FAHREN."); 544 lcd.setCursor(0,1); 545 lcd.print("IS 546 ENTERED"); 547 delay(2000); 548 } 549 550 if (buttonStateEnter 551 == LOW && directionPush == 1) 552 { 553 554 lcd.clear(); 555 lcd.setCursor(0,0); 556 lcd.print("DEGREES CELSIUS"); 557 558 lcd.setCursor(0,1); 559 lcd.print("IS ENTERED"); 560 delay(2000); 561 562 } 563 564 if (buttonStateEnter == LOW && directionPush == 2) 565 566 { 567 lcd.clear(); 568 569 lcd.setCursor(0,0); 570 lcd.print("WEIGHT IN LBS."); 571 lcd.setCursor(0,1); 572 573 lcd.print("IS ENTERED"); 574 delay(2000); 575 } 576 577 if 578 (buttonStateEnter == LOW && directionPush == 3) 579 { 580 581 lcd.clear(); 582 583 lcd.setCursor(0,0); 584 lcd.print("INCHES"); 585 lcd.setCursor(0,1); 586 587 lcd.print("IS ENTERED"); 588 delay(2000); 589 } 590 591 if 592 (buttonStateEnter == LOW && directionPush == 4) 593 { 594 595 lcd.clear(); 596 597 lcd.setCursor(0,0); 598 lcd.print("MILES PER HOUR"); 599 lcd.setCursor(0,1); 600 601 lcd.print("IS ENTERED"); 602 delay(2000); 603 } 604 lcd.clear(); 605 606 } 607 608 609 // CLEAR BUTTON PRESS 610 if (currentButtonStateClear != 611 buttonStateClear) 612 { 613 buttonStateClear = currentButtonStateClear; 614 615 616 if (buttonStateClear == LOW && directionPush == 617 0) // The Clear button clears all setting data depending on what menu option 618 you are viewing. 619 { // 620 It flahses a brief message stating that the data has been cleared. 621 lcd.clear(); 622 // The press counts for both the up and 623 down variables are also reset to zero. 624 lcd.setCursor(0,0); 625 lcd.print("DEGREES 626 FAHREN."); 627 lcd.setCursor(0,1); 628 lcd.print("IS CLEARED"); 629 630 setting1Counter = 0; 631 downPressCount = 0; 632 upPressCount = 633 0; 634 delay(2000); 635 } 636 637 if (buttonStateClear == LOW 638 && directionPush == 1) 639 { 640 641 lcd.clear(); 642 lcd.setCursor(0,0); 643 lcd.print("DEGREES CELSIUS"); 644 645 lcd.setCursor(0,1); 646 lcd.print("IS CLEARED"); 647 setting2Counter 648 = 0; 649 downPressCount = 0; 650 upPressCount = 0; 651 delay(2000); 652 653 } 654 655 if (buttonStateClear == LOW && directionPush == 2) 656 657 { 658 lcd.clear(); 659 660 lcd.setCursor(0,0); 661 lcd.print("WEIGHT IN LBS."); 662 lcd.setCursor(0,1); 663 664 lcd.print("IS CLEARED"); 665 setting3Counter = 0; 666 downPressCount 667 = 0; 668 upPressCount = 0; 669 delay(2000); 670 } 671 672 if 673 (buttonStateClear == LOW && directionPush == 3) 674 { 675 676 lcd.clear(); 677 678 lcd.setCursor(0,0); 679 lcd.print("INCHES"); 680 lcd.setCursor(0,1); 681 682 lcd.print("IS CLEARED"); 683 setting4Counter = 0; 684 downPressCount 685 = 0; 686 upPressCount = 0; 687 delay(2000); 688 } 689 690 if 691 (buttonStateClear == LOW && directionPush == 4) 692 { 693 694 lcd.clear(); 695 696 lcd.setCursor(0,0); 697 lcd.print("MILES PER HOUR"); 698 lcd.setCursor(0,1); 699 700 lcd.print("IS CLEARED"); 701 setting5Counter = 0; 702 downPressCount 703 = 0; 704 upPressCount = 0; 705 delay(2000); 706 } 707 lcd.clear(); 708 709 } 710 711 712 713 // After a button is pushed and the count recorded, 714 all the states reset back to LOW for the data to be processed correctly. 715 lastButtonStateLeft 716 = currentButtonStateLeft; // resets the left button state to LOW 717 lastButtonStateRight 718 = currentButtonStateRight; // resets the right button state to LOW 719 lastButtonStateUp 720 = currentButtonStateUp; // resets the up button state to LOW 721 lastButtonStateDown 722 = currentButtonStateDown; // resets the down button state to LOW 723 lastButtonStateEnter 724 = currentButtonStateEnter; // resets the enter button state to LOW 725 lastButtonStateClear 726 = currentButtonStateClear; // resets the clear button state to LOW 727 728} 729
Downloadable files
Schematic for project setup
Schematic for project setup
Comments
Only logged in users can leave comments
danhostler1985
0 Followers
•0 Projects
Table of contents
Intro
5
0