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


沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...