Advertisement
skizziks_53

mini LCD + button tester v.01

May 8th, 2019
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. /*
  2.   Reddit menu example - May 8, 2019
  3.   Displays a multi-level menu on a 20x4 LCD screen, with 5-button navigation.
  4.   Board = Uno
  5.   Other hardware: 20x4 I2C LCD
  6.                   5 series-resistive buttons
  7.  
  8.  
  9.   Button 1 should be left, button 2 down, button 3 up, button 4 right. 5 = select.
  10.   buttons, resistors, pin levels:
  11.   1   220Ω  394
  12.   2   470Ω  329
  13.   3   1KΩ   264
  14.   4   2.2KΩ   194
  15.   5   4.7KΩ   114
  16.  
  17. */
  18.  
  19. #include "Wire.h" // For I2C (standard Arduino IDE library)
  20. #include "LiquidCrystal_I2C.h" // Library location --- https://www.arduinolibraries.info/libraries/liquid-crystal-i2-c
  21. LiquidCrystal_I2C lcd(0x27, 20, 4); // 0x27 is -usually- the default I2C bus address of the backpack
  22.  
  23. bool use_serial_messages = true; // general prototyping messages (allows disabling them for speed reasons)
  24.  
  25. int buttonInput_pin = A0;
  26. int button_pressed = 0;
  27. const int button_pressed__left = 1;
  28. const int button_pressed__down = 2;
  29. const int button_pressed__up = 3;
  30. const int button_pressed__right = 4;
  31. const int button_pressed__select = 5;
  32.  
  33.  
  34. void setup() {
  35.   Serial.begin(9600);
  36.   lcd.init();
  37.   // buttonInput_pin is analogRead(), doesn't require declaration
  38.   // I2C pins A4, A5 are fixed and don't require declaration
  39.   lcd_clear_all_lines();
  40.   Serial.println("Exiting setup()");
  41. }
  42.  
  43. void loop() {
  44.  
  45.   lcd_clear_line1();
  46.   delay(500);
  47.   button_pressed = analogRead(buttonInput_pin);
  48.   if (button_pressed > 50 && button_pressed < 130) {
  49.     // select button = 114
  50.     lcd.setCursor(0, 0);
  51.     lcd.print("button = select");
  52.     Serial.println("button = select");
  53.   }
  54.   else if (button_pressed > 180 && button_pressed < 220) {
  55.     // right button = 194
  56.     lcd.setCursor(0, 0);
  57.     lcd.print("button = right");
  58.     Serial.println("button = right");
  59.   }
  60.   else if (button_pressed > 230 && button_pressed < 290) {
  61.     // up button = 264
  62.     lcd.setCursor(0, 0);
  63.     lcd.print("button = up");
  64.     Serial.println("button = up");
  65.   }
  66.   else if (button_pressed > 300 && button_pressed < 360) {
  67.     // down button = 329
  68.     lcd.setCursor(0, 0);
  69.     lcd.print("button = down");
  70.     Serial.println("button = down");
  71.   }
  72.   else if (button_pressed > 370 && button_pressed < 430) {
  73.     // left button = 394
  74.     lcd.setCursor(0, 0);
  75.     lcd.print("button = left");
  76.     Serial.println("button = left");
  77.   }
  78.   else {
  79.     // no button pressed.
  80.     lcd.setCursor(0, 1);
  81.     lcd.print("button = none");
  82.     Serial.println("button = none");
  83.   }
  84.   delay(2000);
  85.  
  86. } // end of main loop
  87.  
  88.  
  89.  
  90.  
  91. void lcd_clear_all_lines() {
  92.   lcd_clear_line1();
  93.   lcd_clear_line2();
  94.   lcd_clear_line3();
  95.   lcd_clear_line4();
  96. }
  97.  
  98. void lcd_clear_line1() {
  99.   lcd.setCursor(0, 0);
  100.   lcd.print("                    ");
  101. }
  102.  
  103. void lcd_clear_line2() {
  104.   lcd.setCursor(0, 1);
  105.   lcd.print("                    ");
  106. }
  107.  
  108. void lcd_clear_line3() {
  109.   lcd.setCursor(0, 2);
  110.   lcd.print("                    ");
  111. }
  112.  
  113. void lcd_clear_line4() {
  114.   lcd.setCursor(0, 3);
  115.   lcd.print("                    ");
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement