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...
}