本帖最后由 迷你强 于 2016-8-31 09:05 编辑
这是一个库里面的程序,灯的长度一开始就定好了!
我想第一个效果的时候可以控制18颗灯
第二个效果的时候可以控制15颗灯
第三个效果的时候可以控制10颗灯
. .。。。。。
.。。。。。。
这样有办法吗?
- // This is a demonstration on how to use an input device to trigger changes on your neo pixels.這是如何使用的輸入設備來觸發你的新變化像素的演示。
- // You should wire a momentary push button to connect from ground to a digital IO pin. When you您應該連接一個瞬時按鈕從地面連接到數字IO引腳。當你
- // press the button it will change to a new pixel animation. Note that you need to press the按下按鈕,它會變成一個新的像素的動畫。請注意,您需要按
- // button once to start the first animation!按鈕一次,開始第一個動畫!
- #include <Adafruit_NeoPixel.h>
- #define BUTTON_PIN 2 // Digital IO pin connected to the button. This will be數字IO引腳連接到按鈕。這將是
- // driven with a pull-up resistor so the switch should帶有上拉電阻驅動,從而該開關應該
- // pull the pin to ground momentarily. On a high -> low摳腳接地瞬間。在高 - >低
- // transition the button press logic will execute.轉型按下按鈕邏輯將執行
- #define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.數字IO引腳連接到NeoPixels。
- #define PIXEL_COUNT 16 //定義PIXEL_COUNT 16
- // Parameter 1 = number of pixels in strip, neopixel stick has 8 參數1 =帶鋼像素數,neopixel粘有8
- // Parameter 2 = pin number (most are valid) 參數2 =針數(最有效)
- // Parameter 3 = pixel type flags, add together as needed: 參數3 =像素類型的標誌,同時根據需要添加:
- // NEO_RGB Pixels are wired for RGB bitstream NEO_RGB像素的有線RGB流
- // NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick NEO_GRB像素的有線暴流,正確neopixel棒
- // NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels) NEO_KHZ400 400千赫比特流(如FLORA像素)
- // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick NEO_KHZ800 800 KHz的比特流(如高密度LED燈條),正確neopixel棒
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); //Adafruit_NeoPixel帶= Adafruit_NeoPixel(PIXEL_COUNT,PIXEL_PIN,NEO_GRB + NEO_KHZ800);
- bool oldState = HIGH; //布爾oldState =高;
- int showType = 0; //INT showType = 0;
- void setup() { //無效設置(){
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- strip.begin();
- strip.show(); // Initialize all pixels to 'off' 初始化所有像素“關”
- }
- void loop() { //空隙環(){
- // Get current button state. 獲取當前按鈕狀態
- bool newState = digitalRead(BUTTON_PIN); //布爾newState = digitalRead(BUTTON_PIN);
-
- // Check if state changed from high to low (button press). 檢查狀態由高變低(按下按鈕)。
- if (newState == LOW && oldState == HIGH) { //如果(newState ==低&& oldState == HIGH){
- // Short delay to debounce button. //短延時去抖按鈕。
- delay(20); //延遲(20);
- // Check if button is still low after debounce. //檢查按鈕仍然偏低抖了。
- newState = digitalRead(BUTTON_PIN);
- if (newState == LOW) { //如果(newState == LOW){
- showType++;
- if (showType > 9) // 如果(showType> 9)
- showType=0;
- //startShow(showType);
- }
- }
- // Set the last button state to the old state. //設置最後一個按鈕的狀態到老態。
- oldState = newState;
- startShow(showType);
- }
- void startShow(int i) { // 無效startShow(int i)以{
- switch(i){ //開關(ⅰ){
- case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off 情況下0:colorWipe(strip.Color(0,0,0),50); //黑/關閉
- break; //打破;
- case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red 情況1:colorWipe(strip.Color(255,0,0),50); //紅
- break;
- case 2: colorWipe(strip.Color(0, 255, 0), 50); // Green 情況2:colorWipe(strip.Color(0,255,0),50); //綠
- break;
- case 3: colorWipe(strip.Color(0, 0, 255), 50); // Blue 情況3:colorWipe(strip.Color(0,0,255),50); //藍
- break;
- case 4: theaterChase(strip.Color(127, 127, 127), 50); // White 情況4:theaterChase(strip.Color(127,127,127),50); //白
- break;
- case 5: theaterChase(strip.Color(127, 0, 0), 50); // Red 情況5:theaterChase(strip.Color(127,0,0),50); //紅
- break;
- case 6: theaterChase(strip.Color( 0, 0, 127), 50); // Blue 情況6:theaterChase(strip.Color(0,0,127),50); //藍
- break;
- case 7: rainbow(20); //案例7:彩虹(20);
- break;
- case 8: rainbowCycle(20); //殼體8:rainbowCycle(20);
- break;
- case 9: theaterChaseRainbow(50); // 情況9:theaterChaseRainbow(50);
- break;
- }
- }
- // Fill the dots one after the other with a color 後,其他用顏色填充點單
- void colorWipe(uint32_t c, uint8_t wait) { //無效colorWipe(uint32_t的C,uint8_t有等待){
- for(uint16_t i=0; i<strip.numPixels(); i++) { // 對於(uint16_t我= 0;我<strip.numPixels();我++){
- strip.setPixelColor(i, c); // strip.setPixelColor(I,C);
- strip.show();
- delay(wait); // 延遲(等待);
- }
- }
- void rainbow(uint8_t wait) { //彩虹無效(uint8_t有等待){
- uint16_t i, j;
- for(j=0; j<256; j++) { // 對於(J = 0;Ĵ<256; J ++){
- for(i=0; i<strip.numPixels(); i++) { // 對於(i = 0;我<strip.numPixels();我++){
- strip.setPixelColor(i, Wheel((i+j) & 255)); // strip.setPixelColor(ⅰ,輪式((I + J)255));
- }
- strip.show();
- delay(wait); //延遲(等待);
- }
- }
- // Slightly different, this makes the rainbow equally distributed throughout 略有不同,這使得彩虹均勻分佈在整個
- void rainbowCycle(uint8_t wait) { //無效rainbowCycle(uint8_t有等待){
- uint16_t i, j;
- for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel //對於(J = 0;Ĵ<256 * 5; J ++){// 5個週期輪上的所有顏色
- for(i=0; i< strip.numPixels(); i++) { //對於(i = 0;我<strip.numPixels();我++){
- strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); //strip.setPixelColor(我,車輪(((I * 256 / strip.numPixels())+ J)255));
- }
- strip.show();
- delay(wait); //延遲(等待);
- }
- }
- //Theatre-style crawling lights. //劇院式燈光爬行。
- void theaterChase(uint32_t c, uint8_t wait) { //無效theaterChase(uint32_t的C,uint8_t有等待){
- for (int j=0; j<10; j++) { //do 10 cycles of chasing //對於(INT J = 0;Ĵ<10; J ++){//做10次追逐
- for (int q=0; q < 3; q++) { //為(中間體Q = 0,Q <3,Q ++){
- for (int i=0; i < strip.numPixels(); i=i+3) { // 對於(INT I = 0;我<strip.numPixels(); I = I + 3){
- strip.setPixelColor(i+q, c); //turn every third pixel on // strip.setPixelColor(I + Q,C); //把每一個第三像素的
- }
- strip.show();
-
- delay(wait); // 延遲(等待);
-
- for (int i=0; i < strip.numPixels(); i=i+3) { // 對於(INT I = 0;我<strip.numPixels(); I = I + 3){
- strip.setPixelColor(i+q, 0); //turn every third pixel off 把每一個第三像素關閉
- }
- }
- }
- }
- //Theatre-style crawling lights with rainbow effect 劇院式燈光爬行彩虹效果
- void theaterChaseRainbow(uint8_t wait) { //無效theaterChaseRainbow(uint8_t有等待){
- for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel // 對於(INT J = 0;Ĵ<256; J ++){//週期的所有256種顏色的輪
- for (int q=0; q < 3; q++) { //為(中間體Q = 0,Q <3,Q ++){
- for (int i=0; i < strip.numPixels(); i=i+3) { //對於(INT I = 0;我<strip.numPixels(); I = I + 3){
- strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on 把每一個第三像素的
- }
- strip.show();
-
- delay(wait); //延遲(等待);
-
- for (int i=0; i < strip.numPixels(); i=i+3) { // 對於(INT I = 0;我<strip.numPixels(); I = I + 3){
- strip.setPixelColor(i+q, 0); //turn every third pixel off // strip.setPixelColor(ⅰ+ Q,0); //把每一個第三像素關閉
- }
- }
- }
- }
- // Input a value 0 to 255 to get a color value. //輸入值0到255得到的顏色值。
- // The colours are a transition r - g - b - back to r. //顏色是一個過渡的R - 克 - B - 回河
- uint32_t Wheel(byte WheelPos) { //uint32_t的車輪(字節WheelPos){
- WheelPos = 255 - WheelPos;
- if(WheelPos < 85) { // 如果(WheelPos <85){
- return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); // 返回strip.Color(255 - WheelPos * 3,0 WheelPos * 3);
- } else if(WheelPos < 170) { //}否則,如果(WheelPos <170){
- WheelPos -= 85;
- return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); // 返回strip.Color(0,WheelPos * 3,255 - WheelPos * 3);
- } else { // }其他{
- WheelPos -= 170;
- return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); //返回strip.Color(WheelPos * 3,255 - WheelPos * 3,0);
- }
- }
复制代码 |