Weitere ähnliche Inhalte Ähnlich wie Arduino程式快速入門 (20) Mehr von 吳錫修 (ShyiShiou Wu) (20) Arduino程式快速入門2. 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
4. Makeeachdaycount
引入標頭檔 (header file)
專案資料夾(儲存*.ino的目錄)下的自訂標頭檔
#include "config.h"
libraries資料夾下的函式庫標頭檔
#include <Servo.h>
定義常數 (constant)
#define RED_LED 12 //不用等號與分號,通常使用⼤寫字⺟
宣告變數 (variable)
int lightness = 0; //變數型別 變數名稱 = 初始值;
Arduino程式語法 1/3
4
6. 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
7. 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
11. 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
14. Makeeachdaycount
使用雙引號標記字串資料
char str1[] = "Arduino";
sizeof(str1)結果為8,會自動加上'0'(空字元)做為字串結束標記
字串就是⼀種字元陣列 (char array)
char str2[]={'A', 'r', 'd', 'u', 'i', 'n', 'o', '0'};
字串
14
15. 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
16. 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
17. 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
18. 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
20. Makeeachdaycount
變數命名規則
只能包含字⺟、數字及底線
第⼀個字不能是數字
不能使用保留字
arduino IDE中保留字會標示顏色 (arduino-1.8.5libkeywords.txt)
變數注意事項
區分⼤小寫
使用有意義的文字組合
clock_pin
clockPin 小駝峰式命名法 (lower camel case)
ClockPin ⼤駝峰式命名法 (upper camel case)
變數 2/4
20
21. 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
22. 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
23. 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
25. 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
26. 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
27. Makeeachdaycount
位元運算子 (Bitwise Operators)
& (bitwise and)
<< (bitshift left)
>> (bitshift right)
^ (bitwise xor)
| (bitwise or)
~ (bitwise not)
Arduino運算子 3/7
27
30. 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
31. 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
32. Makeeachdaycount
優先序 (Precedence)
決定運算的先後次序
a = b + c * d;
結合性 (associativity)
決定相同運算同時出存在時,要由左向右或是由右向左運算
a = b + c + d;
最好使用小括號明確設定運算優先序
Precedence & associativity 1/2
32
36. 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
37. 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
39. 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
42. 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