Chapter 3 exercise questions of Arduino from Foundation to practice

Source: Internet
Author: User

I wrote it here, but it hasn't been tested yet.

1. Move the two ends of the led to the center and return to the two sides.

 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 }

Add result video

2. Led bounce ball

 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 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.