Thread: LCD Increment and Decrement For Display

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    11

    Question LCD Increment and Decrement For Display

    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.

    LCD & Keypad Shield Quickstart Guide | Freetronics

    Code:
    #include <LiquidCrystal.h>
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    static int button_counter=0;
    #define btnRIGHT  0        // Okay
    #define btnUP     1        // inc
    #define btnDOWN   2        // dec
    #define btnLEFT   3        // Select
    #define btnSELECT 4        // Menu
    #define btnNONE   5
    #define beeper A1      // Alarm buzzer
    #define shortBeep 100
    #define longBeep  500
    
    void setup()
    {
    Serial.begin(9600);
    }
    void loop()
    {
    lcd_call();
    }
    
     void lcd_call()
    {
    
     int button = read_LCD_buttons();  
    
    
    
    if(button==btnUP)
    {
     button_counter=button_counter+1; 
     Serial.print("button_counter");
     Serial.println(button_counter);
    }else if(button==btnDOWN)
    {
      button_counter=button_counter-1; 
       Serial.println(button_counter);
    }
    
    if (button_counter>5)
    {
      button_counter=1;
    }else
    
    while(button_counter<5)
    {
       int button = read_LCD_buttons();  
      if(button != prev_button && button !=  btnNONE)
    {
      prev_button = button; 
    //timedBeep(shortBeep,2); 
    }
    
    if ((((button ==btnUP )||(button_counter==1))||((button ==btnDOWN )))&&(prev_button==btnUP))
    {
    digitalClockDisplay();//timedBeep(200,1); 
    
    }else if((((button ==btnUP )||(button_counter==2))||((button ==btnDOWN )))&&(prev_button==btnUP))
    {
     Display_angle();//timedBeep(200,1); 
    }else if((((button ==btnUP )||(button_counter==3))||((button ==btnDOWN )))&&(prev_button==btnUP))
    {
    Display_coordinate();//timedBeep(200,1); 
    }else if((((button ==btnUP )||(button_counter==4))||((button ==btnDOWN )))&&(prev_button==btnUP))
    {
      button_loop();//timedBeep(500,4); 
    }else if((((button ==btnUP )||(button_counter==5))||((button ==btnDOWN )))&&(prev_button==btnUP))
    {
       Display_Wind();//timedBeep(200,1); 
    }
      
    }
    }
    
    
    void Display_Wind()
    {
    ....Display Wind perameter........
    }
    void button_loop()
    {
    .....Button loop selection.........
    }
    void Display_coordinate()
    {
    .....Display coordinate.........
    }
    void  Display_angle()
    {
    ......Display angle........
    }
    void digitalClockDisplay()
    {
    .......Display Date and time.........
    }
    
    int read_LCD_buttons()
    {
      adc_key_in = analogRead(0);      // read the value from the sensor
      // my buttons when read are centered at these valies: 0, 131, 307, 481, 722
      // we add approx 50 to those values and check to see if we are close
      // No button pressed should be 1023
      if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
      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;  // when all others fail, return this...
    
    }
    
    
    
    
    
    
    
    
                                 http://forum.arduino.cc/Themes/ardui...s/doc_edit.png                                                                                                                         

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    ok, do it.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    i shall assume you asked how to scroll though different display messages depending on buttons pressed

    LCD & Keypad Shield Quickstart Guide | Freetronics

    hope you find this usefull

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First, you should be making an effort to post well formatted code. This means consistent indentation, commenting out comments in the code (e.g. "....Display Wind perameter........"), and making sure you only paste the code and not links and strange characters (forum tags within code seem to inhibit the syntax highlighting).

    As posted, your code is extremely difficult to take in visually. Put in the effort to make it more readable if you want better help. This time only, I'll fix it for you so you can see how it should look:

    Code:
    #include <LiquidCrystal.h>
    
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    
    static int button_counter=0;
    
    #define btnRIGHT  0      // Okay
    #define btnUP     1      // inc
    #define btnDOWN   2      // dec
    #define btnLEFT   3      // Select
    #define btnSELECT 4      // Menu
    #define btnNONE   5
    #define beeper A1        // Alarm buzzer
    #define shortBeep 100
    #define longBeep  500
    
    
    void setup()
    {
        Serial.begin(9600);
    }
    
    
    void loop()
    {
        lcd_call();
    }
    
    
    void lcd_call()
    {
        int button = read_LCD_buttons();  
    
        if(button==btnUP)
        {
            button_counter=button_counter+1; 
            Serial.print("button_counter");
            Serial.println(button_counter);
        }
        else if(button==btnDOWN)
        {
            button_counter=button_counter-1; 
            Serial.println(button_counter);
        }
    
        if (button_counter>5)
        {
            button_counter=1;
        }
        else
            while(button_counter<5)
            {
                int button = read_LCD_buttons();  
                if(button != prev_button && button !=  btnNONE)
                {
                    prev_button = button; 
                    //timedBeep(shortBeep,2); 
                }
    
                if ((((button ==btnUP )||(button_counter==1))||((button ==btnDOWN )))&&(prev_button==btnUP))
                {
                    digitalClockDisplay();//timedBeep(200,1); 
                }
                else if((((button ==btnUP )||(button_counter==2))||((button ==btnDOWN )))&&(prev_button==btnUP))
                {
                    Display_angle();//timedBeep(200,1); 
                }
                else if((((button ==btnUP )||(button_counter==3))||((button ==btnDOWN )))&&(prev_button==btnUP))
                {
                    Display_coordinate();//timedBeep(200,1); 
                }
                else if((((button ==btnUP )||(button_counter==4))||((button ==btnDOWN )))&&(prev_button==btnUP))
                {
                    button_loop();//timedBeep(500,4); 
                }
                else if((((button ==btnUP )||(button_counter==5))||((button ==btnDOWN )))&&(prev_button==btnUP))
                {
                    Display_Wind();//timedBeep(200,1); 
                }
            }
    }
    
    
    void Display_Wind()
    {
        //....Display Wind perameter........
    }
    
    
    void button_loop()
    {
        //.....Button loop selection.........
    }
    
    
    void Display_coordinate()
    {
        //.....Display coordinate.........
    }
    
    
    void  Display_angle()
    {
        //......Display angle........
    }
    
    
    void digitalClockDisplay()
    {
        //.......Display Date and time.........
    }
    
    
    int read_LCD_buttons()
    {
        adc_key_in = analogRead(0);      // read the value from the sensor
        // my buttons when read are centered at these valies: 0, 131, 307, 481, 722
        // we add approx 50 to those values and check to see if we are close
        // No button pressed should be 1023
        if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
        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;  // when all others fail, return this...
    
    }


    Second, it's considered improper to cross-post your question to different forums at the same time. Someone on one forum might spend their time answering your question, without realizing that it might have been already answered elsewhere. This wastes the time and effort of that person, as well as keeping them from helping someone else.



    Finally, you should be more clear about your problem. Be more specific when you describe what you're trying to accomplish. Since you sort of told us what you need to do, and provided code you wrote to attempt it, you should also describe exactly what the problem is you're seeing. This is especially important for device-specific code, where most people here do not have the hardware at their disposal.

    I've never worked with the Arduino before, but I do have some comments about your code.

    I see a few variables being used that have not been declared ("prev_button", "adc_key_in").

    In the "lcd_call()" function, you increment "button_counter" when UP is pressed, and decrement it when DOWN is pressed. Then you check if it's greater than five - if so, you reset it to one. But what if it is less than one? You don't seem to take that case into account.

    The logic says "if the counter is greater then five, set it to one ... else run your checking loop to determine the display". Why is this an if/else relationship? Shouldn't the checking loop be run regardless? Technically, this seems to happen anyway, since that function is called in a loop of its own. But you should strive for clear logic.

    It seems the biggest problem is that your "display update" logic is terribly flawed. Why do you have this logic in a loop? And more importantly, why does it loop "while button_counter is less than five"? You do not change the value of "button_counter" in this loop, so unless there's another mechanism I'm unaware of (e.g. interrupts), the program execution will be stuck there. Sure you check for a button press within the loop, but the counter value will never change.

    It seems like the code for the "lcd_call()" function was banged out with little thought. You need to figure out the logic, on paper, before writing it in code. A basic flowchart will help you see the logic you need to implement. Break the problem down into smaller pieces, and tackle them one at a time. Take it step by step. Make sure you understand the flow of logic, and the decisions your logic needs to make. If necessary, translate the flowchart into pseudo-code. Only then should you begin writing code. Start small - for instance, write code to print specific messages when certain buttons are pressed so you know that part is working correctly. Then add some more code to solve the next small problem, and test that. Gradually build your program, testing along the way.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    11
    LCD Increment and Decrement For Display-button-page-001-jpg

    logic I WANTED TO IMPLIMENT AS ABOVE

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That attachment is impossible to read - the smallish text has been compressed beyond any reasonable interpretation.

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    11

    Question

    I have modified the Code . Now Display working fine . But i could not able to enter into set peramter window from Display or while Selecting menu/right button


    Code:
    
    void lcd_Menu()
    {
     int button = 0; 
     menuOption = 1;
     lcdClear();
     lcd.print("MENU DISPLAY:");
     while (menuOption <= menuOptions)
     {
      button = read_LCD_buttons();
     if (button == btnRIGHT)
     {
      menuOption++;
     if (menuOption == 2)
     {
      lcdClear();
      lcd.print("DISPLAY:");  
      button = read_LCD_buttons();
      while((button = read_LCD_buttons()) != btnLEFT)
       {
        Serial.println(button);
        DS_Counter=Display_selection();
        switch(DS_Counter)
        {
          case 1:lcd.setCursor(0,0);digitalClockDisplay();timedBeep(shortBeep,1);break;
          case 2:  Display_angle(); lcd.setCursor(0,0);timedBeep(shortBeep,1);break;       
          case 3: Display_coordinate();lcd.setCursor(0,0);timedBeep(shortBeep,1);break; 
          case 4: Display_Wind();lcd.setCursor(0,0);timedBeep(shortBeep,1);break;      
          case 5:break;        
         }
                                  
        }
        menuOption++;
       }
     if (menuOption == 3)
     {
      int button1;
      lcdClear();
      lcd.print("SET PARAMETER");
      button1 = read_LCD_buttons();
      while(Set_Counter<5)
      {
       Serial.println(button1);
       Set_Counter=Select_section();
       switch(Set_Counter)
       {
        case 1: setDateTime();break;          
        case 2:  set_latitude();break;        
        case 3: set_longitude() ; break;     
        case 4: set_trackertime(); break;  
        case 5:break;
       }  
      }menuOption++;
               
     }
     if(menuOption == 4)
      {
       lcdClear();
       lcd.print("EXIT WINDOW");
      }   
      }
     }
    }

  8. #8
    Registered User
    Join Date
    Jan 2014
    Posts
    11
    Ubuntu Pastebin

    The code is attached above

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Have you verified that read_LCD_buttons() works correctly?
    If not, do so.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    11
    Yes i have already checked . these button working properly. I think there problem in exiting loop.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    menuOption++; should NOT be in the while loop!
    Your indentation is so bad I have no idea if it is or is not.

    I have doubts that you need all the menuOption++ in your code; I suggest learning to indent your code better.

    Tim S.
    Last edited by stahta01; 02-27-2014 at 10:39 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Have you tried doing debouncing code?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    Have you tried doing debouncing code?

    Tim S.
    If debouncing code not needed have you tried doing code to stop till key is released.

    Your description of your problem is lacking; I know it does NOT do X; but, I have no idea of what is does do.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    Registered User
    Join Date
    Jan 2014
    Posts
    11
    This what i wanna do
    '
    Code:
    MENU:
    button=read_button();
    MENU OPTION=4;
    MENU_COUNTER=1
    if (button==RIGHT)
    {
    INCREMENT    MENU_COUNTER;
    }
    CHECK (MENU_COUNTER<MENU OPTION)
    
    IF MENU_COUNTER ==1
         CALL DISPLAY ();
    IF MENU_COUNTER==2
     CALL SET_FUNCTION();
    
    ELSE
       EXIT MENU DISPLAY & DISPLAY DATE AND TIME;MENU_COUNTER=1;
    
    CALL DISPLAY()
            LABEL1:BUTTON=read_button();
           IF( BUTTON==UP)
                INCREMENT   DISPLAY_COUNTER;
           IF( BUTTON==DOWN)
               DECREMENT   DISPLAY_COUNTER; 
           IF(BUTTON==LEFT)
                EXIT TO MENU
           IF (BUTTON==RIGHT)
               EXIT TO SET PERAMETER
        SWITCH( DISPLAY_COUNTER)
                        1: DATE AND TIME DISPLAY
                        2: COORDINATE
                        3:WINDSPEED
                        4:DESIRED ANGLE
                        5:EXIT FROM SWITCH FUNCTION
                       GOTO LABEL1:
             
    
    CALL SET_FUNCTION();
           LABEL2:BUTTON=read_button();
          F( BUTTON==UP)
                INCREMENT   SET_COUNTER;
           IF( BUTTON==DOWN)
               DECREMENT   SET_COUNTER; 
           IF(BUTTON==LEFT)
                EXIT TO MENU
           SWITCH( DISPLAY_COUNTER)
                        1: SET DATE AND TIME DISPLAY;   INCREMENT   SET_COUNTER;
                        2: SET COORDINATE;   INCREMENT   SET_COUNTER;
                        3:SET START TIME & STOP TIME;   INCREMENT   SET_COUNTER;
                        4:EXIT FROM SWITCH FUNCTION
                       GOTO LABE2:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment and Decrement
    By Johnathan1707 in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 02:20 PM
  2. newb need help with increment and decrement
    By cgurl05 in forum C Programming
    Replies: 2
    Last Post: 03-05-2006, 04:50 PM
  3. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM
  4. increment/decrement operators
    By ZakkWylde969 in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2003, 04:17 PM
  5. Increment/Decrement for loop?
    By --]bthNzA in forum C Programming
    Replies: 11
    Last Post: 12-29-2001, 01:10 AM

Tags for this Thread