2021年11月16日 星期二

B4R Button Debounce

 B4R  Button Debounce







#Region Project Attributes

#AutoFlushLogs: True

#CheckArrayBounds: True

#StackBufferSize: 600

#End Region

'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src


Sub Process_Globals

Public Serial1 As Serial

Private pinButton As Pin 'pin for the button

Private pinLED2 As Pin 'pin for LED 2 on the esp32 buildin LED

Private pinLEDGreen As Pin 'pin for the green Led

Private LightOn = False As Boolean

Private BounceTime As ULong

Private BounceDelay = 10 As ULong

End Sub



Private Sub AppStart

Serial1.Initialize(115200)

Log("AppStart")

pinButton.Initialize(23, pinButton.MODE_INPUT_PULLUP)

'Using the internal pull up resistor to prevent the pin from floating.

pinButton.AddListener("pinButton_StateChanged")

pinLEDGreen.Initialize(12, pinLEDGreen.MODE_OUTPUT)

End Sub



Sub pinButton_StateChanged (State As Boolean)

Log("State: ", State)

'state will be False when the button is clicked because of the PULLUP mode.

If State = False Then 'remember, False means button pressed.

If Millis - BounceTime < BounceDelay Then

Return 'Return, bouncing

Else

LightOn = Not(LightOn)

pinLEDGreen.DigitalWrite(LightOn)

BounceTime = Millis 'reset BounceTime to current time

End If

End If

End Sub


沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...