Arduino爱好者

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

[经验] 【Arduino】168种传感器模块系列实验(131)--24位WS2812环形灯板

[复制链接]
 楼主| 发表于 2019-10-1 08:48 | 显示全部楼层
04.jpg
连接
LED环具有四个连接点。

标为GND的引脚为接地引脚,应连接至控制LED的微控制器的接地引脚,也应连接至电源的负极。

标有PWR + 5V的引脚是电源输入引脚,应连接到合适的电源。5 V的输入电压用于为环供电,当以全亮度输出白色时,环上的每个LED在5 V时可消耗高达50 mA的电流。这意味着该环最多可吸收约1.2 A电流。

标为“ 数据输入 ”的引脚是一个输入,用于接收控制LED的信号。该引脚应连接至基于SK6812 / WS281x的LED产品的微控制器输出引脚或信号输出引脚。

标有“ 数据输出 ”的引脚是可选的。它可以连接到另一个基于SK6812 / WS281x的LED产品的数据输入引脚,以形成一条链。

我们的0.1英寸排针可以用来连接环的四个针,也可以将导线直接焊接到板上。环并不与任何标头销或连接器发货。

如果在使用NeoPixels时未采取适当的预防措施,则很容易损坏它们。通常,对于基于SK6812 / WS2812B的产品,建议在接地线和电源线之间添加至少10 F的电容器,并在微控制器和信号输入引脚之间添加100Ω至1000Ω的电阻。

 楼主| 发表于 2019-10-1 08:59 | 显示全部楼层
模块电原理图
09.jpg

 楼主| 发表于 2019-10-1 09:02 | 显示全部楼层
ATTiny85与WS2812 LED环配合使用示意图
10 (1).jpg
 楼主| 发表于 2019-10-1 10:05 | 显示全部楼层
本帖最后由 eagler8 于 2020-10-9 10:47 编辑
  1. /*
  2. 【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
  3. 实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
  4. 项目一:循环点亮24位绿色LED
  5. Module      UNO
  6. VCC   ——   5V
  7. GND  ——   GND
  8. DI    ——   D6
  9. */

  10. #include <Adafruit_NeoPixel.h>
  11. #ifdef __AVR__
  12. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  13. #endif

  14. // Which pin on the Arduino is connected to the NeoPixels?
  15. #define PIN        6 // On Trinket or Gemma, suggest changing this to 1

  16. // How many NeoPixels are attached to the Arduino?
  17. #define NUMPIXELS 24 // Popular NeoPixel ring size

  18. // When setting up the NeoPixel library, we tell it how many pixels,
  19. // and which pin to use to send signals. Note that for older NeoPixel
  20. // strips you might need to change the third parameter -- see the
  21. // strandtest example for more information on possible values.
  22. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

  23. #define DELAYVAL 300 // Time (in milliseconds) to pause between pixels

  24. void setup() {
  25.   // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  26.   // Any other board, you can remove this part (but no harm leaving it):
  27. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  28.   clock_prescale_set(clock_div_1);
  29. #endif
  30.   // END of Trinket-specific code.

  31.   pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  32. }

  33. void loop() {
  34.   pixels.clear(); // Set all pixel colors to 'off'

  35.   // The first NeoPixel in a strand is #0, second is 1, all the way up
  36.   // to the count of pixels minus one.
  37.   for(int i=0; i<NUMPIXELS; i++) { // For each pixel...

  38.     // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
  39.     // Here we're using a moderately bright green color:
  40.     pixels.setPixelColor(i, pixels.Color(0, 150, 0));

  41.     pixels.show();   // Send the updated pixel colors to the hardware.

  42.     delay(DELAYVAL); // Pause before next pass through loop
  43.   }
  44. }
复制代码

 楼主| 发表于 2019-10-1 10:17 | 显示全部楼层
11.jpg
 楼主| 发表于 2019-10-1 10:32 | 显示全部楼层
