极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17592|回复: 7

arduino 大侠们,请帮看这个程序,有9种效果。我想每一个效果灯条的长度可以自己设定

[复制链接]
发表于 2016-8-29 11:27:02 | 显示全部楼层 |阅读模式
本帖最后由 迷你强 于 2016-8-31 09:05 编辑

这是一个库里面的程序,灯的长度一开始就定好了!
我想第一个效果的时候可以控制18颗灯
       第二个效果的时候可以控制15颗灯
       第三个效果的时候可以控制10颗灯
    .  .。。。。。
       .。。。。。。
这样有办法吗?



  1. // This is a demonstration on how to use an input device to trigger changes on your neo pixels.這是如何使用的輸入設備來觸發你的新變化像素的演示。
  2. // You should wire a momentary push button to connect from ground to a digital IO pin.  When you您應該連接一個瞬時按鈕從地面連接到數字IO引腳。當你
  3. // press the button it will change to a new pixel animation.  Note that you need to press the按下按鈕,它會變成一個新的像素的動畫。請注意,您需要按
  4. // button once to start the first animation!按鈕一次,開始第一個動畫!

  5. #include <Adafruit_NeoPixel.h>

  6. #define BUTTON_PIN   2    // Digital IO pin connected to the button.  This will be數字IO引腳連接到按鈕。這將是
  7.                           // driven with a pull-up resistor so the switch should帶有上拉電阻驅動,從而該開關應該
  8.                           // pull the pin to ground momentarily.  On a high -> low摳腳接地瞬間。在高 - >低
  9.                           // transition the button press logic will execute.轉型按下按鈕邏輯將執行

  10. #define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.數字IO引腳連接到NeoPixels。

  11. #define PIXEL_COUNT 16    //定義PIXEL_COUNT 16

  12. // Parameter 1 = number of pixels in strip,  neopixel stick has 8  參數1 =帶鋼像素數,neopixel粘有8
  13. // Parameter 2 = pin number (most are valid)                       參數2 =針數(最有效)
  14. // Parameter 3 = pixel type flags, add together as needed:         參數3 =像素類型的標誌,同時根據需要添加:
  15. //   NEO_RGB     Pixels are wired for RGB bitstream                 NEO_RGB像素的有線RGB流
  16. //   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick                      NEO_GRB像素的有線暴流,正確neopixel棒
  17. //   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)                                               NEO_KHZ400 400千赫比特流(如FLORA像素)
  18. //   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick         NEO_KHZ800 800 KHz的比特流(如高密度LED燈條),正確neopixel棒
  19. 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);

  20. bool oldState = HIGH;     //布爾oldState =高;
  21. int showType = 0;         //INT showType = 0;

  22. void setup() {           //無效設置(){
  23.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  24.   strip.begin();
  25.   strip.show(); // Initialize all pixels to 'off'  初始化所有像素“關”
  26. }

  27. void loop() {                   //空隙環(){
  28.   // Get current button state.     獲取當前按鈕狀態
  29.   bool newState = digitalRead(BUTTON_PIN);         //布爾newState = digitalRead(BUTTON_PIN);
  30.   
  31.   // Check if state changed from high to low (button press).     檢查狀態由高變低(按下按鈕)。
  32.   if (newState == LOW && oldState == HIGH) {                     //如果(newState ==低&& oldState == HIGH){
  33.     // Short delay to debounce button.                           //短延時去抖按鈕。
  34.     delay(20);                                                   //延遲(20);
  35.     // Check if button is still low after debounce.              //檢查按鈕仍然偏低抖了。
  36.     newState = digitalRead(BUTTON_PIN);
  37.     if (newState == LOW) {                                       //如果(newState == LOW){
  38.       showType++;                                             
  39.       if (showType > 9)                                          // 如果(showType> 9)
  40.         showType=0;
  41.       //startShow(showType);
  42.     }
  43.   }

  44.   // Set the last button state to the old state.                   //設置最後一個按鈕的狀態到老態。
  45.   oldState = newState;
  46.   startShow(showType);
  47. }

  48. void startShow(int i) {                                                 // 無效startShow(int i)以{
  49.   switch(i){                                                            //開關(ⅰ){
  50.     case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off          情況下0:colorWipe(strip.Color(0,0,0),50); //黑/關閉
  51.             break;                                                     //打破;
  52.     case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red                 情況1:colorWipe(strip.Color(255,0,0),50); //紅
  53.             break;
  54.     case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green                情況2:colorWipe(strip.Color(0,255,0),50); //綠
  55.             break;
  56.     case 3: colorWipe(strip.Color(0, 0, 255), 50);  // Blue                 情況3:colorWipe(strip.Color(0,0,255),50); //藍
  57.             break;
  58.     case 4: theaterChase(strip.Color(127, 127, 127), 50); // White          情況4:theaterChase(strip.Color(127,127,127),50); //白
  59.             break;
  60.     case 5: theaterChase(strip.Color(127,   0,   0), 50); // Red            情況5:theaterChase(strip.Color(127,0,0),50); //紅
  61.             break;
  62.     case 6: theaterChase(strip.Color(  0,   0, 127), 50); // Blue           情況6:theaterChase(strip.Color(0,0,127),50); //藍
  63.             break;
  64.     case 7: rainbow(20);                                                 //案例7:彩虹(20);
  65.             break;
  66.     case 8: rainbowCycle(20);                                           //殼體8:rainbowCycle(20);
  67.             break;
  68.     case 9: theaterChaseRainbow(50);                                    // 情況9:theaterChaseRainbow(50);
  69.             break;
  70.   }
  71. }

  72. // Fill the dots one after the other with a color                      後,其他用顏色填充點單
  73. void colorWipe(uint32_t c, uint8_t wait) {                             //無效colorWipe(uint32_t的C,uint8_t有等待){
  74.   for(uint16_t i=0; i<strip.numPixels(); i++) {                        //  對於(uint16_t我= 0;我<strip.numPixels();我++){
  75.       strip.setPixelColor(i, c);                                       // strip.setPixelColor(I,C);
  76.       strip.show();
  77.       delay(wait);                                                     // 延遲(等待);
  78.   }
  79. }

  80. void rainbow(uint8_t wait) {                                           //彩虹無效(uint8_t有等待){
  81.   uint16_t i, j;

  82.   for(j=0; j<256; j++) {                                               // 對於(J = 0;&#308;<256; J ++){
  83.     for(i=0; i<strip.numPixels(); i++) {                               // 對於(i = 0;我<strip.numPixels();我++){
  84.       strip.setPixelColor(i, Wheel((i+j) & 255));                      //   strip.setPixelColor(ⅰ,輪式((I + J)255));
  85.     }
  86.     strip.show();
  87.     delay(wait);                                                       //延遲(等待);
  88.   }
  89. }

  90. // Slightly different, this makes the rainbow equally distributed throughout         略有不同,這使得彩虹均勻分佈在整個
  91. void rainbowCycle(uint8_t wait) {                                                    //無效rainbowCycle(uint8_t有等待){
  92.   uint16_t i, j;

  93.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel                         //對於(J = 0;&#308;<256 * 5; J ++){// 5個週期輪上的所有顏色
  94.     for(i=0; i< strip.numPixels(); i++) {                                             //對於(i = 0;我<strip.numPixels();我++){
  95.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));       //strip.setPixelColor(我,車輪(((I * 256 / strip.numPixels())+ J)255));
  96.     }
  97.     strip.show();
  98.     delay(wait);                                                                     //延遲(等待);
  99.   }
  100. }

  101. //Theatre-style crawling lights.                                                    //劇院式燈光爬行。
  102. void theaterChase(uint32_t c, uint8_t wait) {                                       //無效theaterChase(uint32_t的C,uint8_t有等待){
  103.   for (int j=0; j<10; j++) {  //do 10 cycles of chasing                             //對於(INT J = 0;&#308;<10; J ++){//做10次追逐
  104.     for (int q=0; q < 3; q++) {                                                     //為(中間體Q = 0,Q <3,Q ++){
  105.       for (int i=0; i < strip.numPixels(); i=i+3) {                                 //  對於(INT I = 0;我<strip.numPixels(); I = I + 3){
  106.         strip.setPixelColor(i+q, c);    //turn every third pixel on                 //   strip.setPixelColor(I + Q,C); //把每一個第三像素的
  107.       }
  108.       strip.show();
  109.      
  110.       delay(wait);                                                                  //  延遲(等待);
  111.      
  112.       for (int i=0; i < strip.numPixels(); i=i+3) {                                 // 對於(INT I = 0;我<strip.numPixels(); I = I + 3){
  113.         strip.setPixelColor(i+q, 0);        //turn every third pixel off            把每一個第三像素關閉
  114.       }
  115.     }
  116.   }
  117. }

  118. //Theatre-style crawling lights with rainbow effect                                 劇院式燈光爬行彩虹效果
  119. void theaterChaseRainbow(uint8_t wait) {                                            //無效theaterChaseRainbow(uint8_t有等待){
  120.   for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel            // 對於(INT J = 0;&#308;<256; J ++){//週期的所有256種顏色的輪
  121.     for (int q=0; q < 3; q++) {                                                     //為(中間體Q = 0,Q <3,Q ++){
  122.         for (int i=0; i < strip.numPixels(); i=i+3) {                               //對於(INT I = 0;我<strip.numPixels(); I = I + 3){
  123.           strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on   把每一個第三像素的
  124.         }
  125.         strip.show();
  126.       
  127.         delay(wait);                                                                //延遲(等待);
  128.       
  129.         for (int i=0; i < strip.numPixels(); i=i+3) {                               //  對於(INT I = 0;我<strip.numPixels(); I = I + 3){
  130.           strip.setPixelColor(i+q, 0);        //turn every third pixel off         //  strip.setPixelColor(ⅰ+ Q,0); //把每一個第三像素關閉
  131.         }
  132.     }
  133.   }
  134. }

  135. // Input a value 0 to 255 to get a color value.                                   //輸入值0到255得到的顏色值。
  136. // The colours are a transition r - g - b - back to r.                            //顏色是一個過渡的R - 克 - B - 回河
  137. uint32_t Wheel(byte WheelPos) {                                                   //uint32_t的車輪(字節WheelPos){
  138.   WheelPos = 255 - WheelPos;
  139.   if(WheelPos < 85) {                                                             //  如果(WheelPos <85){
  140.    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);                       // 返回strip.Color(255 - WheelPos * 3,0 WheelPos * 3);
  141.   } else if(WheelPos < 170) {                                                     //}否則,如果(WheelPos <170){
  142.     WheelPos -= 85;
  143.    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);                       // 返回strip.Color(0,WheelPos * 3,255 - WheelPos * 3);
  144.   } else {                                                                        // }其他{
  145.    WheelPos -= 170;
  146.    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);                       //返回strip.Color(WheelPos * 3,255 - WheelPos * 3,0);
  147.   }
  148. }

