void UP_Down_Keyvalue(struct menu *s1ptr,int i,int b)
{ int temp=0,ch,position=0,lower=0,upper=12;
int array_temp[11];
LCD_GoToXY(1,i);
LCD_Printf("%d:%d:%d:%s",s1ptr->Hour,s1ptr->Minute,s1ptr->Second,"AM");
while(OK_S!=64)
{
LCD_GoToXY(1,i);
LCD_DisplayNumber(10,temp,2);
if(UP_S)
{ while(UP_S);
if(temp==upper)
temp=-1;
temp++;
}
if(DOWN_S) // down
{ while(DOWN_S);
if(temp==0)
temp=13;
--temp;
}
exit2:
if(RIGHT_S||LEFT_S)
{
array_temp[i];
if(RIGHT_S)
{ while(RIGHT_S);
if(i==11)goto exit1; // check is it last point 10 ie 11
i +=3;
temp=0;
}
exit1:
if (LEFT_S)
{ while(LEFT_S);
if(i==2) goto exit2;
i -=3;
temp=0;
}
// LCD_Printf("%d",i);
//DELAY_ms(10000);
}
s1ptr->Hour= array_temp[1];
s1ptr->Minute=array_temp[3];
s1ptr->Second=array_temp[5];
}
}
The while loop in the program which break the program enne press ok buttonYour code written in inline style is quite confusing, don't have separating tasks into specific functions, has the while statement that inserts a pause in the program run, besides containing goto instructions which are not recommended in C, breaking the structure of the program. Anyway, answering the question: A feasible option in a simple case like this can be creating a 'state machine' with the switch-case statements, where depending on the previous state, the detection of events from each button switch exectution to the appropriate state.
Please am asking a good logic or sample reference or an algorithmThis is very poor practice to use key scanning functions together with display handling which is high time consuming. There is no filtering alghorighm, flowchart itself is unclear and unreadable. I whould say, that this is a good example 'how the code should not looks like'.
How to do this an efficient way ?
You have already got some tips, so I don't think anyone will implement them for you, we hope that you will do it by yourself. Just summarizing in a more direct way - if you have not read anything of what was said: Create separate functions, one for each task (display, keyboard, time, ...) and another function to control the change of state, and manage them allocating in time the execution of each one. Instead of stamping the routines straigh inline, think about dealing with flags.
Code C++ - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 [CODE] int navigation_key(int key,int key_max_value,int key_lower_value ) { int step = 3;// two digti and ":" int key_value; while(!key) { int last_postion = 11 // for LCD Last Postion for display int first_position = 2 // for LCD first position for display swithc(key) { case 3: // for UP key if(position == key_max_value) key_value -= key_lower_value; // resetting key_value key_value++; return key_value; // return to display case 4: // for down key if(key_value == lower_value) key_value = key_max_value; key_value--; return key_value; case 2: // Right key if(position == last_postion) position -=step; // if lcd position maximum backward one position position++; return position; case 1: // left Key if(position == first_position) position +=step; // if lcd position first forward to next position--; return position; }[/CODE]
while(!key)
...
{
swithc(key)
case 4: ...
case 3: ...
case 2: ...
case 1: ...
besides containing goto instructions which are not recommended in C, breaking the structure of the program.
/* Function Key Value For UP_Down Key */ int UP_Down_Keyvalue(int upper,unsigned int lower,char position)
{ int ch;
int key_value = 0;
do{ LCD_GoToXY(1,position);
LCD_DisplayNumber(10,key_value,2);
ch = Key_pressed();
if(ch==3)
{ if(key_value==upper)
{ key_value = lower; goto exit1;}
key_value++;
}
if(ch==4)
{ if(key_value==lower)
{ key_value = upper;goto exit1;}
key_value--;
}
exit1: continue;
}while(ch!=5);
return key_value;
}
temp =UP_Down_Keyvalue(59,0,2);// uper value ,lower value ,lcd display position
Hi,
Consider to use a debounce technique..
Currently I only see level sensitive key_press processing, but you need to process key_down events (edges) only.
But maybe I overlooked this.
Klaus
Code C++ - [expand] | |
1 2 3 4 5 6 7 8 9 10 11 | int Key_pressed(void) { while(1){ if (LEFT_S) ms_delay()50; { while(LEFT_S);return 1; } if (RIGHT_S)ms_delay()50;{ while(RIGHT_S);return 2; } if (UP_S) ms_delay()50; { while(UP_S); return 3; } if (DOWN_S) ms_delay()50;{ while(DOWN_S);return 4 ; } if (OK_S)ms_delay()50; { while(OK_S);return 5 ; } else return 0; } } |
You can use the code posted in post #14. It has debounce implemented.
Why not recommended goto structure , I have seen peoples used somewhere
But it is not level sensitive denounce .You can use the code posted in post #14. It has debounce implemented.
But I didn't have enough logic .I am child in this field but ,It is just a matter of good practices on structured programming, since in this way your routines will no longer have well defined entry and exit points turning it hard to debug; which does not prevent the use of such a resource, but I doubt that people actually use it when strictly necessary.
Clear WDT and timeout feature has to be implemented while waiting at the while() loop.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?