0

I am trying to create Menu Selection process.if SELECT button pressed once should display the Display parameter and if select button pressed twice should goes to Set_parameter window(Where we set parameter). once it is display parameter using up and down arrow Choose the Display window .But problem i am facing here is It enter the Display parameter but comes out Menu window directly.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Pin configuration of the buttons
#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
int button_counter=0;
void setup()
{
    Serial.begin(9600);
}
void loop()
{
    Select_Menu();
}
void Select_Menu()
{
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("MENU");
 int button=read_LCD_buttons();
 if(button==btnSELECT)
 {
    button_counter=button_counter+1
 }
 if(button_counter==1)
 {
     Display_function();
 }else if((button_counter==2))
 {
    Set_function();
 }else
 {
    button_counter=0;
 }
}

void Display_function()
{
    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()
{
 lcd.setCursor(0,0);
 lcd.print("WS kmph:");
 lcd.print(Wind_Kmph);
 lcd.setCursor(0,1);  
 lcd.print("WS m/s:");
 lcd.print(Wind_Speed);
}
void button_loop()
{
 lcd.begin(16,2);
 lcd.setCursor(0,0);
 lcd.print("Welcome");
}
void Display_coordinate()
{
 lcd.begin(16,2);
 lcd.setCursor(0,0);
 lcd.print("Latitude:");
 lcd.print(latitude);
 lcd.setCursor(0,1);  
 lcd.print("Longitude:");
 lcd.print(longitude);
}
void  Display_angle()
{
 lcd.begin(16,2);
 lcd.setCursor(0,0);
 lcd.print("DESIRED: ");
 lcd.print(tracker_des_angle,DEC);
 lcd.setCursor(0,1);
 lcd.print("ACTUAL: ");
 lcd.print(tracker_actual_pos,DEC);

}
void digitalClockDisplay()
{
 lcd.begin(16,2);
 lcd.setCursor(0,0);
 lcd.print("Date:");
 lcd.print(local_day);
 lcd.print("/");
 lcd.print(local_month);
 lcd.print("/");
 lcd.print(local_year);
 lcd.setCursor(0,1);
 lcd.print("Time:");
 lcd.print(local_h);
 lcd.print(":");
 lcd.print(local_m);
 lcd.print(":");
 lcd.print(local_s);
 lcd.print("   ");
}
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...

}
2
Contributors
1
Reply
6
Views
3 Years
Discussion Span
Last Post by Banfa
0

This code appears to lack design additionally it appears to have C++ elements but you have posted it into the C forum.

If I was going to pick on 1 thing then I do not think the variable button_counter is doing you any favours. Counting the button presses is not a good way to run a menu. The correct way to run a menu is to kepp track of what menu item is on display and know what the action of every button press on the current menu should be. Included in this is not needing to know what the previous button press is.

For example If the Angle is displayed
* Up - takes you to co-ordinate
* Down - takes you to time
* Select - Takes you to the angle set menu (if you can set the angle)
* Left - dones nothing (or takes you up one menu level if that makes sense)
* Right - does nothing (or the same as select)

The point is that a menu is stateful on what is currently displayed, not what button presses have gone before.

Work out your menu heirarchy and how the buttons cause transitions between the menus, draw a menu state diagram. The program it.

A second thing read_lcd_buttons, is not as useful as it could be. It should return the current button that is depressed but it would also be useful if it returned how long it has been depressed or if the button has just been depressed. If you think of button presses as events then between 2 button press events the button should be released. If you want to have repeat on hold then make that a different event or provide a press count with the event so that you can react differently to button just pressed and button pressed and held. However all of this should be handled by your function to read the buttons, the rest of the code does not want to be trying to work out what is happening with the buttons, it just wants nice simple button events to respond to.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.