SlideShare a Scribd company logo
電子工程系車用電子與資訊組
Arduino程式快速入門
吳錫修
sswu@nkut.edu.tw
Revised on July 6, 2018
程式基本架構、程式語法、資料型別與變數、運算
子、條件敘述、迴圈敘述
Makeeachdaycount
/* 註解區 (block comment)
通常在程式檔開頭,簡述程式之功能、作者、設計日期、程式版本等訊息
ShyiShiou Wu; November 5, 2017
*/
// 單行註解 (single line comment)
// 標頭檔區,指定程式使用到的函式庫標頭檔
#include <Servo.h>
#include "config.h"
// 宣告區,程式中使用的全域變數
Servo myservo;
// 設置函式,Arduino重置時,會先執行設置函式,且只會執行⼀次
void setup(){
// put your setup code here, to run once:
Serial.begin(9600);
}
Arduino程式基本架構 1/2
2
Makeeachdaycount
// 程式主廻圈,Arduino會循環執行loop函式
void loop() {
// put your main code here, to run repeatedly:
}
// 其它自訂函式,依功能需求自行設計之函式
void mySub() {
// put your subroutine code here
}
Arduino程式基本架構 2/2
3
Makeeachdaycount
 引入標頭檔 (header file)
 專案資料夾(儲存*.ino的目錄)下的自訂標頭檔
#include "config.h"
 libraries資料夾下的函式庫標頭檔
#include <Servo.h>
 定義常數 (constant)
#define RED_LED 12 //不用等號與分號,通常使用⼤寫字⺟
 宣告變數 (variable)
int lightness = 0; //變數型別 變數名稱 = 初始值;
Arduino程式語法 1/3
4
Makeeachdaycount
 每行指令敘述以分號結尾
int lightness = 0;
 程式碼區分⼤小寫 (case sensitive)
lightness ≠ Lightness
 程式區塊,左右⼤括弧必須成對
void setup() {
...
}
Arduino程式語法 2/3
5
Makeeachdaycount
 自訂副程式 (subroutine)
 沒有回傳值
//void 副程式名稱(參數1, 參數2, ...)
void showMessage() {
//副程式程式碼
...
}
 自訂函式 (function)
 使用return回傳資料
//傳回值型別 函式名稱(參數1, 參數2, ...)
int function_name(int arg1, int arg2) {
//函式程式碼
...
return (arg1 == arg2); //傳回值
}
Arduino程式語法 3/3
6
Makeeachdaycount
 資料型別決定資料容器 (變數) 的格式與容量
 boolean
 只有true或false二種值 (HIGH或LOW)
 char
 字元資料,佔1 byte
 使用單引號標記字元,例如'A'
 也可使用ACSII code,例如65
 做為數值資料時,有效值-128~127
 unsigned char
 1 byte,數值0~255
Arduino Uno資料型別 1/4
7
Makeeachdaycount
 ASCII code
 控制字元
 顯示字元
Arduino Uno資料型別 2/4
8
Makeeachdaycount
 byte
 8-bit無號數,數值0~255
 int
 16-bit整數,-32,768~32,767
 unsigned int
 16-bit無號數,0~65,535
 word
 16-bit無號數,0~65,535
 short
 16-bit整數,-32,768~32,767
Arduino Uno資料型別 3/4
9
Makeeachdaycount
 long
 32-bit整數,-2,147,483,648L~2,147,483,647L
 unsigned long
 32-bit無號數,0~4,294,967,295L
 float
 4 bytes浮點數,最⼤值3.4028235E+38,最小值-3.4028235E+38
 double
 4 bytes倍精準數,同float
Arduino Uno資料型別 4/4
10
Makeeachdaycount
 ⼀般的變數只能儲存⼀個值,陣列則可以用來存放多個值
 陣列中的每個資料稱為陣列元素 (element),程式中以索引值存取陣
列元素,索引值從0開始
byte ledPins[] = {2, 4, 8};
pinMode(ledPins[0], OUTPUT);
pinMode(ledPins[1], OUTPUT);
pinMode(ledPins[2], OUTPUT);
 二維陣列
