News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Improved debounce for keypad

Started by John Drew, Apr 16, 2022, 08:16 AM

Previous topic - Next topic

John Drew

Quote from: Yasin on May 19, 2022, 12:33 PM@keytapper thaks a lot. This was out of my mind.
Endproc and I assume Exitproc is the same, they are just a Return in different clothes
John

Yasin

Quote from: John Drew on May 19, 2022, 01:52 PMEndproc and I assume Exitproc is the same, they are just a Return in different clothes
John

Dear @John Drew  They are not the same. While in PROC, ENDPROC can be used once. Its purpose is to tell the compiler where PROC ends. EXITPROC can be written more than once. To return when there is no need to continue PROC.

John Drew

Thanks for the correction Yasin. I'll have to read the manual :)
Cheers
John

John Drew

#23
@basiclover

Here's a simple setup that reads the keypad and then debounces. It shows a complete procedure and a way of calling it.
There are many other ways to debounce as shown on this thread, some of them simpler. I find this one very effective.
Note you need to dimension bInkey as a global byte  (that is, outside the procedure).
I haven't tried to compile it but I think it's free of errors.

'dimension bInkey as byte

Proc GetKey()
Dim bCountIn As Byte
Dim bTemp As Byte

    bCountin = 0
    bInkey = InKey
    If bInkey = 16 Then          'no press
      Return
    Else
      bTemp = binkey            'contact of button
    EndIf 
    Repeat                      'look for delays in no button
    Agn:bInkey = InKey
        If bInkey = bTemp And bInkey <> 16 Then
            GoTo Agn
        Else
            If binkey = 16 then
              Inc bCountIn
              DelayMS 10
            Else
              bcountin = 0
            EndIf 
        EndIf     
    Until bCountIn = 10
    bInkey = bTemp
EndProc

Main:
    GetKey()
    If bInkey<> 16 Then Print At 1,1, Dec bInkey
    GoTo Main

basiclover

seems that code lines are missing ? or I'm stupid

keytapper

Quote from: John Drew on May 20, 2022, 05:12 AMHere's a simple setup

Sorry for spotting your oversight. There could be a better readability if you will use
code rather than quote tags ;D
Ignorance comes with a cost

Gamboa

BasicLover,

Click on [Expand], and you will see all the code.

Regards,
Gamboa
Long live for you

basiclover


John Drew

#28
Click on expand button.
I couldn't find the code button, but I've fixed it now. Thanks Gamboa.
John

basiclover

Hy John
when I compile I get error "item missing" on the last EndIf

basiclover

added "then" at the end of line "If binkey = 16", then it compiles
any comment?

Dompie

In old versions of the compiler you could often omit 'then' but in the latest versions of the Positron compiler 'then' is often necessary.

Johan

basiclover


John Drew

#33
Quote from: basiclover on May 20, 2022, 02:29 PMadded "then" at the end of line "If binkey = 16", then it compiles
any comment?
Whoops, My most frequent mistake. Sorry about that. I also program in Delphi where "then" is not required. That's my excuse :)
I've fixed it with an edit.
John

basiclover

no excuse required John
I appreciate your help very much
Thanks again