文章程式碼顯示

2017年12月22日 星期五

《入門》寫程式Arduino教學 - 09 : 實作練習:跑馬燈、紅綠燈

經由前面的練習,我們來實作一個有趣的應用,分別是跑馬燈以及紅綠燈

跑馬燈(影片連結)

我們用一個很直觀的方式來設計這個跑馬燈


int LED1 = 13;

int LED2 = 12;

int LED3 = 11;

int LED4 = 10;

int LED5 = 9;

int LED6 = 8;

int LED7 = 7;

int LED8 = 6;

int delayTime = 200; //延遲 200ms



void setup(){

  pinMode(LED1,OUTPUT);

  pinMode(LED2,OUTPUT);

  pinMode(LED3,OUTPUT);

  pinMode(LED4,OUTPUT); 

  pinMode(LED5,OUTPUT); 

  pinMode(LED6,OUTPUT); 

  pinMode(LED7,OUTPUT); 

  pinMode(LED8,OUTPUT); 

}



void loop(){

  INI();

  

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,LOW);

  digitalWrite(LED3,LOW);

  digitalWrite(LED4,LOW);

  digitalWrite(LED5,LOW);

  digitalWrite(LED6,LOW);

  digitalWrite(LED7,LOW);

  digitalWrite(LED8,LOW);

  delay(delayTime);

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,LOW);

  digitalWrite(LED4,LOW);

  digitalWrite(LED5,LOW);

  digitalWrite(LED6,LOW);

  digitalWrite(LED7,LOW);

  digitalWrite(LED8,LOW);

  delay(delayTime);

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,LOW);

  digitalWrite(LED5,LOW);

  digitalWrite(LED6,LOW);

  digitalWrite(LED7,LOW);

  digitalWrite(LED8,LOW);

  delay(delayTime);

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,HIGH);

  digitalWrite(LED5,LOW);

  digitalWrite(LED6,LOW);

  digitalWrite(LED7,LOW);

  digitalWrite(LED8,LOW);

  delay(delayTime);

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,HIGH);

  digitalWrite(LED5,HIGH);

  digitalWrite(LED6,LOW);

  digitalWrite(LED7,LOW);

  digitalWrite(LED8,LOW);

  delay(delayTime);

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,HIGH);

  digitalWrite(LED5,HIGH);

  digitalWrite(LED6,HIGH);

  digitalWrite(LED7,LOW);

  digitalWrite(LED8,LOW);

  delay(delayTime);

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,HIGH);

  digitalWrite(LED5,HIGH);

  digitalWrite(LED6,HIGH);

  digitalWrite(LED7,HIGH);

  digitalWrite(LED8,LOW);

  delay(delayTime);

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,HIGH);

  digitalWrite(LED5,HIGH);

  digitalWrite(LED6,HIGH);

  digitalWrite(LED7,HIGH);

  digitalWrite(LED8,HIGH);

  delay(delayTime);

  INI();

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,HIGH);

  digitalWrite(LED5,HIGH);

  digitalWrite(LED6,HIGH);

  digitalWrite(LED7,HIGH);

  digitalWrite(LED8,HIGH);

  delay(delayTime);

  INI();

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,HIGH);

  digitalWrite(LED5,HIGH);

  digitalWrite(LED6,HIGH);

  digitalWrite(LED7,HIGH);

  digitalWrite(LED8,HIGH);

  delay(delayTime);

  INI();

  digitalWrite(LED1,HIGH);

  digitalWrite(LED2,HIGH);

  digitalWrite(LED3,HIGH);

  digitalWrite(LED4,HIGH);

  digitalWrite(LED5,HIGH);

  digitalWrite(LED6,HIGH);

  digitalWrite(LED7,HIGH);

  digitalWrite(LED8,HIGH);

  delay(delayTime);

}



void INI(){

  digitalWrite(LED1,LOW);

  digitalWrite(LED2,LOW);

  digitalWrite(LED3,LOW);

  digitalWrite(LED4,LOW);

  digitalWrite(LED5,LOW);

  digitalWrite(LED6,LOW);

  digitalWrite(LED7,LOW);

  digitalWrite(LED8,LOW);

  delay(delayTime);

}


紅綠燈(影片連結)

int LED_R = 13;
int LED_Y = 12;
int LED_G = 11;
int LED_R1 = 10;
int LED_Y1 = 9;
int LED_G1 = 8;

void setup(){
  pinMode(LED_R,OUTPUT);
  pinMode(LED_Y,OUTPUT);
  pinMode(LED_G,OUTPUT);
  pinMode(LED_R1,OUTPUT);
  pinMode(LED_Y1,OUTPUT);
  pinMode(LED_G1,OUTPUT);
}

void loop(){
  INI();
  
  digitalWrite(LED_G,HIGH);
  digitalWrite(LED_R1,HIGH);
  delay(5000);
  digitalWrite(LED_G,LOW);
  digitalWrite(LED_Y,HIGH);
  delay(1000);
  digitalWrite(LED_Y,LOW);
  digitalWrite(LED_R,HIGH);
  digitalWrite(LED_R1,LOW);
  digitalWrite(LED_G1,HIGH);
  delay(5000);
  digitalWrite(LED_G1,LOW);
  digitalWrite(LED_Y1,HIGH);
  delay(1000);
}

void INI(){
  digitalWrite(LED_R,LOW);
  digitalWrite(LED_Y,LOW);
  digitalWrite(LED_G,LOW);
  digitalWrite(LED_R1,LOW);
  digitalWrite(LED_Y1,LOW);
  digitalWrite(LED_G1,LOW);
}


由這兩個例子我們會發現,雖然使用了定義變量以及自幾建立一個函式的功能,已經將程式的可讀性上升不少,但整體的程式還是變得很冗長。這是因為我們還沒有學到「陣列」的觀念而導致的,但我們不需要急著去學陣列(陣列的資訊可洽此連結),因為初學的過程中使用的機率不高,目前就先用這種直觀的方式來進行練習就可以了。


電路部分可以參考以下

↓↓↓ 連結到部落格方針與索引 ↓↓↓

Blog 使用方針與索引