Arduino從基礎到實踐第三章練習題

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   width   for   re   

先寫在這裡,還沒經過測試。

1. LED兩端往中間移動,到中間後向兩邊返回。

 1 // adr301.ino 2  3 byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; 4 int ledDelay(65); 5 int direction = 1; 6 int currentLED = 0; 7 unsigned long changeTime; 8  9 void setup() {10     for(int i=0; i<10; i++){11         pinMode(ledPin[i], OUTPUT);12     }13 14     changeTime = millis();15 }16 17 void loop() {18     if((millis() - changeTime) > ledDelay){19         changeLED();20         changeTime = millis();21     }22 }23 24 void changeLED() {25     for(int i=0; i<10; i++){26         digitalWrite(ledPin[i], LOW);27     }28 29     digitalWrite(ledPin[currentLED], HIGH);30     digitalWrite(ledPin[10 - 1 - currentLED], HIGH);31 32     currentLED += direction;33 34     if(currentLED == 4){35         direction = -1;36     }37 38     if(currentLED == 0){39         direction = 1;40     }41 }

添加結果視頻

2. LED彈跳球

 1 // adr302.ino 2  3 byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; 4 int ledDelay(65); 5 int direction = 1; 6 int currentLED = 0; 7 int maxHeight = 9; 8 unsigned long changeTime; 9 10 void setup() {11     for(int i=0; i<10; i++){12         pinMode(ledPin[i], OUTPUT);13     }14 15     changeTime = millis();16 }17 18 void loop() {19     if((millis() - changeTime) > ledDelay){20         changeLED();21         changeTime = millis();22     }23 }24 25 void changeLED() {26     for(int i=0; i<10; i++){27         digitalWrite(ledPin[i], LOW);28     }29 30     digitalWrite(ledPin[currentLED], HIGH);31 32     currentLED += direction;33 34     if(currentLED == maxHeight){35         direction = -1;36         maxHeight -= 1;37     }38 39     if(currentLED == 0){40         direction = 1;41         maxHeight = 9;42     }43 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.