2021年11月15日 星期一

B4R第一個實驗 按鍵控制LED

 B4R第一個實驗 按鍵控制LED

1) IDE環境設定

安裝 Arduino IDE 與 B4R程式

設定Arduino ESP8266,ESP32環境

點擊額外的開發板管理員網址最右方圖示 boards,開啟開發板管理員網址輸入視窗,填入下方連結

https://arduino.esp8266.com/stable/package_esp8266com_index.json

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json


B4R 設定ESP32板子暨軟體存放位置




確認板子與USB port 號碼


按B4R程式的F5 或組譯與上傳


長按ESP32主板上右下的IO0按鈕(有些會標示為BOOT),直到出現上傳開始再放開IO0,此按鈕為工作模式切換鈕,上傳程式時都需要按這個按鈕將ESP32的「工作模式」轉換成「下載模式」,此為保護措施,避免無意間覆蓋掉原有的程式內容,不過有些ESP32已經取消這個機制,稱為一鍵下載(或一鍵燒錄)。
按一下左側RESET重開機以開始工作
按Reset 啟動App Start



2) 程式

#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


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...