本帖最后由 eagler8 于 2020-10-9 10:54 编辑
  1. /*
  2.   【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
  4.   项目二:循环快扫24位红绿蓝色LED流水灯
  5.   Module      UNO
  6.   VCC   ——   5V
  7.   GND  ——   GND
  8.   DI    ——   D6
  9. */

  10. #include <FastLED.h>
  11. #define LED_PIN     6
  12. #define NUM_LEDS    24
  13. CRGB leds[NUM_LEDS];
  14. void setup() {
  15.   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  16. }
  17. void loop() {
  18.   for (int i = 0; i <= 23; i++) {
  19.     leds = CRGB ( 0, 0, 255);
  20.     FastLED.show();
  21.     delay(40);
  22.   }
  23.   for (int i = 0; i <= 23; i++) {
  24.     leds = CRGB ( 255, 0, 0);
  25.     FastLED.show();
  26.     delay(40);
  27.   }
  28.   for (int i = 0; i <= 23; i++) {
  29.     leds = CRGB ( 0, 255, 0);
  30.     FastLED.show();
  31.     delay(40);
  32.   }
  33. }
复制代码

 楼主| 发表于 2019-10-1 10:43 | 显示全部楼层
17-.gif
 楼主| 发表于 2019-10-1 11:59 | 显示全部楼层
本帖最后由 eagler8 于 2020-10-9 10:55 编辑
  1. /*
  2.   【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
  4.   项目三:使用红色、绿色和蓝色三种参数将任何LED设置为任何颜色
  5.   Module      UNO
  6.   VCC   ——   5V
  7.   GND  ——   GND
  8.   DI    ——   D6
  9. */

  10. #include <FastLED.h>
  11. #define LED_PIN     6
  12. #define NUM_LEDS    24
  13. CRGB leds[NUM_LEDS];
  14. void setup() {
  15.   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  16.   
  17. }
  18. void loop() {
  19.   
  20.   leds[0] = CRGB(255, 0, 0);
  21.   FastLED.show();
  22.   delay(500);  
  23.   leds[1] = CRGB(0, 255, 0);
  24.   FastLED.show();
  25.   delay(500);
  26.   leds[2] = CRGB(0, 0, 255);
  27.   FastLED.show();
  28.   delay(500);
  29.   leds[5] = CRGB(150, 0, 255);
  30.   FastLED.show();
  31.   delay(500);
  32.   leds[9] = CRGB(255, 200, 20);
  33.   FastLED.show();
  34.   delay(500);
  35.   leds[14] = CRGB(85, 60, 180);
  36.   FastLED.show();
  37.   delay(500);
  38.   leds[19] = CRGB(50, 255, 20);
  39.   FastLED.show();
  40.   delay(500);
  41. }
复制代码

 楼主| 发表于 2019-10-1 12:06 | 显示全部楼层
本帖最后由 eagler8 于 2020-10-9 10:56 编辑
  1. /*
  2.   【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
  4.   项目四:流水灯变幻彩虹灯
  5.   Module      UNO
  6.   VCC   ——   5V
  7.   GND  ——   GND
  8.   DI    ——   D6
  9. */

  10. #include <Adafruit_NeoPixel.h>

  11. #define PIN 6
  12. #define BRIGHTNESS 24

  13. Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);

  14. void setup() {
  15.   strip.setBrightness(BRIGHTNESS);
  16.   strip.begin();
  17.   strip.show();
  18. }

  19. void loop() {
  20.   colorWipe(strip.Color(150, 0, 0), 50); // Red
  21.   colorWipe(strip.Color(0, 150, 0), 50); // Green
  22.   colorWipe(strip.Color(0, 0, 150), 50); // Blue
  23.   colorWipe(strip.Color(150, 150, 150), 50); // BlueWite
  24.   rainbowCycle(1);

  25. }

  26. void colorWipe(uint32_t c, uint8_t wait) {
  27.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  28.     strip.setPixelColor(i, c);
  29.     strip.show();
  30.     delay(wait);
  31.   }
  32. }

  33. void rainbow(uint8_t wait) {
  34.   uint16_t i, j;
  35.   for (j = 0; j < 256; j++) {
  36.     for (i = 0; i < strip.numPixels(); i++) {
  37.       strip.setPixelColor(i, Wheel((i + j) & 255 ));
  38.     }
  39.     strip.show();
  40.     delay(wait);
  41.   }
  42. }

  43. void rainbowCycle(uint8_t wait) {
  44.   uint16_t i, j;
  45.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  46.     for (i = 0; i < strip.numPixels(); i++) {
  47.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  48.     }
  49.     strip.show();
  50.     delay(wait);
  51.   }
  52. }

  53. uint32_t Wheel(byte WheelPos) {
  54.   if (WheelPos < 85) {
  55.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  56.   } else if (WheelPos < 170) {
  57.     WheelPos -= 85;
  58.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  59.   } else {
  60.     WheelPos -= 170;
  61.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  62.   }
  63. }
