【程式設計篇】多腳位定義的技巧- 上篇

讀者開啟 Arduino IDE 開發環境後,我們攥寫一段程式,如下表所示之流水燈範例程式,開發板接上八組 LED 燈之後,將程式編譯完成後,上傳到開發板進行測試。

表 2 流水燈範例程式

流水燈範例程式(Water_light)

#define Led1_Pin 4

#define Led2_Pin 5

#define Led3_Pin 6

#define Led4_Pin 7

#define Led5_Pin 8

#define Led6_Pin 9

#define Led7_Pin 10

#define Led8_Pin 11

 

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin Blink_Led_Pin as an output.

  pinMode(Led1_Pin, OUTPUT);    //定義Blink_Led_Pin為輸出腳位

  pinMode(Led2_Pin, OUTPUT);    //定義Blink_Led_Pin為輸出腳位

  pinMode(Led3_Pin, OUTPUT);    //定義Blink_Led_Pin為輸出腳位

  pinMode(Led4_Pin, OUTPUT);    //定義Blink_Led_Pin為輸出腳位

  pinMode(Led5_Pin, OUTPUT);    //定義Blink_Led_Pin為輸出腳位

  pinMode(Led6_Pin, OUTPUT);    //定義Blink_Led_Pin為輸出腳位

  pinMode(Led7_Pin, OUTPUT);    //定義Blink_Led_Pin為輸出腳位

  pinMode(Led8_Pin, OUTPUT);    //定義Blink_Led_Pin為輸出腳位

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

 

}

 

// the loop function runs over and over again forever

void loop() {

  // Step 0

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

  // Step 1

  digitalWrite(Led1_Pin, HIGH);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

    // Step 2

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, HIGH);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

    // Step 3

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, HIGH);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

      // Step 4

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, HIGH);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

    // Step 5

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, HIGH);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

      // Step 6

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, HIGH);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

      // Step 7

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, HIGH);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

    // Step 8

  digitalWrite(Led1_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led2_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led3_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led4_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led5_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led6_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led7_Pin, LOW);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  digitalWrite(Led8_Pin, HIGH);    // 將腳位Blink_Led_Pin設定為低電位  turn the LED off by making the voltage LOW

  delay(1000);              //休息1秒 wait for a second 

}

程式下載:https://github.com/brucetsao/arduino_Programming_Trick

讀者也可以在作者 YouTube 頻道(https://www.youtube.com/user/UltimaBruce )中,在網址https://www.youtube.com/watch?v=3eeIpdJVJoE&feature=youtu.be ,看到本次實驗-流水燈範例程式結果畫面。

下一頁還有更多精彩教學喔 !

曹永忠
作者

曹永忠,國立中央大學資訊管理學系博士,目前在暨南大學電機工程學系兼任助理教授與自由作家,專注於軟體工程、軟體開發與設計、物件導向程式設計......並持續發表作相關專業著作。

使用 Facebook 留言
發表回應
謹慎發言,尊重彼此。按此展開留言規則