【Arduino】相关实现

LED

class LED {
public:
    LED(int c_leds[], int c_n) {
        leds = c_leds;
        n = c_n;
    };
    void bullet(int speed, char direction) {
        int k = n, t = 0;
        //从左至右
        if (direction == 'L') {
            for (int i = 0;i < n;i++) {
                for (int j = 0;j < k - 1;j++) {
                    digitalWrite(leds[j], HIGH);
                    delay(1000 - speed);
                    digitalWrite(leds[j], LOW);
                }
                digitalWrite(leds[(k--) - 1], HIGH);
            }

            //清屏
            for(int i=0;i<n;i++){
               digitalWrite(leds[i], LOW);
            }
            //从右至左
        }
        else if (direction == 'R') {
            for (int i = 0;i < n;i++) {
                for (int j = k - 1;j >= t;j--) {
                    digitalWrite(leds[j], HIGH);
                    delay(1000 - speed);
                    digitalWrite(leds[j], LOW);
                }
                digitalWrite(leds[t++], HIGH);
            }
            //清屏
            for(int i=0;i<n;i++){
               digitalWrite(leds[i], LOW);
            }
            
            
        }
    }

    void waterfall(int speed, char direction) {

        //从左至右
        if (direction == 'L') {
            for (int i = 0;i < n;i++) {
                digitalWrite(leds[i], HIGH);
                delay(1000 - speed);
                digitalWrite(leds[i], LOW);
            }
            //从右至左
        }
        else if (direction == 'R') {
            for (int i = n;i >= 0;i--) {
                digitalWrite(leds[i], HIGH);
                delay(1000 - speed);
                digitalWrite(leds[i], LOW);
            }
        }
    }

private:
    int* leds, n;
};

子弹效果

void led_bullet(int leds[],int n,int speed,char direction){
     int k = n,t=0;
     //从左至右
     if(direction == 'L'){
     for(int i=0;i<n;i++){
        for(int j=0;j<k-1;j++){
            digitalWrite(leds[j],HIGH);
            delay(1000-speed);
            digitalWrite(leds[j],LOW);
         }   
            digitalWrite(leds[(k--)-1],HIGH);
       }
       //从右至左
       }else if(direction == 'R'){
           for(int i=0;i<n;i++){
              for(int j=k-1;j>=t;j--){
                  digitalWrite(leds[j],HIGH);
                  delay(1000-speed);
                  digitalWrite(leds[j],LOW);
               }   
                  digitalWrite(leds[t++],HIGH);
             }
         }
}

流水灯效果

void led_waterfall(int leds[],int n,int speed,char direction){

       //从左至右
       if(direction == 'L'){
               for(int i=0;i<n;i++){
                 digitalWrite(leds[i],HIGH);
                 delay(1000-speed);
                 digitalWrite(leds[i],LOW);
                }
        //从右至左
    	}else if(direction == 'R'){
             for(int i=n;i>=0;i--){
               digitalWrite(leds[i],HIGH);
               delay(1000-speed);
               digitalWrite(leds[i],LOW);
             }
      }
}

你可能感兴趣的:(#,单片机)