--- title: 'Arduino 紅外線遙控與LED亮度控制' disqus: hackmd --- *** ![downloads](https://img.shields.io/github/downloads/atom/atom/total.svg) ![build](https://img.shields.io/appveyor/ci/:user/:repo.svg) ![chat](https://img.shields.io/discord/:serverId.svg) *** ## 目錄 [TOC] ## 模擬平台 **[![](https://hackmd.io/_uploads/SyZ9VAuvh.png) ](https://tinkercad.com)** ### 使用原因: *** 手邊沒有這麼多類型的電子元件,目前只有一塊學校發的ESP8266, 而Tinkercad是一款免費的3D建模程序,可在Web瀏覽器中運行。 在Tinkercad Circuits中,可以模擬和驗證Arduino開發板的功能, 它們提供了許多電子元件去進行模擬和測試。 ![](https://hackmd.io/_uploads/SkGwW5Xv3.png) ![](https://hackmd.io/_uploads/H1yOb9mP2.png) *** 使用者故事 -- ```gherkin= Feature: Arduino LED Control Scenario: LED blinking with light sensor Given the LED pin is set to 13 And the Arduino program is running When I press a button on the infrared remote control Then the LED should turn on And the LED brightness should vary based on the light sensor input And the LED should blink on and off every 100 milliseconds until I press the button again And the LED should turn off ``` > Read more about Gherkin here: https://docs.cucumber.io/gherkin/reference/ 實作部分 --- ### 電路視圖: ![](https://hackmd.io/_uploads/ryZzK2rv2.png) ### 線路視圖: ![](https://hackmd.io/_uploads/r184Y2rPh.png) ### 元件清單: ![](https://hackmd.io/_uploads/SksBt3Sv2.png) ### 程式碼: ```cpp #include <IRremote.h> const byte RECV_PIN = 2; // 紅外線接收接腳 const byte LED_PIN = 13; // LED 接腳 const byte analogInPin = A5; // 光敏電阻接腳 int sensorValue = 0; // 預設光敏电阻的值為"0" int outputValue = 0; // 預設輸出的值為"0" IRrecv irrecv(RECV_PIN); // 建立紅外線接收器物件,指定接收腳位 decode_results results; // 建立解碼結果物件 void setup() { Serial.begin(9600); // 初始化串列通訊,設定鮑率為 9600 pinMode(LED_PIN, OUTPUT); // 設定 LED 腳位為輸出模式 pinMode(analogInPin, INPUT); // 設定光敏电阻輸入腳位為輸入模式 irrecv.enableIRIn(); // 啟用紅外線接收器 } void loop() { if (irrecv.decode(&results)) { // 當接收到紅外線訊號時 Serial.println(results.value, HEX); irrecv.resume(); // 清除接收緩衝區,以便在下一次迴圈中可以接收到新的紅外線信號。 while (1) { // 進入無限迴圈 sensorValue = analogRead(analogInPin); // 讀取光敏電阻輸入的值 outputValue = map(sensorValue, 974, 54, 0, 255); // 將讀取的值映射到LED亮度範圍 analogWrite(LED_PIN, outputValue); // 設定 LED 的亮度 Serial.println("LED_on"); delay(100); // 延遲 100 毫秒 if (irrecv.decode(&results)) { // 如果再次收到紅外線訊號 break; // 跳出迴圈 } } digitalWrite(LED_PIN, LOW); // 關閉 LED irrecv.resume(); // 清除接收緩衝區,以便在下一次迴圈中可以接收到新的紅外線信號。 } } ``` ### 輸出: ![](https://hackmd.io/_uploads/ByUZ9nrw2.png) ### 問題: *** 在使用Tinkercad模擬時: 按下遙控器上的任何按紐,收到的紅外線訊號都是"FFFFFFFF", 可能是因為Tinkercad在紅外線模擬方面存在一些限制或問題。 Tinkercad是一個虛擬模擬平台,並不完全模擬所有硬體的功能。 在某些情況下,特別是與較複雜的硬體模組相關的模擬可能不夠準確或不支援某些功能。 因此,紅外線訊號在Tinkercad模擬中可能無法正確地被模擬和接收, 導致一直收到的訊號值為 "FFFFFFFF"。這並不代表程式碼有問題, 而是模擬平台的限制所致。 但是,我在YT看別人用相同的平台做類似的模擬,輸出卻毫無異常, ![](https://hackmd.io/_uploads/BkcPh3Hvn.png) 影片上傳時間是11個月前。 這讓我感到無比困惑,目前仍在尋找問題點。 2023/6/14 更: ![](https://hackmd.io/_uploads/r1ddZmUwn.png) 來自影片作者的回覆。 *** ### 優化內容: > **由於接收訊號的問題,優化的部分無限期延後,直到問題解決。** ### 輸出: > **同上。** :::info **問題尚未解決TT,有任何想法歡迎留言!** ::: 使用者流程 --- ```sequence User->Arduino: 啟動程序 Arduino->LED: 設置引腳為輸出模式 User->Arduino: 開始執行程序 Arduino->Infrared Remote Control: 等待紅外遙控器按紐按下 Infrared Remote Control->Arduino: 發送紅外信號 Arduino->LED: 打開LED燈 Arduino-->User: 返回LED已點亮的消息 Arduino->Light Sensor: 讀取光敏傳感器輸入值 Arduino->LED: 根據光敏傳感器输入值調整LED亮度 Arduino->Delay: 延遲100毫秒 Arduino->LED: 關閉LED燈 Arduino-->User: 返回LED已熄滅的消息 Arduino->Delay: 延遲100毫秒 Arduino->Infrared Remote Control: 等待紅外遥控器按紐再次按下 Infrared Remote Control->Arduino: 發送紅外信號 User->Arduino: 結束程序 ``` > Read more about sequence-diagrams here: http://bramp.github.io/js-sequence-diagrams/ 項目時間軸 --- ```mermaid gantt title 時間規劃 section 項目 LED亮度控制       :2021-06-12, 1d 紅外線遙控 :2021-06-12, 1d 偵錯 :2021-06-12, 1d section 優化 嘗試解決問題(尚未解決) :2021-06-13, 1d ``` > Read more about mermaid here: http://mermaid-js.github.io/mermaid/ ## 附錄&常見問題 :::info **Find this document incomplete?** Leave a comment! ::: ###### tags: `Arduino` `Arduino_Uno` `Tinkercad_Circuits` `LED_ Brightness_Control` `Infrared_Remote_Control`