LCD Increment and Decrement For Display

Discussion in 'Programmer's Corner' started by ajitnayak, Feb 25, 2014.

  1. ajitnayak

    Thread Starter Member

    Feb 22, 2013
    36
    0
    Dear all.
    I am trying to call Display menu. If up key is pressed Displayed has to be incremented and stay in particular window if Decremented, go to previous Display function and show previous Display function.
    I am using Arduino UNO board and below circuit Setup for reading key



    http://www.freetronics.com/pages/16x2-lcd-shield-quickstart-guide#.Uv9jb87pegI

    Code ( (Unknown Language)):
    1.  
    2.  
    3.  
    4. #include <LiquidCrystal.h>
    5. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    6. static int button_counter=0;
    7. #define btnRIGHT  0        // Okay
    8. #define btnUP     1        // inc
    9. #define btnDOWN   2        // dec
    10. #define btnLEFT   3        // Select
    11. #define btnSELECT 4        // Menu
    12. #define btnNONE   5
    13. #define beeper A1      // Alarm buzzer
    14. #define shortBeep 100
    15. #define longBeep  500
    16.  
    17. void setup()
    18. {
    19. Serial.begin(9600);
    20. }
    21. void loop()
    22. {
    23. lcd_call();
    24. }
    25.  
    26.  void lcd_call()
    27. {
    28.  
    29.  int button = read_LCD_buttons();  
    30.  
    31.  
    32.  
    33. if(button==btnUP)
    34. {
    35.  button_counter=button_counter+1;
    36.  Serial.print("button_counter");
    37.  Serial.println(button_counter);
    38. }else if(button==btnDOWN)
    39. {
    40.   button_counter=button_counter-1;
    41.    Serial.println(button_counter);
    42. }
    43.  
    44. if (button_counter>5)
    45. {
    46.   button_counter=1;
    47. }else
    48.  
    49. while(button_counter<5)
    50. {
    51.    int button = read_LCD_buttons();  
    52.   if(button != prev_button && button !=  btnNONE)
    53. {
    54.   prev_button = button;
    55. //timedBeep(shortBeep,2);
    56. }
    57.  
    58. if ((((button ==btnUP )||(button_counter==1))||((button ==btnDOWN )))&&(prev_button==btnUP))
    59. {
    60. digitalClockDisplay();//timedBeep(200,1);
    61.  
    62. }else if((((button ==btnUP )||(button_counter==2))||((button ==btnDOWN )))&&(prev_button==btnUP))
    63. {
    64.  Display_angle();//timedBeep(200,1);
    65. }else if((((button ==btnUP )||(button_counter==3))||((button ==btnDOWN )))&&(prev_button==btnUP))
    66. {
    67. Display_coordinate();//timedBeep(200,1);
    68. }else if((((button ==btnUP )||(button_counter==4))||((button ==btnDOWN )))&&(prev_button==btnUP))
    69. {
    70.   button_loop();//timedBeep(500,4);
    71. }else if((((button ==btnUP )||(button_counter==5))||((button ==btnDOWN )))&&(prev_button==btnUP))
    72. {
    73.    Display_Wind();//timedBeep(200,1);
    74. }
    75.  
    76. }
    77. }
    78.  
    79.  
    80. void Display_Wind()
    81. {
    82. ....Display Wind perameter........
    83. }
    84. void button_loop()
    85. {
    86. .....Button loop selection.........
    87. }
    88. void Display_coordinate()
    89. {
    90. .....Display coordinate.........
    91. }
    92. void  Display_angle()
    93. {
    94. ......Display angle........
    95. }
    96. void digitalClockDisplay()
    97. {
    98. .......Display Date and time.........
    99. }
    100.  
    101. int read_LCD_buttons()
    102. {
    103.   adc_key_in = analogRead(0);      // read the value from the sensor
    104.   // my buttons when read are centered at these valies: 0, 131, 307, 481, 722
    105.   // we add approx 50 to those values and check to see if we are close
    106.   // No button pressed should be 1023
    107.   if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
    108.   if (adc_key_in < 50)   return btnRIGHT;
    109.   if (adc_key_in < 195)  return btnUP;
    110.   if (adc_key_in < 380)  return btnDOWN;
    111.   if (adc_key_in < 555)  return btnLEFT;
    112.   if (adc_key_in < 790)  return btnSELECT;  
    113.   return btnNONE;  // when all others fail, return this...
    114.  
    115. }
    116.  
    117.  
    118.  
    119.  
     
    Last edited: Feb 25, 2014
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Did you have a problem or a question you would like to discuss?
     
    spinnaker likes this.
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    That's some list of conditional statements there. Have you explored all your options to disprove something simpler would work like...

    Code ( (Unknown Language)):
    1. if((((button ==btnUP )||(button_counter==4))||((button ==btnDOWN )))&&(prev_button==btnUP))
    2. {
    3.     button_loop();//timedBeep(500,4);
    4. }
    5. else
    6. {
    7.     Display_Wind();//timedBeep(200,1);
    8. }
    9.  
     
Loading...