复制代码

 楼主| 发表于 2019-10-1 12:20 | 显示全部楼层
本帖最后由 eagler8 于 2020-10-9 10:56 编辑
  1. /*
  2.   【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
  4.   项目五:循环流水变幻呼吸灯
  5.   Module      UNO
  6.   VCC   ——   5V
  7.   GND  ——   GND
  8.   DI    ——   D6
  9. */

  10. // NeoPixel test program showing use of the WHITE channel for RGBW
  11. // pixels only (won't look correct on regular RGB NeoPixel strips).

  12. #include <Adafruit_NeoPixel.h>
  13. #ifdef __AVR__
  14. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  15. #endif

  16. // Which pin on the Arduino is connected to the NeoPixels?
  17. // On a Trinket or Gemma we suggest changing this to 1:
  18. #define LED_PIN     6

  19. // How many NeoPixels are attached to the Arduino?
  20. #define LED_COUNT  24

  21. // NeoPixel brightness, 0 (min) to 255 (max)
  22. #define BRIGHTNESS 50

  23. // Declare our NeoPixel strip object:
  24. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
  25. // Argument 1 = Number of pixels in NeoPixel strip
  26. // Argument 2 = Arduino pin number (most are valid)
  27. // Argument 3 = Pixel type flags, add together as needed:
  28. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  29. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  30. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  31. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  32. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

  33. void setup() {
  34.   // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  35.   // Any other board, you can remove this part (but no harm leaving it):
  36. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  37.   clock_prescale_set(clock_div_1);
  38. #endif
  39.   // END of Trinket-specific code.

  40.   strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  41.   strip.show();            // Turn OFF all pixels ASAP
  42.   strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  43. }

  44. void loop() {
  45.   // Fill along the length of the strip in various colors...
  46.   colorWipe(strip.Color(255,   0,   0)     , 50); // Red
  47.   colorWipe(strip.Color(  0, 255,   0)     , 50); // Green
  48.   colorWipe(strip.Color(  0,   0, 255)     , 50); // Blue
  49.   colorWipe(strip.Color(  0,   0,   0, 255), 50); // True white (not RGB white)

  50.   whiteOverRainbow(75, 5);

  51.   pulseWhite(5);

  52.   rainbowFade2White(3, 3, 1);
  53. }

  54. // Fill strip pixels one after another with a color. Strip is NOT cleared
  55. // first; anything there will be covered pixel by pixel. Pass in color
  56. // (as a single 'packed' 32-bit value, which you can get by calling
  57. // strip.Color(red, green, blue) as shown in the loop() function above),
  58. // and a delay time (in milliseconds) between pixels.
  59. void colorWipe(uint32_t color, int wait) {
  60.   for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  61.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  62.     strip.show();                          //  Update strip to match
  63.     delay(wait);                           //  Pause for a moment
  64.   }
  65. }

  66. void whiteOverRainbow(int whiteSpeed, int whiteLength) {

  67.   if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;

  68.   int      head          = whiteLength - 1;
  69.   int      tail          = 0;
  70.   int      loops         = 3;
  71.   int      loopNum       = 0;
  72.   uint32_t lastTime      = millis();
  73.   uint32_t firstPixelHue = 0;

  74.   for(;;) { // Repeat forever (or until a 'break' or 'return')
  75.     for(int i=0; i<strip.numPixels(); i++) {  // For each pixel in strip...
  76.       if(((i >= tail) && (i <= head)) ||      //  If between head & tail...
  77.          ((tail > head) && ((i >= tail) || (i <= head)))) {
  78.         strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
  79.       } else {                                             // else set rainbow
  80.         int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  81.         strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  82.       }
  83.     }

  84.     strip.show(); // Update strip with new contents
  85.     // There's no delay here, it just runs full-tilt until the timer and
  86.     // counter combination below runs out.

  87.     firstPixelHue += 40; // Advance just a little along the color wheel

  88.     if((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
  89.       if(++head >= strip.numPixels()) {      // Advance head, wrap around
  90.         head = 0;
  91.         if(++loopNum >= loops) return;
  92.       }
  93.       if(++tail >= strip.numPixels()) {      // Advance tail, wrap around
  94.         tail = 0;
  95.       }
  96.       lastTime = millis();                   // Save time of last movement
  97.     }
  98.   }
  99. }

  100. void pulseWhite(uint8_t wait) {
  101.   for(int j=0; j<256; j++) { // Ramp up from 0 to 255
  102.     // Fill entire strip with white at gamma-corrected brightness level 'j':
  103.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  104.     strip.show();
  105.     delay(wait);
  106.   }

  107.   for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
  108.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  109.     strip.show();
  110.     delay(wait);
  111.   }
  112. }

  113. void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {
  114.   int fadeVal=0, fadeMax=100;

  115.   // Hue of first pixel runs 'rainbowLoops' complete loops through the color
  116.   // wheel. Color wheel has a range of 65536 but it's OK if we roll over, so
  117.   // just count from 0 to rainbowLoops*65536, using steps of 256 so we
  118.   // advance around the wheel at a decent clip.
  119.   for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536;
  120.     firstPixelHue += 256) {

  121.     for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...

  122.       // Offset pixel hue by an amount to make one full revolution of the
  123.       // color wheel (range of 65536) along the length of the strip
  124.       // (strip.numPixels() steps):
  125.       uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());

  126.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  127.       // optionally add saturation and value (brightness) (each 0 to 255).
  128.       // Here we're using just the three-argument variant, though the
  129.       // second value (saturation) is a constant 255.
  130.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
  131.         255 * fadeVal / fadeMax)));
  132.     }

  133.     strip.show();
  134.     delay(wait);

  135.     if(firstPixelHue < 65536) {                              // First loop,
  136.       if(fadeVal < fadeMax) fadeVal++;                       // fade in
  137.     } else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,
  138.       if(fadeVal > 0) fadeVal--;                             // fade out
  139.     } else {
  140.       fadeVal = fadeMax; // Interim loop, make sure fade is at max
  141.     }
  142.   }

  143.   for(int k=0; k<whiteLoops; k++) {
  144.     for(int j=0; j<256; j++) { // Ramp up 0 to 255
  145.       // Fill entire strip with white at gamma-corrected brightness level 'j':
  146.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  147.       strip.show();
  148.     }
  149.     delay(1000); // Pause 1 second
  150.     for(int j=255; j>=0; j--) { // Ramp down 255 to 0
  151.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  152.       strip.show();
  153.     }
  154.   }

  155.   delay(500); // Pause 1/2 second
  156. }
复制代码

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

本版积分规则

小黑屋|Archiver|手机版|Arduino爱好者

GMT+8, 2023-1-29 04:48 , Processed in 0.071851 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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