B4R Code Snippet Enhanced pusbutton handling

This small sketch shows how to handle pushbutton debouncing and automatic repetition.
It's a full working example of one button handling, duplicate the code for each button, changing variables' name according to your needs. The timer is one for all, so no need to duplicate, simply duplicate and adjust code inside the timer.
Have fun:)
 

Attachments

  • enpusbutton.zip
    1.5 KB · Views: 329
Last edited:

tigrot

Well-Known Member
Licensed User
Longtime User
If you want to spare one byte for each button, reuse counter for the two conditions, since they are independent.
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private Timertasti As Timer
' ****** Define a block of variables for each pushbutton defined
    Private premutof1 As Byte=0   ' valore iniziale
    Private timerstf1 As Byte=0  'timer F1 start repeat
    ' Private timerrpf1 As Byte=0  'timer ripetizione
    Private ButtonF1 As Pin 'pin for the button
    Private BounceTimeF1 As ULong
    Private BounceDelayF1 = 10 As ULong
'********************** End pusbutton define
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Timertasti.Initialize("timertasti_Tick",20)
    Timertasti.Enabled=True
    'Using the internal pull up resistor to prevent the pin from floating. define for each buttom
    ButtonF1.Initialize(22, ButtonF1.MODE_INPUT_PULLUP) ' PinA0
    ButtonF1.AddListener("ButtonF1_StateChanged")
End Sub

Private Sub sendtasto(tasto As Byte)
Log("Key pressed" , tasto)
End Sub

private Sub timertasti_Tick
'***** define this block for each button you need *************
    If premutof1=1 Then
        timerstf1= timerstf1+1
        If timerstf1>30 Then
            premutof1=2
            sendtasto(0) ' first virtual pushbutton pressed event
            timerstf1=0
    End If
    End If
    If premutof1=2 Then
        timerstf1= timerst1+1
        If timerstf1>10 Then
            timerstf1=0
            sendtasto(0)  ' repeating virtual pushbutton pressed event
        End If
    End If
'************************ End pushbutton block
End Sub

'**** Event block for each pushbutto defined
Private Sub ButtonF1_StateChanged (State As Boolean)
    Log("STATE P1:",State)
    If State = False Then 'remember, False means button pressed.
        If Millis - BounceTimeF1 < BounceDelayF1 Then
            Log("debounce")
            Return 'Return, bouncing
        Else
            BounceTimeF1 = Millis 'reset BounceTime to current time
            sendtasto( 0)  '***** every button can share the same sub or you can define a different sub
            premutof1=1  'inizia l'attesa per la ripetizione
        End If
    Else
        premutof1=0   ' valore iniziale
        timerstf1=0  'timer F1 start repeat
        ' timerrpf1=0  'timer ripetizione
    End If
End Sub
 
Last edited:
Top