Store status of previous key pressed

Dear all,

I need coding help on this. I have 6 keys up , down , right ,left, select, NONE.
Here i need small code which return the status of key being pressed and stay remain in same state.
Assume i pressed left key , it has to stay in left key stage unless another key pressed.
if none key is pressed means no key pressed it should stay in previous key being pressed

You really can't figure it out?

  • Make a new variable, previous_key.
  • Set previous_key to NULL or -1.
  • Read the key press.
  • If key press does not equal previous_key (which it won't, the first time), update previous_key.

I'm sure you can figure the rest out from here.

Is it right syntax

 void lcd_call()
{
int button=0;
int prev_button=0;
button = read_LCD_buttons();
if (button!=btnNONE)
{
  goto labe1;
}else
{
 prev_button= button;
}
label:serial.println("hello");

Is it right syntax

Nope.

void lcd_call()
{
int button=0; // Ok
int prev_button=0; // No, this will overwrite prev_button and it will not remember what the button previously was. Take it out and make it global.
button = read_LCD_buttons(); // Ok
if (button!=btnNONE) // add spaces for clarity, what does btnNONE equal, -1, 0, '/0' ?
{
goto labe1; //NO!!! Labels are not needed, especially not for something as simple as this.
}else // separate them on new lines
{
prev_button= button; // When do you compare button to prev_button?
}
label:serial.println("hello"); // Again, don't use labels.

This my problem.

I have 6 keys :
Left-> Display date
right->display wind
up-> display coordinate
down → display angle
select → Select MENU for user to set value.

Once left key pressed , It always show date and time with updation,
if right key is pressed . Display wind loads.

IN below code it displaying only date and time since i set default value as right i.e 0.

void lcd_call()
{
  int button;
  
int prev_button;
//button = read_LCD_buttons();
// prev_button=button;
label:button=prev_button;
 
  switch(button)
  {
    
  case 0:do{
             digitalClockDisplay();
              button = read_LCD_buttons();
              prev_button=button;
             }while(button!=btnNONE);
             //break;
             
          goto label;
       
  case 1:   do{
            Display_Wind();
              button = read_LCD_buttons();prev_button=button;
             }while(button!=btnNONE);//break;
              
          goto label;
 case 2:do{
              Display_angle();
              button = read_LCD_buttons(); prev_button=button;
             }while(button!=btnNONE);//break;
           //prev_button=button;
          goto label;
          
  case 3:do{
           Display_coordinate();
              button = read_LCD_buttons(); prev_button=button;
             }while(button!=btnNONE);
              //prev_button=button;
              goto label;
case 4:do{
             button_loop();
              button = read_LCD_buttons(); prev_button=button;
             }while(button!=btnNONE);
             // prev_button=button;
         goto label; 
   
    
  }


 }

Code 2:Here code is updating date and time only when left key is pressed.SInce i am not calculating previous key pressed.

void lcd_call()
{
  int button;
button = read_LCD_buttons();
if (button ==left)
{
digitalClockDisplay();
}else if((button ==right)
{
 Display_angle();
}else if((button ==up)
{
Display_coordinate();
}else if((button ==down)
{
  button_loop();
}else
{
digitalClockDisplay();
}
int prev_button=0; [b]// No, this will overwrite prev_button and it will not remember what the button previously was. Take it 
int prev_button;//Is it correct
if (button!=btnNONE) [b]// add spaces for clarity, what does btnNONE equal, -1, 0,  '/0' ? [/b]
btnNONE =5;
#define btnRIGHT  0		// Okay
#define btnUP     1		       // inc
#define btnDOWN   2		// dec
#define btnLEFT   3		// Select
#define btnSELECT 4		// Menu
#define btnNONE   5

I understand what you want to do. But you need to follow the comments I gave you. Do you know what a global variable is, and why are you using labels?

HazardsMind:
I understand what you want to do. But you need to follow the comments I gave you. Do you know what a global variable is, and why are you using labels?

Yes i know, global variable is a variable that is accessible in every scope. But i don't which variable made as global variable Here.

Have you checked other code i have written .The code 2 seem working fine if i can retrieve the previous state of button pressed.

I thought of this code. But i couls not able to update it

int button=0;
int prev_button=0;
 void lcd_call()
{

 button = read_LCD_buttons();  

if (button ==btnRIGHT &&prev_button==btnRIGHT )
{
digitalClockDisplay();
}else if(button ==btnUP   && prev_button==btnUP)
{
 Display_angle();
}else if(button ==btnDOWN && prev_button==btnDOWN )
{
Display_coordinate();
}else if(button ==btnSELECT && prev_button==btnSELECT)
{
  button_loop();
}else if(button ==btnLEFT && prev_button==btnLEFT)
{
   Display_Wind();
}else
{
 prev_button= button;
}
}

:roll_eyes:

int prev_button = -1; // set globally at top of code
.
.
.
void lcd_call()
{
  int button = read_LCD_buttons();
  if(button != prev_button)
  {
    prev_button = button;
  } 

  if (prev_button == left)
  {
    digitalClockDisplay();
  }
  else if(prev_button == right)
  {
    Display_angle();
  }
  else if(prev_button == up)
  {
    Display_coordinate();
  }
  else if(prev_button == down)
  {
    button_loop();
  }
  else
  {
    digitalClockDisplay();
  }

} // end of function

It would be more presentable if you used case statements instead of all the IF/ELSE statements for your functions.

Thanks for the code. I tested this . it doing same thing it stay in loop . it wait for key to press again to update the time and date
It should valid for the other key.
means to say if right key pressed it always show Display_angle();
but here it is coming back to digitalClockDisplay(); since no key has been pressed.

else
  {
    digitalClockDisplay();
  }

Ahh, I forgot that when no button is pressed, it outputs 5. So what you do is add “&& button != btnNONE” to “if(button != prev_button)” and it will work.

Ya thanks . Code is working fine .But i found there glitter on screen.Seems background it trying to update values.

Sorry I have pasted code i have written.

void lcd_call()
{
 button = read_LCD_buttons();  
  if(button != prev_button && button !=  btnNONE)
  {
    prev_button = button; 
  } 

  if (prev_button == btnRIGHT)
  { 
   Display_Wind();button = read_LCD_buttons();
  }
  else if(prev_button == btnUP)
  { 
    Display_angle();button = read_LCD_buttons();
  }
  else if(prev_button == btnDOWN)
  { 
    Display_coordinate();button = read_LCD_buttons();
  }
  else if(prev_button == btnSELECT)
  { 
    button_loop();button = read_LCD_buttons();
  }else if(prev_button ==btnLEFT)
  { 
   digitalClockDisplay(); 
  }
  

}

Modified , But still Glitter is there

void lcd_call()
{
 button = read_LCD_buttons();  
  if(button != prev_button && button !=  btnNONE)
  {
    prev_button = button; 
  } 

  if (prev_button == btnRIGHT)
  { 
   Display_Wind();
  }
  else if(prev_button == btnUP)
  { 
    Display_angle();
  }
  else if(prev_button == btnDOWN)
  { 
    Display_coordinate();
  }
  else if(prev_button == btnSELECT)
  { 
    button_loop();
  }else if(prev_button ==btnLEFT)
  { 
   digitalClockDisplay(); 
  }
  

}
 

}

Why do you have all those read lcd button lines? They are not needed, all you need is the first one. That "glitter" is what happens when you rapidly clear the screen and show a new value over and over again.

I have solved glitter issue. But i wanted to its correct or not.But Problem is For Selecting button it takes time. Since due delay.

void loop()
{
lcd_call();
delay(500);
}
void lcd_call()
{
..................Code here.......................
}

ut Problem is For Selecting button it takes time

So, get rid of the "delay()"

If i remove the delay. It start GLittering . IS there any other way to solve glitter . Without affecting program

IS there any other way to solve glitter

Yes, of course.
Only write to the display if the values have changed since last time.

There's a clue in the title of this thread.

AWOL:

IS there any other way to solve glitter

Yes, of course.
Only write to the display if the values have changed since last time.

There's a clue in the title of this thread.

Ya I have Written code written for the same problem, If any key pressed store status and display relevant display .. Is there any wrong with my code syntax so it producing glitter

Syntax is not going to cure "glitter" - if syntax is wrong, the compiler will tell you.
Not writing to the display when you don't need to is going to cure "glitter".