复制代码
回复

使用道具 举报

发表于 2016-8-29 14:23:04 | 显示全部楼层
#define PIXEL_COUNT 16    //定義PIXEL_COUNT 16

把这个地方修改为你需要的就行了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-30 14:50:14 | 显示全部楼层
zoologist 发表于 2016-8-29 14:23
#define PIXEL_COUNT 16    //定義PIXEL_COUNT 16

把这个地方修改为你需要的就行了

改这里只能设定一种长度!我想要的是每一种效果,我想单独设置一种长度!
  不知道可以实现吗???
回复 支持 反对

使用道具 举报

发表于 2016-8-30 14:59:14 | 显示全部楼层
crbgz 发表于 2016-8-30 14:50
改这里只能设定一种长度!我想要的是每一种效果,我想单独设置一种长度!
  不知道可以实现吗???

可以,比如,你用 16个灯,那么第一种每次只有10个亮,是这个意思吧?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-31 08:53:32 | 显示全部楼层
zoologist 发表于 2016-8-30 14:59
可以,比如,你用 16个灯,那么第一种每次只有10个亮,是这个意思吧?

是的!!就是想要这种效果!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-31 08:55:54 | 显示全部楼层
crbgz 发表于 2016-8-31 08:53
是的!!就是想要这种效果!

还想到一个做法,不知道可以做吗?
#define PIXEL_COUNT 16    //定義PIXEL_COUNT 16
这个变量可以用按键来增加吗?
就是按一下按键就变成#define PIXEL_COUNT 17
再按一下就变成#define PIXEL_COUNT 18
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-31 08:56:22 | 显示全部楼层
crbgz 发表于 2016-8-31 08:53
是的!!就是想要这种效果!

还想到一个做法,不知道可以做吗?
#define PIXEL_COUNT 16   
这个变量可以用按键来增加吗?
就是按一下按键就变成#define PIXEL_COUNT 17
再按一下就变成#define PIXEL_COUNT 18
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-31 08:56:53 | 显示全部楼层
crbgz 发表于 2016-8-31 08:53
是的!!就是想要这种效果!

还想到一个做法,不知道可以做吗?
#define PIXEL_COUNT 16   
这个变量可以用按键来增加吗?
就是按一下按键就变成#define PIXEL_COUNT 17
再按一下就变成#define PIXEL_COUNT 18
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-13 23:46 , Processed in 0.036629 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表