搜尋程式碼

第14/31-5課:Arduino編程map()示例2

第14/31-5課:Arduino編程map()示例2

呢個教學示範點樣用 Arduino map() 函數嚟控制 LED 嘅亮度,用百分比數值(0–100%)而唔係 analogWrite() 需要嘅原始 0–255 範圍。呢個係一個基本技巧,可以簡化你嘅程式碼,令佢更加直觀,因為你可以用百分比嚟思考,而唔係任意嘅位元組數值。

Sunfounder 車仔組裝完成

呢個技巧對任何需要調整感應器讀數或者控制訊號嘅項目都好重要。例如,你可以用佢嚟:

  • 根據可變電阻嘅讀數控制 LED 嘅亮度或者摩打嘅速度。
  • 將溫度感應器嘅輸出(例如 0–1023)轉換成攝氏或者華氏範圍。
  • 將搖桿嘅模擬輸入對應嚟控制伺服摩打嘅位置。
  • 建立一個簡單嘅用戶介面,用百分比數值嚟設定裝置嘅功率水平。

完成呢堂課之後,你就會識得寫更乾淨、更容易維護嘅程式碼,令佢更容易閱讀同調整。

硬件/零件

呢個項目用嘅零件好少,好適合初學者。你需要:

  • 一塊 Arduino 板(例如 Uno)。
  • 一粒 LED。
  • 一粒 220 歐姆電阻。
  • 杜邦線。
  • 一塊麵包板(可選但建議用)。

接線指南

呢個項目嘅接線好簡單,好似影片入面示範咁(影片 20:47)。LED 通過一粒 220 歐姆限流電阻連接到 Arduino 嘅第 3 腳。

將 LED 嘅陽極(較長嘅腳)通過電阻連接到第 3 腳。將 LED 嘅陰極(較短嘅腳)連接到 Arduino 嘅接地(GND)腳。

程式碼解釋

呢個例子集中喺一個用戶自訂函數,用嚟簡化控制 LED 亮度嘅過程。程式碼圍繞一個自訂函數 pwmValue() 嚟建立,呢個係令程式碼變得直觀嘅關鍵。

等我哋拆解吓程式碼入面可以設定嘅部分:

int LEDpin = 3; //定義 LED 嘅腳位

呢行定義咗 LED 連接嘅 Arduino 腳位。你可以將數字 3 改做你 Arduino 板上任何其他支援 PWM 嘅腳位。

呢個項目嘅核心係自訂函數 pwmValue()。呢個函數接收一個百分比數值作為輸入,然後將佢轉換成 analogWrite() 使用嘅 0–255 範圍。呢度就係 map() 函數發揮作用嘅地方。

int pwmValue(int percent)
{
  if(percent >=0 && percent <=100)
  {
    return map(percent, 0,100,  0,255);
  }else
  {
    return 0;
  }
}

要用呢個函數,你只需要用你想要嘅亮度百分比嚟呼叫佢。函數會返回啱嘅數值畀 analogWrite()。例如,pwmValue(60) 會返回 153,即係 255 嘅 60%。函數仲包含一個安全檢查,確保輸入嘅百分比係喺 0 到 100 之間,如果超出呢個範圍就返回 0。

setup() 函數入面,你可以睇到點樣用佢:

int b=5;//60% 亮度
Serial.println( pwmValue(b)   );//將佢打印喺序列監視器
analogWrite(LEDpin, pwmValue(b));//用 60% 亮度開著 LED

呢度,變數 b 設定為你想要嘅亮度百分比。然後用函數 pwmValue(b) 嚟將對應嘅數值打印到序列監視器,同時設定 LED 嘅亮度。要改變 LED 嘅亮度,你只需要修改 b 嘅數值。map() 函數會幫你處理所有需要嘅計算。

實際項目/示範

喺影片入面,主持人透過改變 b 變數嘅數值並將程式碼上傳到 Arduino 嚟示範呢個項目。當 b 設定為 60 時,程式碼會將佢對應到 153,LED 會以 60% 亮度著燈。將 b 改做 90 會對應到 229,令 LED 明顯更光。設定為 100 會對應到 255,即係最大亮度。示範清楚顯示咗 map() 函數點樣令你可以用直觀嘅百分比數值控制 LED,同時序列監視器確認咗實際發送到腳位嘅 0–255 數值(影片 21:29)。

呢條影片嘅相關項目

章節

  • [00:00] Millis 同 map 函數簡介
  • [01:19] 咩係 Millis 同點解要用佢
  • [05:46] 用 Millis 連事件嚟採取行動
  • [08:35] Millis 例子 2 連按鈕
  • [12:14] Millis 例子 3 檢查兩次按鈕按下
  • [17:20] map() 函數簡介
  • [19:47] 將百分比對應到 LED 亮度嘅程式碼例子
  • [20:47] map() 函數嘅實際示範
  • [22:41] 建立自訂函數嚟處理對應

圖片

Sunfounder car assembled
Sunfounder car assembled
Sunfounder car assembled
Sunfounder car assembled
920-Lesson 14/31-5: Arduino coding map() example 2
語言: C++
/*
Lesson 14/31-5: Arduino coding map( ) example-2
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);//initialzie serial monitor with 9600 baud
    pinMode(LEDpin, OUTPUT);//set pin as output
    int b=5;//60% brightness

    Serial.println( pwmValue(b)   );//print it on serial monitor
    analogWrite(LEDpin, pwmValue(b));// turn the LED on with 60% brightness   
 }

 void loop() {

 
 }

/*
 * pwmValue(int percent)
 * @param precent is value between 0 to 100
 * @return it returns the mapped value from 0 to 255
 * written by Ahmad Shamshiri on Dec 9, 2022
 * 
 */
 int pwmValue(int percent)
 {
  if(percent >=0 && percent <=100)
  {
    return map(percent, 0,100,  0,255);
  }else
  {
    return 0;
  }
 } //pwmValue end
 

資源與參考資料

文件📁

用户手册