#02 認識Arduino 程式架構

本章節內容若要詳細閱讀內容,請至Arduino網站查詢(https://www.arduino.cc/reference/en/)

Arduino IDE編輯器是常用的C語言編譯器,Arduino IDE編輯器特色體積小、使用C語言編寫以及支援其他相容的MCU開發版,如ESP8266、ESP32…等開發版,非常受歡迎的工具。

Arduino IDE下載處 https://www.arduino.cc/en/Main/Software

Arduino是一個開放原始碼的組織,提供開發設計人員建立電路雛形,允許使用者建立功能互動的元件分享,他們是需要使用滿意度高的用戶進行贊助,繼續推動開放原始碼各項專案,因此下載Arduino IDE的編輯器過程中,會出現讓使用者下載卡關的選項,若有餘力者就就進行實質的贊助貢獻,如果只是學習可以直接繞道下載。

安裝過程不多做贅述,建議完整安裝並信任Arduino所發行的程式選項勾選,可以避免不必要的錯誤問題。

安裝完成後,執行Arduino的程式編輯器,開啟範例了解程式的架構。

上圖的範例是讓Arduino 開發版內建LED燈閃爍的效果,許多使用者會將這個範例視為判定手上的Arduino 開發板好壞的方法。以下是程式的範例:

/*
  Blink

  Turns 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, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Blink
*/

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

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

Arduino C語言架構

在上頭的範例程式片段清楚可以看見兩個副程式,分別是: setup() 與 loop(),兩個差異點說明如下:

  • setup() 當Arduino開發板送電啟動會產生開機的事件,自動引導執行setup副程式。
  • loop() Arduino持續工作期間的觸發事件,周而復始會持續不斷執行loop副程式。

Arduino 的事件管理不光只有setup() 與 loop()兩個,還有其他的事件觸發程序,例如:實體序列埠通訊(rs232)事件 serialEvent()、硬體中斷事件捕捉 attachInterrupt(),如下簡圖所示。這些相關的事件後續再進行補充說明。

Arduino C語言的變數與型態

  • 全域變數(Public):生命週期與系統同存亡,任何副程式都可以使用已經宣告的變數內容。
  • •私有變數(Private):變數存活時間僅限於程式所執行函數內,當離開函數後,該變數內容立即清除,私有變數無法跨副程式調用。
/* 全域變數範例 */
//宣告一個全域變數
int status = 0;

void setup()
{
  //程式碼
  status = 1; //設定變數內容=1
}
void loop()
{
  //程式碼
  status ++; //每次執行loop()事件,status的內容+1
}

/* 私有變數範例 */
void setup()
{
  int status = 0; //宣告setup()副程式內的變數
//程式碼
}
void loop()
{
  //程式碼
  int status = 1; //宣告loop()副程式內的變數,雖然變數名稱相同,但彼此之間無關聯
}

常見的變數型態

資料型態 說明 記憶體長度 數值範圍
 boolean 布林 8 bits true (1, HIGH), false(0, LOW)
 byte 位元組 8 bits 0~255
 char 字元 8 bits -128~127
 short  短整數 16 bits -32768~32767
 int 整數 16 bits -32768~32767
 word 字 16 bits 0~65535
 long 長整數 32 bits -2147483648~2147483647
 float 浮點數 32 bits +-3.4028235e+38
 double 倍精度浮點數 32 bits +-3.4028235e+38

變數宣告的方法

int Name;
int Name = 0;
char Name = 'c';
float pi = 3.1415;

常用的Arduino C流程控制

C語言常用的流程控制:

  • if… else… :當條件式為 true(真)時,就執行成立敘述區塊,當條件式為 false(假)時,則執行不成立敘述區塊。
  • for 迴圈:有條件計數迴圈。
  • do… while迴圈:做完do迴圈後,while條件為”true”再次進入do迴圈。
  • while迴圈:while條件為”true”,則進入while迴圈。
  • switch… case… 條件選擇:switch一個變數,然後透過case比對內容,內容符合時會進入case流程。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *