read switch a few times to be sure and debounce......
IF UPBT=0 THEN
PAUSE 20 ' or 40 or 50
IF UPBT=0 THEN 'go ahead........
MENUITEM = MENUITEM+1
do same for release check .... if upbt=1 then.....
read switch a few times to be sure and debounce......
IF UPBT=0 THEN
PAUSE 20 ' or 40 or 50
IF UPBT=0 THEN 'go ahead........
MENUITEM = MENUITEM+1
do same for release check .... if upbt=1 then.....
Last edited by amgen; - 21st April 2023 at 16:26.
For couple of buttons this code works fine, but when I increase number of button handling operations, some weird things occur - the further from beginning is the button handling routine, more rarely it responds to user.
The code below has 8 "blocks" of button handling.
first 3 work fine, 4th one works only maybe on 10 or 15th press. 5th and further - do not work at all.
And this is not issue of particular code - if I move say "5th block" to 1st place of this code, then it works fine.
PIC18F45K80 @64mhz.
Code:IF UPBT=0 THEN MENUITEM=MENUITEM+1 IF MENUITEM>6 THEN MENUITEM=1 pause 200 ENDIF WHILE UPBT=0:WEND IF DNBT=0 THEN MENUITEM=MENUITEM-1 IF MENUITEM<1 THEN MENUITEM=6 pause 200 ENDIF WHILE DNBT=0:WEND IF LBUT=0 AND MENUITEM=2 THEN RICXVI=RICXVI+1 IF RICXVI>31 THEN RICXVI=1 pause 200 GOSUB SETTIME ENDIF WHILE LBUT=0:WEND IF RBUT=0 AND MENUITEM=2 THEN RICXVI=RICXVI-1 IF RICXVI<1 THEN RICXVI=31 pause 200 GOSUB SETTIME ENDIF WHILE RBUT=0:WEND IF LBUT=0 AND MENUITEM=1 THEN TVE=TVE+1 IF TVE>13 THEN TVE=1 pause 200 GOSUB SETTIME ENDIF WHILE LBUT=0:WEND IF RBUT=0 AND MENUITEM=1 THEN TVE=TVE-1 IF TVE<1 THEN TVE=13 pause 200 GOSUB SETTIME ENDIF WHILE RBUT=0:WEND IF LBUT=0 AND MENUITEM=3 THEN DGE=DGE+1 IF DGE>7 THEN DGE=1 pause 200 GOSUB SETTIME ENDIF WHILE LBUT=0:WEND IF RBUT=0 AND MENUITEM=3 THEN DGE=DGE-1 IF DGE<1 THEN DGE=7 pause 200 GOSUB SETTIME ENDIF WHILE RBUT=0:WEND
I tried to reduce PAUSE 200 to 20 or even 2 - no change.
what about something like this instead?
it could be simplified even more, but...Code:IF UPBT=0 THEN MENUITEM=MENUITEM+1 IF MENUITEM>6 THEN MENUITEM=1 PAUSE 20 WHILE UPBT=0:WEND PAUSE 20 ENDIF IF DNBT=0 THEN MENUITEM=MENUITEM-1 IF MENUITEM<1 THEN MENUITEM=6 PAUSE 20 WHILE DNBT=0:WEND PAUSE 20 ENDIF IF LBUT=0 THEN IF MENUITEM=1 THEN TVE=TVE+1 IF TVE>13 THEN TVE=1 GOSUB SETTIME ENDIF IF MENUITEM=2 THEN RICXVI=RICXVI+1 IF RICXVI>31 THEN RICXVI=1 GOSUB SETTIME ENDIF IF MENUITEM=3 THEN DGE=DGE+1 IF DGE>7 THEN DGE=1 GOSUB SETTIME ENDIF PAUSE 20 WHILE LBUT=0:WEND PAUSE 20 ENDIF IF RBUT=0 THEN IF MENUITEM=1 THEN TVE=TVE-1 IF TVE<1 THEN TVE=13 GOSUB SETTIME ENDIF IF MENUITEM=2 THEN RICXVI=RICXVI-1 IF RICXVI<1 THEN RICXVI=31 GOSUB SETTIME ENDIF IF MENUITEM=3 THEN DGE=DGE-1 IF DGE<1 THEN DGE=7 GOSUB SETTIME ENDIF PAUSE 20 WHILE RBUT=0:WEND PAUSE 20 ENDIF
You could create a bit Flag. When the Button is pressed, the Flag is toggled. When you get to a reasonable point in Code1 Routine, check your Flag. If it has toggled (is now a 1 for example), use GOTO to jump to Code2. Put "IF Flag = 0 THEN : GOTO Code1" in convenient spots throughout in Code2, etc.
debounce button check... and instead of while-wend .... use for/next pause here is 2 second check so if holding button > 2 seconds, can advance MENUitem then check again for release
HERE:
IF UPBT=0 THEN
pause 50
IF UPBT=0 THEN ..............................#debounce check
MENUITEM=MENUITEM+1
IF MENUITEM>6 THEN MENUITEM=1
PAUSE 20
for a=1 to 20
pause 50................................................ ............# if 2 second press, advance item and wait again
IF UPBT=1 THEN ---leave to somewhere .................# XXXXX WHILE UPBT=0:WEND
next a
goto HERE
ENDIF
Last edited by amgen; - 21st May 2023 at 18:59.
First I would read all button input at once. Debounce and then decide what to do.
This is like a state machine coding and you will not loose any keypress, especially if it is interrupt driven. Or if you do not want interrupts, then a tight loop can do the job just fine.
Spaghetti coding unfortunately leads to such results.
Ioannis
Bookmarks