第14/31-4課:Arduino編程 map() 示例1
呢個教學係Arduino編程系列嘅一部分,重點係實際應用map()函數。喺呢一課,你會學到點樣將百分比數值(0–100)轉換成PWM(脈衝寬度調製)數值(0–255)嚟控制LED嘅亮度。唔使手動計60%等於153,map()函數會幫你處理線性轉換,令你嘅代碼更簡潔同直觀。
呢種技術喺好多實際應用中好重要,特別係你需要將感測器讀數或用戶輸入轉換成唔同範圍嘅時候。例如,你可以用佢嚟:
- 根據電位器讀數控制直流摩打嘅速度。
- 將溫度感測器嘅原始數值顯示喺0-100%嘅刻度上。
- 將搖桿座標映射到伺服摩打嘅角度。
- 根據環境光感測器調整LED亮度。
- 將0-5V模擬讀數轉換成0-1023數碼數值,或者反過來。
硬件/組件
要跟住呢個項目做,你需要以下組件:
- Arduino板(Uno、Nano等)
- 一粒LED
- 一粒220歐姆電阻
- 杜邦線
- 一塊麵包板(可選,但建議使用)
接線指南
喺影片入面,導師用咗之前一課關於LED漸變嘅接線設置嚟示範呢個項目。電路好簡單:將LED嘅陽極(長腳)連接到220歐姆電阻,再將電阻嘅另一端連接到Arduino嘅數碼引腳3。將LED嘅陰極(短腳)直接連接到Arduino嘅接地(GND)引腳。
代碼解釋
呢個例子嘅代碼好簡潔,核心邏輯喺setup()函數入面。等我哋拆解吓用戶可以配置嘅部分。
int LEDpin = 3; //定義LED嘅引腳
呢行定義咗你LED連接嘅Arduino引腳。喺呢個例子入面,設定為引腳3,呢個係支援PWM嘅引腳,係模擬輸出所必需嘅。如果你將LED連接到其他PWM引腳,可以更改呢個數字。
int b=8;//60%亮度
呢個係你會修改嚟測試唔同亮度水平嘅關鍵變數。佢代表你想要嘅亮度百分比。代碼入面嘅註釋寫住「60%」,但喺影片入面,為咗示範目的,數值設定為8。你可以將佢改成0到100之間嘅任何整數。例如,設定b = 60就代表60%嘅亮度水平。
int brightness = map(b, 0, 100, 0,255);
呢個係成課嘅核心。map()函數將你嘅百分比數值(b)從原本嘅範圍(0到100)轉換成新嘅目標範圍(0到255)。結果儲存喺brightness變數入面,然後用嚟向LED引腳寫入模擬數值。
analogWrite(LEDpin, brightness);// 以60%亮度開啟LED
最後,呢行用analogWrite()將映射後嘅數值發送到LED引腳。呢個設定LED嘅佔空比,從而控制佢嘅亮度。呢種方法嘅好處係,你只需要更改b變數嚟控制LED,而map()函數會處理所有必要嘅計算。
實際項目/示範
喺影片入面,導師上傳代碼並示範更改b變數嘅效果。當b設定為8時,串行監視器會打印映射後嘅數值20,LED明顯好暗。將b改為90會得到映射數值229,令LED光好多。將b設定為100會映射到255,即全亮度。呢個實際示範清楚顯示咗map()函數點樣簡化用用戶友好嘅百分比數值控制模擬輸出嘅過程。
呢條影片嘅相關項目
- 第14/31-1課:Arduino Millis(無延遲)同map函數 | SunFounder Robojax
- 第14/31-2課:Arduino編碼millis()示例2(帶事件)
- 第14/31-3課:Arduino編碼millis()示例3(帶按鈕)
- 第14/31-5課:Arduino編碼map()示例2
章節
- [00:00] Robojax嘅Arduino課程簡介
- [01:19] 咩係millis()同點解要用佢
- [05:46] 示例1:用millis()做計時事件
- [08:29] 示例2:用millis()配合按鈕
- [12:14] 示例3:延遲後偵測第二次按鈕按下
- [17:20] map()函數簡介
- [19:47] map()示例嘅代碼講解
- [20:41] map()函數嘅實際示範
/*
Lesson 14/31: Arduino coding map( ) example-1
Get this code and watch video Download and resource page https://robojax.com/RJT597
YouTube video https://www.youtube.com/watch?v=lqgzQohNiIM
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
int LEDpin = 3; //defind pin for LED
void setup() {
Serial.begin(9600);//initialize serial monitor with 9600 baud
pinMode(LEDpin, OUTPUT);//set pin as output
int b=8;//60% brightness
int brightness = map(b, 0, 100, 0,255);
Serial.println(brightness);//print it on serial monitor
analogWrite(LEDpin, brightness);// turn the LED on with 60% brightness
}
void loop() {
}
你可能需要嘅嘢
-
亞馬遜SunFounder Arduino Learning Kit on AliExpresss.click.aliexpress.com
-
亞馬遜
-
亞馬遜
-
亞馬遜
資源與參考資料
-
文件记录Documentation for SunFounder 3 in 1 IoT/Smart Car/Learning Kitdocs.sunfounder.com
-
外部Purchase SunFounder's 3-in-1 Smart car learning kitsunfounder.com
文件📁
用户手册
-
SunFounder 3-in-1 Arduino Smart Car assembly manualThis document shows step by step assembly of SunFounder 3-in-1 Arduino Smart Car
SunFounder_car_assembly_manual.pdf1.20 MB