2017.01.06 13:30

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

ADVERTISEMENT

本篇為『MAKER 系列』系列之程式設計篇子系列第三篇:多腳位定義的技巧(下篇),乃是多腳位定義的技巧(上篇)之下集。筆者本系列是針對非資訊、電機、電子等 Makers 攥寫的系列,這三、四年來在 Maker 領域寫書、發表文章、辦展、授課,常遇到許多學子訓練不足,以交作業的心態來學習,並沒有把程式底子打好。

文 / 曹永忠、郭晉魁、吳佳駿、許智誠、蔡英德

上篇中談到在 Arduino 開發版程式設計之中,同時使用多個GPIO非常常見,但是GPIO腳位的變動更是頻繁,一旦 GPIO 腳位變動,相對應的程式必須要相對變更,所以使用陣列變數來替代單一變數Define宣告,(曹永忠, 吳佳駿, 許智誠, & 蔡英德, 2016a, 2016b, 2016c),對陣列變數控制與使用,最更進一步介紹與深入。

ADVERTISEMENT

本文內容希望透過筆者的經驗,一步一步分享筆者解決這樣問題的經驗,透過這樣練習,讓讀者可以養成正確有效的寫作習慣,避免往後無所謂的臭蟲(Bugs)產生。

本系列希望貢獻筆者一些經驗,讓非資訊、電機、電子等 Makers 可以學到一些程式開發的技巧、原理、法則與穩固的技術,因本系列文章主要讀者為初學者,內容程度為基礎入門程度,深入之處不足,但請高手們給筆者賜教,也請讀者關注本系列。

使用燈號狀態簡化程式

如下圖所示,我們發現我們使用八個 Leds 燈,而燈號成為連續數字,所以我們如果使用陣列變數來代表腳位,可以更有效管理這些腳位。

ADVERTISEMENT


圖 1 流水燈連接電路圖

我們在上表:使用陣列與迴圈設定腳位程式,我們用陣列與迴圈來簡化腳位的設定與初始化,但我們在流水燈範例程式中,流水燈的動作還未寫出來,如下表所示。我們先看看流水燈範例程式中燈號動作的程式

表 1 流水燈動作程式

ADVERTISEMENT

步驟

程式碼

第一燈

  // 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 

}

在上表所示中,我們先看看流水燈範例程式中燈號動作的程式碼(紅色字、底線為主),只有這個燈號動作的程式碼中,將腳位都設成高電位(HIGH),其餘都是低電位(LOW),如果我設一個燈號的變數,來讓系統知道目前所亮的燈號,或許就可以知道如何控制燈號

int NowLedOn = 0 ;

我們再透過迴圈來處理八個燈號,如下列程式:

       for (int i =0 ; i < 8; i++)

          {

 

 

          }

 

然而我們並不知道在迴圈裡面,要亮哪個登,我們再將下表控制燈號亮與滅的if判斷式,加在迴圈程式裡。

ADVERTISEMENT

表 2 控制燈號亮與滅

              if (NowLedOn == i)

              {

                digitalWrite(LedPins[i], HIGH);   // turn on Led

              }

              else

              {

                digitalWrite(LedPins[i], LOW);    // turn off Led

              }

我們發現,六十四行的程式簡化成四行,依樣達到相同效果。

我們將使用陣列與迴圈控制燈號程式,上傳到開發板進行測試。

請接下一頁精彩教學唷 ~

ADVERTISEMENT