Arduino 入门教程(十五) WS2811跑马灯

自己写的算法

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#define PIN            8
#define NUMPIXELS      167

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 50; // delay for half a second
int snakeLen = 6;  // snake's len
int endDex; // snake end's index
int dex = 0; // the start index
bool isNext = true;
void setup() {
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  pixels.begin();
  endDex = snakeLen; // only once
}

void loop() {
  if(isNext){ 
      isNext = false;
      colorWrap();
    }
}
void colorWrap(){
   for(int i=dex;i<snakeLen;i++){
    pixels.setPixelColor(i%NUMPIXELS, pixels.Color(0,150,0)); // i%167 because run second circle for reset
    pixels.show(); 
    delay(delayval); 
    if(i == dex + (endDex-1)){
         isNext = true;
         pixels.setPixelColor(dex%NUMPIXELS, pixels.Color(0,0,0)); // close the led in the end
         pixels.show();
         dex ++;
         snakeLen ++;
      }
  }
 }

展开阅读全文

没有更多推荐了,返回首页