2016年1月2日 星期六

[Arduino] Ex1_1:一閃一閃亮晶(Blink)

Ex1_1:一閃一閃亮晶(Blink)
Ted Lee@CAVEDU
Jan. 2, 2016
88x31.png[3]

本文從Arduino專案開發前的工作环境建置、測試,引入了毋需接電路(Lab1_1_1)及需另接電路(Lab1_1_2)兩個實驗來引領學員邁進Arduino的神秘殿堂,並援用了Arduino IDE內建範例集中的Blink來介紹Arduino控制程式的基本架構及語法。
本範例过関後,可獲得LED灯光Blink卡Arduino程式架構卡各一張。

難易度:易

材料:
  1. LED(Light-Emitting Diode,發光二極体)一顆
  2. 220W電阻一顆(色环:紅紅棕金)
  3. 杜邦線若干條
  4. 麵包板一塊
  5. Arduino Uno R3一塊(本系列文章皆簡稱為Arduino開發版
  6. A-B型USB傳輸線一條

Arduino開發环境建置
  1. Arduino的官網(official website)右側选擇與目前電腦中安裝的作業系統(Operating System)相符的版本[2]。
  2. 雙撃(double click)步驟1下載完成的「arduino-1.6.7-windows.exe」進行安裝。
  3. 在安裝的过程也會順帶將開發版的USB驅動程式(driver)一併裝妥。
  4. 选擇要安裝的目錄。
  5. 從File®Preferences中調整Arduino IDE(Integrated Development Environment)环境,設定顯示字型為14、標示程式碼的行号(line number)
  6. 將開發版以A-B型USB連接線串接電腦與開發版。
  7. 設定開發版的型号(board model)通信使用的埠号(port number)

    1. 從功能表Tools的Board: "Arduino/Genuino Uno"选擇我們使用的開發Arduino/Genuino Uno
    2. 設定板子和電腦的通信Port為COMx(編號x會因不同電腦及不同USB埠而配置(allocate)到不同的數值。一般而言,也就是說您和隔壁學員所「抽到」的號碼牌x不會同号,這是正常的情形喔,Don't panic!)。
  8. 將Blink.ino燒錄到開發版上。

這個範例有兩個實驗:

Lab1_1_1:奌亮內建(on-board)LED(使用Arduino第13腳內部連接的LED)
Lab1_1_2:奌亮外接LED(Arduino第13腳串接一限流電阻與LED)

從以下的Arduino開發版的電路图(schematic diagram)中,可以看到的第13腳(藍框)同時接到LED(綠框)及輸出。所以這兩個實驗都用同一接腳來奌亮LED,控制程式亦為同一支:Blink.ino。













Lab1_1_1
第一個實驗將以Arduino IDE內建的範例程式集(built-in examples)中的Blink(註一)做為踏入Arduino殿堂的第一支程式,同時也測試一下前面設定了老半天的环境是否可以正確地誏我們開發程式(註二)。
開啟Blink後的程式碼(註三)為:
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

程式呼叫(invoke)了Arduino內建(built-in)API(Application Programming Interface,應用程式介面)中的三個函式:pinMode()digitalWrite()delay()來完成任務(註四)。

程式流程圖(flow chart)為:
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}


執行結果

註一:從範例中學習(learning by examples)[1]是學習寫程式(programming)最快的方法之一,筆者強烈推荐!
註二:大抵上,軟、硬体的專案(project)開發流程為:
Arduino程式的除錯技巧可参閱使用Autodesk 的123D Circuits來玩Arduino。
註三:Arduino程式(xxx.ino)的基本架構(structure)由兩個副程式(subroutines)所組成:
  1. setup():程式執行前的設定(setting),稱之為初始化(initialize),例如:設定串列傳輸速率為9,600bps(bit per second)
  2. loop():不斷重複執行(repeatedly execution)的副程式控制碼(control code),例如:控制馬達不停轉動。
void setup() {
     
}
void loop() {
 
}
註四:Arduino程式的語法(syntax)類C(C-like)高階程式語言(high-level programming language)。想流暢地駕馭程式創作,需要了解:
  1. 了解Arduino的基本語法 程式語言的語法好比英文的文法(grammar),一定要按照規定的格式(format)表述才能誏呆呆的電腦理解我們要交付的任務,例如:除了特殊指令(像if、while…)外,每條指令都一定要以;表程結尾。
  2. 學會查Arduino API文件 在學習Arduino專案程式開發的过程中,我們必需時時對這個官方提供的API有一定程度的了解:
    • API的架構(structure) 筆者將之分成以下六大類:
      1. I/O(Input/Output,輸入/輸出)
        1. 數位(Digital I/O)
        2. 類比(Analog I/O)
        3. 進階(Advanced I/O)
      2. 時間(Time)
      3. 數學(Math)
        1. 三角函數(Trigonometry)
        2. 乱數(Random Numbers)
      4. 字串(Strings)
        1. 字元(Characters)
        2. 位元(Bits)及位元組(Bytes)
      5. 中斷(Interrupts)
        1. 內部中斷(Internal Interrupts)
        2. 外部中斷(External Interrupts)
      6. 通信(Communication)
    • API函式的功能 例如:digitalWrite()是將高/低電位(HIGH/LOW)值輸出到Arduino開發版。
    • API函式的參數(parameter/argument) 以digitalWrite(pin,value)來說,它使用兩個参數來將value值輸出到Arduino開發版的pin腳位
Lab1_1_2
接線图.fzz)及接線影片
執行結果

参考資料:
  1. Learning Programming Fast In Just 5 Steps.
  2. Getting Started with Arduino on Windows.
  3. 六種授權條款