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


沒有留言:

張貼留言

Telegram +ESP32自動發報機

  Telegram   +ESP32自動發報機 這套系統是一個典型的 IoT(物聯網)架構 ,結合了遠端配置(Python)、通訊中介(MQTT)與硬體執行(ESP32)。 以下我為您拆解這兩支程式的核心運作原理: 一、 系統架構流程 Python 端 (控制台) :使用者輸入...