char keypad[4][4] = { //矩陣按鍵
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
陣列 (array)
11
Makeeachdaycount
 十進制 (decimal)
 123
 二進制 (binary)
 B1111011
 限用於表示8位元數值 (0 to 255)
 八進制 (octal)
 0173
 十六進制 (hexadecimal)
 0x7B
數值資料表示法 1/2
12
Makeeachdaycount
 浮點數
 10.0 10 10
 2.34E5 2.34 * 10^5 234000
 67e-12 67.0 * 10^-12 0.000000000067
數值資料表示法 2/2
13
Makeeachdaycount
 使用雙引號標記字串資料
 char str1[] = "Arduino";
 sizeof(str1)結果為8,會自動加上'0'(空字元)做為字串結束標記
 字串就是⼀種字元陣列 (char array)
 char str2[]={'A', 'r', 'd', 'u', 'i', 'n', 'o', '0'};
字串
14
Makeeachdaycount
 String物件比字元陣列提供更多便利的字串處理運算
 建構元
String str1 = String(235, BIN); //"11101011"
String str2 = String(45, HEX); //"2d"
String str3 = "Hello String"; //"Hello String"
 字串串接
int sensorValue = analogRead(A0);
String msg$ = "Sensor value: " + sensorValue;
 尋找特定字元位置索引
String html_str = "<HTML><HEAD><BODY>";
int i1 = html_str.indexOf('>'); //5
int i2 = html_str.indexOf('>', i1 + 1); //11
int lastOpeningBracket = html_str.lastIndexOf('<'); //12
String物件 1/4
15
Makeeachdaycount
 計算字串⻑度
String hello_str = "Hello! arduio ";
Serial.println(hello_str.length()); //18
hello_str.trim(); //切除字串後面多餘空白
Serial.println(hello_str.length()); //14
 ⼤小寫轉換
String arduino_str = "Arduino programming";
Serial.println(arduino_str); //Arduino programming
arduino_str.toUpperCase();
Serial.println(arduino_str); //ARDUINO PROGRAMMING
arduino_str.toLowerCase();
Serial.println(arduino_str); //arduino programming
String物件 2/4
16
Makeeachdaycount
 替換字串
String hello_str = "Hello! arduio";
Serial.println(hello_str); //Hello! arduino
hello_str.replace("Hello","Hi");
Serial.println(hello_str); //Hi! arduino
 子字串
String head_str = "Content-Type: text/html";
Serial.println(head_str.substring(14, 18)); //text
 移除子字串
String hello_str = "Hi! arduio";
hello_str.remove(2, 6); //Hiino
String物件 3/4
17
Makeeachdaycount
 比較字串
String cmd1_str = "Turn on";
String cmd2_str = "Turn off";
Serial.println(cmd1_str == "Turn on");
Serial.println(cmd2_str.equals("Turn Off"));
Serial.println(cmd2_str.equalsIgnoreCase("Turn Off"));
String物件 4/4
18
Makeeachdaycount
 在程式中用來暫存資料
 宣告變數 (declaring variables)
 語法:資料型別 變數名稱 [= 初始值];
int inputVariable1;
int inputVariable2 = 0;
變數 1/4
19
Makeeachdaycount
 變數命名規則
 只能包含字⺟、數字及底線
 第⼀個字不能是數字
 不能使用保留字
 arduino IDE中保留字會標示顏色 (arduino-1.8.5libkeywords.txt)
 變數注意事項
 區分⼤小寫
 使用有意義的文字組合
 clock_pin
 clockPin 小駝峰式命名法 (lower camel case)
 ClockPin ⼤駝峰式命名法 (upper camel case)
變數 2/4
20
Makeeachdaycount
 有效範圍 (scope)
 全域變數 (global variable),宣告在#include指令之後,提供整份
Arduino程式使用
 區域變數 (local variable),宣告在程式區塊中,只能在所宣告的程式區塊
中使用;每次執行時變數會被動態創建 (create) 和銷毁 (destroy)
int gPWMval; //整個程式檔都可使用gPWMval變數
void setup() {
//...
}
void loop() {
int i, j; //在loop程式區塊中可使用變數i與j
//...
for (int k = 0; k < 100; k++){ //變數k只能在for程式區塊中使用
//...
}
}
變數 3/4
21
Makeeachdaycount
 靜態變數 (static variable)
 在區域變數宣告前加上static關鍵字
 程式執行期間變數內容會保留
void walk(int steps){
static int a; //每次執行walk()函式,變數a會保留上⼀次執行結果
int b; //每次執行walk()函式,變數b會重置
a = a + steps;
b = b + steps;
}
變數 4/4
22
Makeeachdaycount
int a;
void setup() {
Serial.begin(9600);
a = a + 1;
Serial.print("a = ");
Serial.println(a);
func1();
func2();
a = a + 1;
Serial.print("a = ");
Serial.println(a);
func1();
func2();
}
void loop() {
}
void func1() {
int b;
b = a + b;
Serial.print("b in func1 = ");
Serial.println(b);
}
void func2(){
static int b;
b = a + b;
Serial.print("b in func2 = ");
Serial.println(b);
}
Lab 變數測試
23
Makeeachdaycount
 程式執行過程中不會變更的數
 常數名稱通常使用⼤寫字⺟
 #define RED_LED 12
 編譯時,RED_LED會被替換成數字12
 const byte RED_LED = 12;
 保留RED_LED變數,但限定內容不可變動
 內建常數
 INPUT, OUTPUT
 HIGH, LOW
 true, false
常數
24
Makeeachdaycount
 算術運算子 (Arithmetic Operators)
 % (modulo)
 * (multiplication)
 + (addition)
 - (subtraction)
 / (division)
 = (assignment operator)
float r = 3/2; //r=1.0
float r = 3/2.0; //r=1.5
float r = 3/(float)2; //r=1.5
Arduino運算子 1/7
25
Makeeachdaycount
 比較運算子 (Comparison Operators)
 != (not equal to)
 < (less than)
 <= (less than or equal to)
 == (equal to)
 > (greater than)
 >= (greater than or equal to)
Arduino運算子 2/7
26
Makeeachdaycount
 位元運算子 (Bitwise Operators)
 & (bitwise and)
 << (bitshift left)
 >> (bitshift right)
 ^ (bitwise xor)
 | (bitwise or)
 ~ (bitwise not)
Arduino運算子 3/7
27
Makeeachdaycount
 布林運算子 (Boolean Operators)
 ! (logical not)
 && (logical and)
 || (logical or)
Arduino運算子 4/7
28
Makeeachdaycount
 遞增運算
 ++ (increment)
 x++; 等同x = x + 1;
 遞減運算
 -- (decrement)
 x--; 等同x = x - 1;
Arduino運算子 5/7
29
Makeeachdaycount
 複合運算子 (Compound Operators)
 同時執行『算數運算子或位元運算子』及『指定運算子』兩件工作
 += (compound addition)
 x += y; 等同x = x + y;
 -= (compound subtraction)
 x -= y; 等同x = x - y;
 *= (compound multiplication)
 x *= y; 等同x = x * y;
 /= (compound division)
 x /= y; 等同x = x / y;
Arduino運算子 6/7
30
Makeeachdaycount
 &= (compound bitwise and)
 x &= y; 等同x = x & y;
 |= (compound bitwise or)
 x |= y; 等同x = x | y;
 ^= (compound bitwise xor)
 x ^= y; 等同x = x ^ y;
Arduino運算子 7/7
31
Makeeachdaycount
 優先序 (Precedence)
 決定運算的先後次序
 a = b + c * d;
 結合性 (associativity)
 決定相同運算同時出存在時,要由左向右或是由右向左運算
 a = b + c + d;
 最好使用小括號明確設定運算優先序
Precedence & associativity 1/2
32
Makeeachdaycount
運算子 優先序
() 1
!、~、++、−− 2
×、/ 3
+、− 4
>>、<< 5
<、<=、>、>= 6
==、!= 7
^、&、| 8
&& 9
|| 10
= 12
Precedence & associativity 2/2
33
Makeeachdaycount
 循序結構 (Sequence)
 選擇結構 (Selection)
程式的三種基本控制結構 1/2
34
truefalse
Makeeachdaycount
 重複結構 (Iteration)
程式的三種基本控制結構 2/2
35
true
false
Makeeachdaycount
 if … else
if (condition1){
//do Thing A
}
else if (condition2){
//do Thing B
}
else if (condition3){
//do Thing C
}
else {
//do Thing D
}
if敘述 1/2
36
Makeeachdaycount
 範例
byte cmd = Serial.read();
if (cmd == '1') { //從序列埠收到'1'
digitalWrite(LED, HIGH);
Serial.println("Turn LED on");
}
if (cmd == '0') { //從序列埠收到'0'
digitalWrite(LED, LOW);
Serial.println("Turn LED off");
}
if敘述 2/2
37
Makeeachdaycount
 switch…case
switch (var) { //var必須是整數或字元
case label1:
// statements
break;
case label2:
// statements
break;
default:
// statements
}
switch…case敘述 1/2
38
Makeeachdaycount
 範例
byte cmd = Serial.read();
switch (cmd){
case '0':
digitalWrite(LED, LOW);
Serial.println("Turn LED off");
break;
case '1':
digitalWrite(LED, HIGH);
Serial.println("Turn LED on");
break;
}
switch…case敘述 2/2
39
Makeeachdaycount
 for迴圏
for (initialization; condition; increment) {
//statement(s);
}
 範例
byte ledPins[] = {2, 4, 8, 3, 6};
for (int i = 0; i < 5; i++){
pinMode(ledPins[i], OUTPUT);
}
for迴圈敘述 1/3
40
Makeeachdaycount
 while迴圏
while(condition){
// statement(s)
}
 範例
int i=0;
while (i <= 255){
analogWrite(pwmPin, i);
delay(10);
i++;
}
Arduino迴圈敘述 2/3
41
Makeeachdaycount
 do … while迴圏
do {
// statement block
} while (condition);
 範例
int i=0;
do{
analogWrite(pwmPin, i);
delay(10);
i++;
} while (i <= 255);
Arduino迴圈敘述 3/3
42
Makeeachdaycount
 http://www.arduino.cc/reference/en/
Arduino語言參考資料
43

More Related Content

PDF
Arduino基礎IO控制
PDF
Arduino overview
PDF
Arduino序列通訊應用
PDF
Arduino程式除錯
PDF
Arduino藍牙傳輸應用
PDF
Arduino序列通訊
PDF
Arduino感測應用
PDF
Dart のコード自動生成の仕組みと、コード自動生成のパッケージを自作する方法について
Arduino基礎IO控制
Arduino overview
Arduino序列通訊應用
Arduino程式除錯
Arduino藍牙傳輸應用
Arduino序列通訊
Arduino感測應用
Dart のコード自動生成の仕組みと、コード自動生成のパッケージを自作する方法について

What's hot (20)

PDF
2018AOI論壇_如何導入深度學習來提升工業瑕疵檢測技術_工研院賴璟皓
PDF
Arduino應用系統設計 - Arduino程式快速入門
PDF
Profiling PyTorch for Efficiency & Sustainability
PDF
機器人齊步走 mBlock5 mbot_ver8_探奇邱信仁
PDF
Arduino 底層原始碼解析心得
PDF
Python與Ardinio整合應用
PDF
from Source to Binary: How GNU Toolchain Works
PPTX
Arduino 習作工坊 - Lesson 2 動力之夜
PPTX
用Raspberry Pi 學Linux I2C Driver
PDF
mbot2.0教學-超音波感測應用.pdf
PDF
Openwrt startup
PPTX
產業:IGBT產業
PDF
AVR Micro controller Interfacing
PDF
產業+個股_Final.pdf
PDF
mBot教學(4) - 移動控制
PDF
mBot教學(8) - 巡線控制應用
PPTX
新趨勢:Open RAN
PDF
2023年投資展望會-電子產業.pdf
PDF
Lab Manual.pdf
PDF
HC 05藍芽模組連線
2018AOI論壇_如何導入深度學習來提升工業瑕疵檢測技術_工研院賴璟皓
Arduino應用系統設計 - Arduino程式快速入門
Profiling PyTorch for Efficiency & Sustainability
機器人齊步走 mBlock5 mbot_ver8_探奇邱信仁
Arduino 底層原始碼解析心得
Python與Ardinio整合應用
from Source to Binary: How GNU Toolchain Works
Arduino 習作工坊 - Lesson 2 動力之夜
用Raspberry Pi 學Linux I2C Driver
mbot2.0教學-超音波感測應用.pdf
Openwrt startup
產業:IGBT產業
AVR Micro controller Interfacing
產業+個股_Final.pdf
mBot教學(4) - 移動控制
mBot教學(8) - 巡線控制應用
新趨勢:Open RAN
2023年投資展望會-電子產業.pdf
Lab Manual.pdf
HC 05藍芽模組連線
Ad

Similar to Arduino程式快速入門 (20)

PDF
Arduino程式快速入門
PDF
Arduino L2
PDF
Arduino overview
PDF
程式人雜誌 -- 2013 年 2 月號
PDF
Arduino Basic
PDF
邏輯思考與第三語言
PPT
第六章 函數與巨集
DOCX
系統程式 -- 第 11 章 嵌入式系統
PDF
開放硬體認知學習指引
PPTX
LinkIt ONE tutorial #1- Basics
PDF
電子學作業一 利用Arduino玩音樂
PDF
9789572239940 試閱章節
PDF
Arduino Yún使用Http restful api控制io
PDF
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)
PPT
C程式-陣列與指標
PDF
Arduino應用系統設計 - 導論
PDF
程式人雜誌 -- 2013 年 1 月 (創刊號)
PPT
89S51電路板
PDF
學習歷程 期末專題-機器人動手做
PPTX
Chapter 3 basic syntax and operator
Arduino程式快速入門
Arduino L2
Arduino overview
程式人雜誌 -- 2013 年 2 月號
Arduino Basic
邏輯思考與第三語言
第六章 函數與巨集
系統程式 -- 第 11 章 嵌入式系統
開放硬體認知學習指引
LinkIt ONE tutorial #1- Basics
電子學作業一 利用Arduino玩音樂
9789572239940 試閱章節
Arduino Yún使用Http restful api控制io
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)
C程式-陣列與指標
Arduino應用系統設計 - 導論
程式人雜誌 -- 2013 年 1 月 (創刊號)
89S51電路板
學習歷程 期末專題-機器人動手做
Chapter 3 basic syntax and operator
Ad

