B4R第一個實驗 按鍵控制LED
1) IDE環境設定
安裝 Arduino IDE 與 B4R程式
設定Arduino ESP8266,ESP32環境
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
Programming |
Reset |
影片 : https://www.youtube.com/watch?v=hQDmtX4vOBI
#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
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")
pinLED2.Initialize(2, pinLED2.MODE_OUTPUT)
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.
pinLED2.DigitalWrite(Not(State))
If State = False Then 'remember, False means button pressed.
LightOn = Not(LightOn)
pinLEDGreen.DigitalWrite(LightOn)
End If
End Sub
沒有留言:
張貼留言