找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4044|回复: 2
打印 上一主题 下一主题
收起左侧

用Adafruit_NeoPixel-1.3.4的全彩灯条 Arduino源程序

[复制链接]
跳转到指定楼层
楼主
ID:69091 发表于 2020-3-10 14:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
经实物实验60颗全部点亮。多种显示方式。

Arduino源程序如下:
  1. /*
  2. strip.setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b);
  3. 第一个参数n是彩带中LED的编号,最接近单片机引脚的编号为0;接下来的三个参数描述像素颜色,分别表示红色,绿色和蓝色的亮度级别,0为最暗,255是最大亮度;

  4. strip.setPixelColor(uint16_t n, uint32_t c);
  5. n是彩带中LED的编号,颜色color是一种32位类型,将红色,绿色和蓝色值合并为一个数字,有时这样做能提高程序的效率.通过下面的方法,可以将红色,绿色和蓝色值转换为32位类型。
  6. uint32_t magenta = strip.Color(red, green, blue);

  7. strip.setBrightness(uint8_t);
  8. 一般只在setup()中调用,以保证在整个程序执行过程中LED颜色亮度的一致性,其实在程序中通过合适的逻辑控制各像素的亮度值可能动画效果更好.

  9. strip.show();
  10. 该方法更新彩带上的全部LED。一个好的习惯是先利用setPixelColor()设置好整个彩带的颜色,然后再调用show()方法,以防止出现动画跳跃而不平滑。
  11. */
  12. #include <Adafruit_NeoPixel.h>
  13. #ifdef __AVR__
  14. #include <avr/power.h>
  15. #endif

  16. //int pinInterrupt = 2; //接中断信号的脚  
  17.   
  18. //void onChange()  
  19. //{  
  20. //   if ( digitalRead(pinInterrupt) == LOW )  
  21. //      Serial.println("Key Down");  
  22. //   else  
  23. //      Serial.println("Key UP");  
  24. //}

  25. /******************************************key
  26. #define BUTTON 3
  27. int val=0;
  28. int old_val=0;
  29. int state=0;
  30. ****************************************/
  31. #define PIN 6

  32. // Parameter 1 = number of pixels in strip
  33. // Parameter 2 = Arduino pin number (most are valid)
  34. // Parameter 3 = pixel type flags, add together as needed:参数 3 = 像素类型标志,根据需要添加在一起:
  35. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  36. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  37. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  38. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  39. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

  40. Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

  41. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  42. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  43. // and minimize distance between Arduino and first pixel.  Avoid connecting
  44. // on a live circuit...if you must, connect GND first.
  45. // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  46. void setup()
  47. {

  48.   
  49.   #if defined (__AVR_ATtiny85__)
  50.     if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  51.   #endif
  52.   // End of trinket special code

  53. // Serial.begin(9600); //打开串口   
  54. //   pinMode( pinInterrupt, INPUT);//设置管脚为输入  
  55. //Enable中断管脚, 中断服务程序为onChange(), 监视引脚变化  
  56. //attachInterrupt( digitalPinToInterrupt(pinInterrupt), onChange, CHANGE);  



  57. // pinMode(BUTTON,INPUT);//key
  58.   strip.begin();
  59.   strip.show(); // Initialize all pixels to 'off'
  60. }

  61. void loop()
  62. {
  63. /****************************************** key
  64. val = digitalRead(BUTTON);

  65.   if((val==HIGH)&&(old_val==LOW))
  66.   {
  67.     state=1-state;
  68.     delay(10);
  69.   }

  70.   old_val = val;

  71.   if(state==1)
  72.   {
  73. ********************************************/   
  74.   // Some example procedures showing how to display to the pixels:显示如何显示到像素的一些示例过程:
  75.   colorWipe(strip.Color(255, 0, 0), 50); // Red delay50
  76.   colorWipe(strip.Color(0, 255, 0), 50); // Green
  77.   colorWipe(strip.Color(0, 0, 225), 50); // Blue
  78.   colorWipe(strip.Color(255, 0, 255), 50); // purple
  79.   
  80.   colorWipe(strip.Color(128, 0, 175), 50); // purple
  81.   colorWipe(strip.Color(0, 128, 255), 50);
  82.   colorWipe(strip.Color(0, 255, 255), 50);
  83.   //colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
  84.   

  85.   
  86.   // Send a theater pixel chase in...发送剧院像素追逐.
  87.   theaterChase(strip.Color(127, 127, 127), 50); // White
  88.   theaterChase(strip.Color(255, 0, 0), 50); // Red
  89.   theaterChase(strip.Color(0, 0, 255), 50); // Blue剧院追逐
  90.   theaterChase(strip.Color(0, 255, 0), 50);
  91.   theaterChase(strip.Color(255, 0, 255), 50);
  92.   theaterChase(strip.Color(255, 255, 0), 50);
  93.   theaterChase(strip.Color(0, 255, 255), 50);
  94.   
  95.   
  96.   rainbow(20);
  97.   rainbowCycle(20);
  98.   theaterChaseRainbow(50);

  99.   rainbowCycle1(20);
  100.   rainbowCycle2(20);
  101. /************************************************** key
  102.   }
  103.   else
  104.   {
  105.     colorWipe(strip.Color(0, 255, 0), 50);
  106.   }
  107. ***************************************************/
  108. }
  109.    
  110. // Fill the dots one after the other with a color用颜色一个接一个地填充这些点
  111. void colorWipe(uint32_t c, uint8_t wait)
  112. {
  113.   for(uint16_t i=0; i<strip.numPixels(); i++)
  114.   {
  115.     strip.setPixelColor(i, c);
  116.     strip.show();
  117.     delay(wait);
  118.   }
  119. }

  120. void rainbow(uint8_t wait)
  121. {
  122.   uint16_t i, j;

  123.   for(j=0; j<256; j++)
  124.   {
  125.     for(i=0; i<strip.numPixels(); i++)
  126.     {
  127.       strip.setPixelColor(i, Wheel((i+j) & 255));
  128.     }
  129.     strip.show();
  130.     delay(wait);
  131.   }
  132. }

  133. // Slightly different, this makes the rainbow equally distributed throughout略有不同,这使得彩虹在整个
  134. void rainbowCycle(uint8_t wait)
  135. {
  136.   uint16_t i, j;

  137.   for(j=0; j<256*5; j++) // 5 cycles of all colors on wheel
  138.   {
  139.     for(i=0; i< strip.numPixels(); i++)
  140.     {
  141.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  142.     }
  143.     strip.show();
  144.     delay(wait);
  145.   }
  146. }

  147. void rainbowCycle1(uint8_t wait)
  148. {
  149.   uint16_t i, j;

  150.   for(j=256*5; j>=0; j--) // 5 cycles of all colors on wheel
  151.   {
  152.     for(i=0; i< strip.numPixels(); i++)
  153.     {
  154.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  155.     }
  156.     strip.show();
  157.     delay(wait);
  158.   }
  159. }

  160. void rainbowCycle2(uint8_t wait)
  161. {
  162.   uint16_t i, j;

  163.   for(j=256*5; j>=0; j--) // 5 cycles of all colors on wheel
  164.   {
  165.     for(i=strip.numPixels(); i>=0; i--)
  166.     {
  167.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  168.     }
  169.     strip.show();
  170.     delay(wait);
  171.   }
  172. }
  173. //Theatre-style crawling lights.
  174. void theaterChase(uint32_t c, uint8_t wait)
  175. {
  176.   for (int j=0; j<10; j++)
  177.   {  //do 10 cycles of chasing
  178.     for (int q=0; q < 3; q++)
  179.     {
  180.       for (uint16_t i=0; i < strip.numPixels(); i=i+3)
  181.       {
  182.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  183.       }
  184.       strip.show();

  185.       delay(wait);

  186.       for (uint16_t i=0; i < strip.numPixels(); i=i+3)
  187.       {
  188.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  189.       }
  190.     }
  191.   }
  192. }

  193. //Theatre-style crawling lights with rainbow effect彩虹色效果的剧院风格爬行灯
  194. void theaterChaseRainbow(uint8_t wait)
  195. {
  196.   for (int j=0; j < 256; j++)
  197.   {     // cycle all 256 colors in the wheel
  198.     for (int q=0; q < 3; q++)
  199.     {
  200.       for (uint16_t i=0; i < strip.numPixels(); i=i+3)
  201.       {
  202.         strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
  203.       }
  204.       strip.show();

  205.       delay(wait);

  206.       for (uint16_t i=0; i < strip.numPixels(); i=i+3)
  207.       {
  208.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  209.       }
  210.     }
  211.   }
  212. }

  213. // Input a value 0 to 255 to get a color value.
  214. // The colours are a transition r - g - b - back to r.
  215. uint32_t Wheel(byte WheelPos)
  216. {
  217.   WheelPos = 255 - WheelPos;
  218.   if(WheelPos < 85)
  219.   {
  220.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  221.   }
  222.     if(WheelPos < 170)
  223.   {
  224.     WheelPos -= 85;
  225.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  226.   }
  227.   WheelPos -= 170;
  228.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  229. }
复制代码

所有资料51hei提供下载:
sketch_RGB.rar (2.96 KB, 下载次数: 37)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:641718 发表于 2021-12-18 13:42 | 只看该作者
为什么要回复需要审核,请等待通过
回复

使用道具 举报

板凳
ID:455526 发表于 2022-2-12 13:11 | 只看该作者
资源不好用
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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