More from 吳錫修 (ShyiShiou Wu) (20)

PDF
Vuforia AR影片程式設計
PDF
micro:bit亮度感測應用
PDF
Vuforia AR 同時追踨多張辨識圖
PDF
micro:bit開關控制應用
PDF
Vuforia AR 應用程式設計入門
PDF
Vuforia AR 應用程式準備作業
PDF
micro:bit LED顯示控制
PDF
IDE for micro:bit
PDF
Microbit 1 introduction
PDF
使用Makeblock App學習mBot程式設計
PDF
使用M部落App學習mBot程式設計
PDF
nodeMCU IOT教學03 - NodeMCU導論
PDF
nodeMCU IOT教學02 - Lua語言
PDF
Unity遊戲程式設計 - 2D Platformer遊戲
PDF
Unity遊戲程式設計 - 2D移動與碰撞處理II
PDF
Unity遊戲程式設計 - 2D運動與碰撞處理I
PDF
Python與Ardinio整合應用
PDF
mBlock積木式設計程式
PDF
Arduino程式除錯
PDF
Unity遊戲設計- 2D動畫製作及應用
Vuforia AR影片程式設計
micro:bit亮度感測應用
Vuforia AR 同時追踨多張辨識圖
micro:bit開關控制應用
Vuforia AR 應用程式設計入門
Vuforia AR 應用程式準備作業
micro:bit LED顯示控制
IDE for micro:bit
Microbit 1 introduction
使用Makeblock App學習mBot程式設計
使用M部落App學習mBot程式設計
nodeMCU IOT教學03 - NodeMCU導論
nodeMCU IOT教學02 - Lua語言
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D移動與碰撞處理II
Unity遊戲程式設計 - 2D運動與碰撞處理I
Python與Ardinio整合應用
mBlock積木式設計程式
Arduino程式除錯
Unity遊戲設計- 2D動畫製作及應用

Arduino程式快速入門