Try   HackMD

0929 Arduino 基礎

tags: 物聯網實境遊戲應用課程

電子零件

  • 課堂上提供的零件於修課結束 (包含退選) 後要歸還,也可以自費帶走。請愛惜使用,如果損壞則請自費。
    • L298N 馬達驅動模組 100元
    • 馬達+輪胎組 78元
    • 萬向輪 48元
  • 電子零件購買
    • 民族路二段上:南一、美和。單價比較高,適合少量購買
    • 露天。單價較低,但需要運費,適合團購

期中驗收

  • 迷宮規格:路寬 28 cm、牆高 15 cm
  • 建議車輛長寬不要超過 20 cm

Arduino

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 主要撰寫程式語言為 C++

  • 為設計學院學生設計的開發板,用來設計人機互動的專案。擁有 high-level API,程式初學者也可以好上手

  • 透過 pin 腳與 sensor 溝通,感知外部環境;或是控制外部元件,作出對應的反應

  • Arduino 專案介紹

  • 開發板

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    • 運作電壓:5V
    • 可接受之供電電壓:5V (From USB-type b)、6V~12V (From 5.5 power connector)
    • 比較
    型號 數位輸出 類比輸出 最大執行檔大小 時脈
    UNO 14 6 32KB 16MHz
    MEGA 54 16 256KB 16MHz

Arduino IDE

  • 官網提供 arduino 專屬的 IDE

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 工具列 icon 依序為

    • 編譯:驗證程式碼
    • 上傳:將編譯出來的執行檔上傳到開發板
    • 開新檔案
    • 開啟舊檔
    • 儲存檔案
    • Serial Monitor:與開發板溝通的介面
  • 設定使用的開發板型號

    • Tools -> Board:選擇使用的型號
    • Tools -> Processor:[不一定會有]選擇開發板的處理器型號
    • Tools -> Port:選擇開發板的連接阜,只要將開發板接到電腦上,IDE 會自動出現對應的選項
      • Windows:控制台 -> 硬體及音效 -> 裝置管理員 -> 連接阜 -> 看開發板對應的 COM 號碼

Arduino API (部分)

基本函式

  • Arduino 程式由主要兩個函式構成
  • setup():初始化函式
  • loop():程式迴圈,主要運作程式寫在這裡

GPIO

  • pinMode(pinNum, mode):設定 pin 腳的運作模式
    • OUTPUT:讓 pin 腳可以輸出訊號,輸出電壓為 5V。配合 digitalWrite(pinNum, value) 來控制輸出訊號。
    • INPUT:讓 pin 腳可以讀取訊號,不要超過 5V。配合 digitalRead(pinNum)analogRead(A_pinMum) 讀取訊號
  • digitalWrite(pinNum, value)
    • HIGH:pin 腳輸出電壓為 5V (analog pin 也可以當 digital pin 輸出)
    • LOW:pin 腳輸出電壓為 GND
  • digitalRead(pinNum):讀取 pin 腳輸入訊號,analog pin 也可以使用
    • HIGH:輸入訊號為 5V
    • LOW:輸入訊號為 GND
  • analogWrite(pinNum, value):讓 pin 腳輸出 PWM 訊號,只有支援 PWM 的 pin 腳才有用
  • analogRead(A_pinNum):讀取 analog pin 輸入的訊號大小,使用 10bit ADC

練習:呼吸燈、按鈕開關 LED

Delay

  • delay(x):暫停 x 毫秒 (ms)
  • delayMicroseconds(x):暫停 x 微秒 (us)

Serial

  • 如何知道開發板運作的狀況? 透過 UART 傳輸特定訊息,或是與開發板溝通
  • Serial.begin(baud rate):設定 UART 傳輸的 baud rate,開啟 serial monitor 也要選擇一樣的 baud rate
/* Output the value of i per second. */
void setup()
{
    Serial.begin(9600);
    // Wait for initialization
    while (!Serial)
        ;
}

int i = 0;
void loop()
{
    Serial.println(i++);
    delay(1000);
}

注意:在 Arduino UNO/MEGA 中 int 是 16 bits、long 是 32 bits、short 是 16 bits、floatdouble 一樣是 32 bits