I have simple code Here. I am calling two function display lcd and check key. by default screen should display date and time. if up arrow pressed go to particular window and show date and time. similarly down key pressed. display menu should decremented.
Problem:Date and time Displaying properly . once up key pressed menu being changed, But as soon as enter menu directly jumping back to date and time.i wanted to stay in menu for 10s then jump to date and display window. Every time before jump to date and time LCD screen screen getting circled with black
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
float latitude=13.0;//latitude in degrees
float longitude=60.0;
float Wind_Speed=13.0;
float Wind_Kmph=78.0;
float desire=45.0;
float actual=43.0;
static int dd;
static int mm;
static int yy;
static int s;
static int h;
static int m;
static int wkDay;
int lastDay = 0;
int lastMonth = 0;
int lastYear = 0;
int lastHour = 0;
int lastMinute = 0;
int lastSecond=0;
//Local time date variables
static int local_day;
static int local_month;
static int local_year;
int local_s;
static int local_h;
static int local_m;
typedef enum {
DATE_TIME,
COORDINATE,
WINDSPEED,
POSITIONTRACK,
DISPAY_PARAMETER,
SET_PARAMETER,
DISPLAY_TYPE_MAX
}
DISPLAY_SCREEN_TYPE;
DISPLAY_SCREEN_TYPE display_screen_type=DATE_TIME;
#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
#define beep 300
int adc_key_in = 0;
int Update_Display=1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Display_LCD();
Chk_key();
}
void Display_LCD()
{
switch(display_screen_type)
{
// lcdClear();
case DATE_TIME:
timedBeep(shortBeep,1);
digitalClockDisplay();
break;
case COORDINATE:
timedBeep(shortBeep,1);
Display_coordinate();
break;
case WINDSPEED:
timedBeep(shortBeep,1);
Display_Wind();
break;
case POSITIONTRACK:
timedBeep(shortBeep,1);
Display_angle();
break;
case DISPAY_PARAMETER:
timedBeep(shortBeep,1);
Display_fun();
break;//display_screen_type=SET_PARAMETER;Update_Display=1;break;
case SET_PARAMETER:
timedBeep(shortBeep,1);
Set_Fun();
break;//display_screen_type=DATE_TIME;Update_Display=1;break;
case DISPLAY_TYPE_MAX:
timedBeep(shortBeep,1);
break;
default:
digitalClockDisplay();
break;
}
if(display_screen_type>DISPLAY_TYPE_MAX)
{
display_screen_type=DATE_TIME;
}
}
void Chk_key()
{
int button=read_LCD_buttons();
switch(button)
{
case btnRIGHT:// RIGHT/MENU BUTTON BEING SELECTED
Action_RIGHT();
break;
case btnUP:// UP KEY/INCREMENT PRESSED
Action_UP();
break;
case btnDOWN: // DOWN / DECREMENT KEY PRESSED
Action_DOWN();
break;
case btnLEFT://LEFT / ESCAPE KEY PRESSED
Action_LEFT();
break;
case btnSELECT://SELECT KEY PRESSED
Action_SELECT();
break;
}
}
//...........................................................Action to be taken......................................................
void Action_UP()
{
switch(display_screen_type)
{
case DATE_TIME:
display_screen_type= COORDINATE;
delay(100);
Update_Display=1;
break;
case COORDINATE:
display_screen_type= WINDSPEED;
delay(100);
Update_Display=1;
break;
case WINDSPEED:
display_screen_type= POSITIONTRACK;
delay(100);
Update_Display=1;
break;
case POSITIONTRACK:
display_screen_type= DATE_TIME;
delay(100);
Update_Display=1;
break;
// default:display_screen_type= DATE_TIME; Update_Display=1;delay(500);break;
}
}
void Action_DOWN()
{
/* lcdClear();
lcd.print("DOWN");*/
switch(display_screen_type)
{
case DATE_TIME:
display_screen_type= POSITIONTRACK;
delay(100);
Update_Display=1;
break;
case COORDINATE:
display_screen_type= DATE_TIME;
delay(100);
Update_Display=1;
break;
case WINDSPEED:
display_screen_type= COORDINATE;
delay(100);
Update_Display=1;
break;
case POSITIONTRACK:
display_screen_type= WINDSPEED;
delay(100);
Update_Display=1;
break;
//default:display_screen_type= DATE_TIME; Update_Display=1;break;
}
}
void Action_LEFT()
{
lcdClear();
lcd.print("LEFT");
}
void Action_SELECT()
{
lcdClear();
lcd.print("SELECT");
}
void Action_RIGHT()
{
lcdClear();
lcd.print("RIGHT");
}
void timedBeep(int beepTime, int beepCount)
{
for (int i = 0; i < beepCount; i ++)
{
digitalWrite(beeper, HIGH);
delay(beepTime);
digitalWrite(beeper, LOW);
delay(beepTime);
}
}
void lcdClear(){
lcd.clear();
lcd.begin(16,2);
lcd.setCursor(0,0);
}
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...
}
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_Wind()
{
lcd.begin(16,2);
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 Display_angle()
{
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("DESIRED: ");
lcd.print(desire,DEC);
lcd.setCursor(0,1);
lcd.print("ACTUAL: ");
lcd.print(actual,DEC);
}
void digitalClockDisplay()
{
// Serial.print(local_s);
//lcd.begin(16,2);
int prv_flag=0;
int current_flag=1;
if( current_flag!=prv_flag)
{
lcd.setCursor(0,0);
lcd.print("Date:");
lcd.print(local_day);
lcd.print("/");
lcd.print(local_month);
lcd.print("/");
lcd.print(local_year);
lcd.print(" ");
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(" ");
lastDay = local_day;
lastMonth = local_month;
lastYear =local_year;
lastHour = local_h;
lastMinute = local_m;
lastSecond=local_s;
prv_flag=1;
}
else
{
lcd.setCursor(0,0);
lcd.print("Date:");
lcd.print(lastDay);
lcd.print("/");
lcd.print(lastMonth);
lcd.print("/");
lcd.print(lastYear);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Time:");
lcd.print(lastHour);
lcd.print(":");
lcd.print(lastMinute);
lcd.print(":");
lcd.print(lastSecond);
lcd.print(" ");
delay(1000);
}
}
void Display_fun()
{
lcdClear();
lcd.print("DISPAY PARA:");
}
void Set_Fun()
{
lcdClear();
lcd.print("SET PARAMETER:");
}