在用高階應用時,控制板基本擔當着執行"當發生XXX時,就做YYY動作"這種工作。
而做這類當XXX時,就YYY的應用時﹐我們需要用到if()這個指令,他的語法是這樣:
if(/*條件A*/){
/*結果A*/
}
如果乎合條件A的內容,就執行結果A的內容,如果不乎合條件A就什麼都不做。
例子
int x = 0; //設定變數x,其初始值為0
void setup() {
Serial.begin(9600);
delay(1000);
}
void loop() {
if (x==1){ //留意不要寫成x=1, 是x==1
Serial.println(x);}
}
結果會因為x是0,不合乎if內的條件而什麼都不會發生。
* 在Arduino內,x=1的意思是把x(左方)設定成1(右方),而x==1則是判斷x(左方)是否和1(右方)是一樣的。
在設定條件時,多用"大於"、"小於"、"等於"等運算符號,我們稱為比較運算符 Comparison Operators。常用的運算符可以按右方箭咀查看
例子
int x = 0; //設定變數x,其初始值為0
void setup() {
Serial.begin(9600);
delay(1000);
}
void loop() {
x++;
if (x>=10){ //留意不要寫成x=1, 是x==1
Serial.println(x);}
}
結果當x大於等於10後才會開始打印數字
而要做"乎合XXX時,就做YYY,不乎合就做ZZZ"的情況﹐我們會在if()後加上else使用,他的語法是這樣:
if()這個指令也有,他的語法是這樣:
if(/*條件A*/){
/*結果A*/
}else{
/*結果B*/
}
如果乎合條件A的內容,就執行結果A的內容,如果不乎合條件A就執行結果B的內容。
例子
int x = 0; //設定變數x,其初始值為0
void setup() {
Serial.begin(9600);
delay(2000);
}
void loop() {
x++;
if (x%2==0){ //如果(除2時餘數為0)
Serial.println(x);
delay(1000);}
else{
Serial.println("Not even number");
delay(1000);}
}
結果為如果x是雙數(除2時餘數為0),就會打印數字,否則就打印"Not even number"。
在了解了if的基本用法後,我們可以加入控制元件(Input component)進行一些進階的應用。
上兩章我們學習了如何令LED亮燈,當中我們用到了指令 pinMode(/*訊號腳*/,OUTPUT);
因為LED屬於由控制板供電控制、並會作出反應的零件,我們稱之為OUTPUT類零件,像下圖右方的馬達、喇叭、顯示屏等零件都屬於OUTPUT類零件
而像上圖左方的開關、按鈕、電阻等零件都屬於輸入訊號給Arduino控制板的零件,即INPUT類零件。這個章節我們會學習使用這類INPUT零件中最簡單的按鈕開關。
在IS擴充板上已經預先連接了若干個I/O零件,工欲善其事,可以按圖片下方的箭頭先了解一下上面有哪些零件。
按照類型上方的零件可以分Input和Output 2類、其對應的腳位可以參考下圖:
*要注意的是A開頭的腳位(buzzer峰鳴器,可變電阻等)需要另加上一顆Jumper零件才能使用。這些零件會在日後的章節再介紹。
今章我們主要會用在擴充板右上方的RGB三色按鈕進行練習。
在按鈕的上方標明了3個按鈕各位連接的腳位,分別為2號、3號和6號腳
要使用開關我們要先了解開關的連接原理。開關的連接如下圖所示,左方的情況在我們未按下開關時,只有上方的正極則通過電阻通到Input腳位,我們稱這個電阻為上拉電阻,因為它把Input的電位拉高成高電位(1)了。而當我們按下開關後(右圖),下方的負極直接連到Input腳位,這時候Input的電位就是低電位(0)了。
讀取輸人訊號主要會使用這個指令:
digitalRead(/*Input pin*/);
digitalRead()和digitalWrite()相反,它會讀取括號內的腳位(pin)訊號,以1(HIGH/高電位)或0(LOW/低電位)的方式表示出來。
先用digitalRead()做個簡單的練習:即時顯示由紅色按鈕接收到的訊號。
int buttonpin=2;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print("The signal of the button is ");
Serial.println(digitalRead(buttonpin));
}
在1a題目的基礎上,同時顯示3個按鈕的訊號吧!
int buttonpin_R=2,buttonpin_G=3,buttonpin_B=6;
int State_R, State_G, State_B; //加入State的變數代替digitalRead();
void setup(){
Serial.begin(9600);
}
void loop(){
State_R=digitalRead(buttonpin_R); //加入State的變數代替digitalRead();
State_G=digitalRead(buttonpin_G);
State_B=digitalRead(buttonpin_B);
Serial.print("The signal of the button RED: ");
Serial.print(State_R);
Serial.print(" Green: ");
Serial.print(State_G);
Serial.print(" Blue: ");
Serial.print(State_B);
Serial.println(" ");
}
加入if的判斷:當按下紅色按鈕,讓紅色燈亮起來、放手時關燈。
int buttonpin_R=2;
int LEDpin_R=__;
int State_R;
void setup(){
pinMode(LEDpin_R,OUTPUT);
}
void loop(){
State_R=digitalRead(buttonpin_R); //加入State的變數代替digitalRead();
if (State_R == _____ ){ //What is the button state at normal(Not pressed)?
digitalWrite(LEDpin_R,____);} //What is the state of the LED at normal(Not pressed)?
else {
digitalWrite(LEDpin_R, ____);} //What is the state of the LED when we press the button?
}
當按下RGB各種顏色的按鈕時,就讓各自對應顏色的燈亮起來、放手時就關掉到應顏色的燈。
int buttonpin_R=2,buttonpin_G=3,buttonpin_B=6;
int LEDpin_R=__,LEDpin_G=__,LEDpin_B=__;
int State_R, State_G, State_B;
void setup(){
pinMode(LEDpin_R,OUTPUT);
pinMode(LEDpin_G,______);
pinMode(________,______);
}
void loop(){
State_R=digitalRead(buttonpin_R); //加入State的變數代替digitalRead();
State_G=digitalRead(buttonpin_G);
State_B=digitalRead(buttonpin_B);
if (State_R == _____ ){ //What is the button state at normal(Not pressed)?
digitalWrite(LEDpin_R,____);} //What is the state of the LED at normal(Not pressed)?
else {
digitalWrite(LEDpin_R, ____);} //What is the state of the LED when we press the button?
if (State_G == _____ ){ //What is the button state at normal(Not pressed)?
digitalWrite(LEDpin_G,____);} //What is the state of the LED at normal(Not pressed)?
else {
digitalWrite(LEDpin_G, ____);} //What is the state of the LED when we press the button?
if (State_B == _____ ){ //What is the button state at normal(Not pressed)?
digitalWrite(LEDpin_B,____);} //What is the state of the LED at normal(Not pressed)?
else {
digitalWrite(LEDpin_B, ____);} //What is the state of the LED when we press the button?
}
完成這章表示你已掌握了基本的控制應用/條件設定了。
在下一章節我們會學習Analog & Digital,讓輸入及輸出可以做出更加細微的變化:1.4 類比與數位-Analog & Digital