【雕爷学编程】Arduino动手做(124)---24位WS2812环形灯板2

2023-10-11 08:10

本文主要是介绍【雕爷学编程】Arduino动手做(124)---24位WS2812环形灯板2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百二十四:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板

在这里插入图片描述

知识点:WS2812

是在寻求一种简单,可扩展和经济实惠的全彩LED的最新进展。红色,绿色和蓝色LED与驱动器芯片一起集成到通过单线控制的微小表面贴装封装中。它们可以单独使用,链接成更长的字符串或组装成更有趣的形状因数。基于WS2812的 LED 驱动器,使用单线控制协议,可分别寻址RGB彩色像素和色带。专用LED驱动器芯片的到来带来了可喜的缓解,减轻了微控制器的繁琐工作,使人们可以专注于应用与创造。

在这里插入图片描述

该24位WS2812环形灯板包含24个可单独寻址的RGB LED,这些LED以紧密间隔排列,所有这些均可通过微控制器的单个数字输出进行控制。该环的外径为2.6英寸(66毫米),可以将多个环链接在一起,使其非常适合为小型机器人或可穿戴电子项目增加光泽。

功能和规格

24个可单独寻址的RGB LED(基于SK6812-或WS2812B的NeoPixels)

圆形外径2.6英寸(66毫米),内径2.05英寸(52.5毫米)

24位色彩控制(每通道8位PWM);每像素1680万种颜色

单线数字控制接口

工作电压:5 V

每个RGB LED在5 V时消耗大约50 mA的电流,红色,绿色和蓝色在全亮度下(环最大为1.2 A)

多个24位WS2812环形灯板可链接在一起

在这里插入图片描述
24位WS2812 5050 RGB LED智能全彩RGB灯环开发板

直径:86mm ,

重量:6g ,

电压:DC4-7V范围供电,

通信接口:单线通讯

LED驱动芯片WS2812(集成在LED里面)

智能反接保护,电源反接不会损坏IC。

IC控制电路与LED点光源公用一个电源。

控制电路与RGB晶片集成在一个5050封装的元器件中,构成一个完整的外控图元点。

内置信号整形电路,任何一个图元点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。

内置上电重定和掉电重定电路。

每个图元点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。

串列级联介面,能通过一根信号线完成资料的接收与解码。

任意两点传传输距离在不超过5米时无需增加任何电路。

当刷新速率30帧/秒时,级联数不小于1024点。

资料发送速度可达800Kbps。

光的颜色高度一致,性价比高。

在这里插入图片描述
在这里插入图片描述
模块参考电原理图

在这里插入图片描述
Arduino实验接线示意图

在这里插入图片描述

Arduino实验开源代码

/*【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)实验一百二十四:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板项目之十七:WS2812FX库最简单的点亮形式*/#include <WS2812FX.h> //导入库#define LED_COUNT 24 //WS2812B LED数量#define LED_PIN  6 //WS2812B LED接脚WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);void setup() {ws2812fx.init(); //初始化ws2812fx.setBrightness(35); //设置亮度(0-255),可以控制总电流(重要!)ws2812fx.setSpeed(100); // 设置速度ws2812fx.setMode(FX_MODE_FIREWORKS_RANDOM);// 设置模式(内置63种模式)ws2812fx.start(); //启动}void loop() {ws2812fx.service(); //循环运行}

Arduino实验场景图

在这里插入图片描述

/*【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板项目三:使用红色、绿色和蓝色三种参数将任何LED设置为任何颜色Module      UNOVCC   ——   5VGND  ——   GNDDI    ——   D6
*/#include <FastLED.h>
#define LED_PIN     6
#define NUM_LEDS    24
CRGB leds[NUM_LEDS];
void setup() {FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);}
void loop() {leds[0] = CRGB(255, 0, 0);FastLED.show();delay(500);  leds[1] = CRGB(0, 255, 0);FastLED.show();delay(500);leds[2] = CRGB(0, 0, 255);FastLED.show();delay(500);leds[5] = CRGB(150, 0, 255);FastLED.show();delay(500);leds[9] = CRGB(255, 200, 20);FastLED.show();delay(500);leds[14] = CRGB(85, 60, 180);FastLED.show();delay(500);leds[19] = CRGB(50, 255, 20);FastLED.show();delay(500);
}
/*【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板项目四:流水灯变幻彩虹灯Module      UNOVCC   ——   5VGND  ——   GNDDI    ——   D6
*/#include <Adafruit_NeoPixel.h>#define PIN 6
#define BRIGHTNESS 24Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);void setup() {strip.setBrightness(BRIGHTNESS);strip.begin();strip.show();
}void loop() {colorWipe(strip.Color(150, 0, 0), 50); // RedcolorWipe(strip.Color(0, 150, 0), 50); // GreencolorWipe(strip.Color(0, 0, 150), 50); // BluecolorWipe(strip.Color(150, 150, 150), 50); // BlueWiterainbowCycle(1);}void colorWipe(uint32_t c, uint8_t wait) {for (uint16_t i = 0; i < strip.numPixels(); i++) {strip.setPixelColor(i, c);strip.show();delay(wait);}
}void rainbow(uint8_t wait) {uint16_t i, j;for (j = 0; j < 256; j++) {for (i = 0; i < strip.numPixels(); i++) {strip.setPixelColor(i, Wheel((i + j) & 255 ));}strip.show();delay(wait);}
}void rainbowCycle(uint8_t wait) {uint16_t i, j;for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheelfor (i = 0; i < strip.numPixels(); i++) {strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));}strip.show();delay(wait);}
}uint32_t Wheel(byte WheelPos) {if (WheelPos < 85) {return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);} else if (WheelPos < 170) {WheelPos -= 85;return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);} else {WheelPos -= 170;return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);}
}
/*【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板项目五:循环流水变幻呼吸灯Module      UNOVCC   ——   5VGND  ——   GNDDI    ——   D6
*/// NeoPixel test program showing use of the WHITE channel for RGBW
// pixels only (won't look correct on regular RGB NeoPixel strips).#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN     6// How many NeoPixels are attached to the Arduino?
#define LED_COUNT  24// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 50// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)void setup() {// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)clock_prescale_set(clock_div_1);
#endif// END of Trinket-specific code.strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)strip.show();            // Turn OFF all pixels ASAPstrip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}void loop() {// Fill along the length of the strip in various colors...colorWipe(strip.Color(255,   0,   0)     , 50); // RedcolorWipe(strip.Color(  0, 255,   0)     , 50); // GreencolorWipe(strip.Color(  0,   0, 255)     , 50); // BluecolorWipe(strip.Color(  0,   0,   0, 255), 50); // True white (not RGB white)whiteOverRainbow(75, 5);pulseWhite(5);rainbowFade2White(3, 3, 1);
}// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)strip.show();                          //  Update strip to matchdelay(wait);                           //  Pause for a moment}
}void whiteOverRainbow(int whiteSpeed, int whiteLength) {if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;int      head          = whiteLength - 1;int      tail          = 0;int      loops         = 3;int      loopNum       = 0;uint32_t lastTime      = millis();uint32_t firstPixelHue = 0;for(;;) { // Repeat forever (or until a 'break' or 'return')for(int i=0; i<strip.numPixels(); i++) {  // For each pixel in strip...if(((i >= tail) && (i <= head)) ||      //  If between head & tail...((tail > head) && ((i >= tail) || (i <= head)))) {strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white} else {                                             // else set rainbowint pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));}}strip.show(); // Update strip with new contents// There's no delay here, it just runs full-tilt until the timer and// counter combination below runs out.firstPixelHue += 40; // Advance just a little along the color wheelif((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?if(++head >= strip.numPixels()) {      // Advance head, wrap aroundhead = 0;if(++loopNum >= loops) return;}if(++tail >= strip.numPixels()) {      // Advance tail, wrap aroundtail = 0;}lastTime = millis();                   // Save time of last movement}}
}void pulseWhite(uint8_t wait) {for(int j=0; j<256; j++) { // Ramp up from 0 to 255// Fill entire strip with white at gamma-corrected brightness level 'j':strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));strip.show();delay(wait);}for(int j=255; j>=0; j--) { // Ramp down from 255 to 0strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));strip.show();delay(wait);}
}void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {int fadeVal=0, fadeMax=100;// Hue of first pixel runs 'rainbowLoops' complete loops through the color// wheel. Color wheel has a range of 65536 but it's OK if we roll over, so// just count from 0 to rainbowLoops*65536, using steps of 256 so we// advance around the wheel at a decent clip.for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536;firstPixelHue += 256) {for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...// Offset pixel hue by an amount to make one full revolution of the// color wheel (range of 65536) along the length of the strip// (strip.numPixels() steps):uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or// optionally add saturation and value (brightness) (each 0 to 255).// Here we're using just the three-argument variant, though the// second value (saturation) is a constant 255.strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,255 * fadeVal / fadeMax)));}strip.show();delay(wait);if(firstPixelHue < 65536) {                              // First loop,if(fadeVal < fadeMax) fadeVal++;                       // fade in} else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,if(fadeVal > 0) fadeVal--;                             // fade out} else {fadeVal = fadeMax; // Interim loop, make sure fade is at max}}for(int k=0; k<whiteLoops; k++) {for(int j=0; j<256; j++) { // Ramp up 0 to 255// Fill entire strip with white at gamma-corrected brightness level 'j':strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));strip.show();}delay(1000); // Pause 1 secondfor(int j=255; j>=0; j--) { // Ramp down 255 to 0strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));strip.show();}}delay(500); // Pause 1/2 second
}
/*【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板项目六:多彩流水灯变幻彩虹灯Module      UNOVCC   ——   5VGND  ——   GNDDI    ——   D6
*/#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    6// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 24// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)// setup() function -- runs once at startup --------------------------------void setup() {// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)clock_prescale_set(clock_div_1);
#endif// END of Trinket-specific code.strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)strip.show();            // Turn OFF all pixels ASAPstrip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}// loop() function -- runs repeatedly as long as board is on ---------------void loop() {// Fill along the length of the strip in various colors...colorWipe(strip.Color(255,   0,   0), 50); // RedcolorWipe(strip.Color(  0, 255,   0), 50); // GreencolorWipe(strip.Color(  0,   0, 255), 50); // Blue// Do a theater marquee effect in various colors...theaterChase(strip.Color(127, 127, 127), 50); // White, half brightnesstheaterChase(strip.Color(127,   0,   0), 50); // Red, half brightnesstheaterChase(strip.Color(  0,   0, 127), 50); // Blue, half brightnessrainbow(10);             // Flowing rainbow cycle along the whole striptheaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
}// Some functions of our own for creating animated effects -----------------// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)strip.show();                          //  Update strip to matchdelay(wait);                           //  Pause for a moment}
}// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase(uint32_t color, int wait) {for (int a = 0; a < 10; a++) { // Repeat 10 times...for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...strip.clear();         //   Set all pixels in RAM to 0 (off)// 'c' counts up from 'b' to end of strip in steps of 3...for (int c = b; c < strip.numPixels(); c += 3) {strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'}strip.show(); // Update strip with new contentsdelay(wait);  // Pause for a moment}}
}// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {// Hue of first pixel runs 5 complete loops through the color wheel.// Color wheel has a range of 65536 but it's OK if we roll over, so// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time// means we'll make 5*65536/256 = 1280 passes through this outer loop:for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...// Offset pixel hue by an amount to make one full revolution of the// color wheel (range of 65536) along the length of the strip// (strip.numPixels() steps):int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or// optionally add saturation and value (brightness) (each 0 to 255).// Here we're using just the single-argument hue variant. The result// is passed through strip.gamma32() to provide 'truer' colors// before assigning to each pixel:strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));}strip.show(); // Update strip with new contentsdelay(wait);  // Pause for a moment}
}// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {int firstPixelHue = 0;     // First pixel starts at red (hue 0)for (int a = 0; a < 30; a++) { // Repeat 30 times...for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...strip.clear();         //   Set all pixels in RAM to 0 (off)// 'c' counts up from 'b' to end of strip in increments of 3...for (int c = b; c < strip.numPixels(); c += 3) {// hue of pixel 'c' is offset by an amount to make one full// revolution of the color wheel (range 65536) along the length// of the strip (strip.numPixels() steps):int      hue   = firstPixelHue + c * 65536L / strip.numPixels();uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGBstrip.setPixelColor(c, color); // Set pixel 'c' to value 'color'}strip.show();                // Update strip with new contentsdelay(wait);                 // Pause for a momentfirstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames}}
}
/*【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板项目七:多彩流水灯变幻彩虹灯之三Module      UNOVCC   ——   5VGND  ——   GNDDI    ——   D6
*/#include <Adafruit_NeoPixel.h>    //needed for the WS2812
#include <avr/pgmspace.h>         //needed for PROGMEM#define PIN 6                    //Pin 1 is DATA In on the bottom Ring
#define BRIGHTNESS 24             // brightness reduced//Lookup for the Candle light
const unsigned int candles[] PROGMEM =
{15, 10, 48, 45, 36, 19, 59, 29, 5, 43, 41, 39, 24, 3, 61
};Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.void setup() {pinMode(PIN, OUTPUT);strip.begin();strip.setBrightness(BRIGHTNESS); // set brightnessstrip.show(); // Initialize all pixels to 'off'
}void loop() {tree();delay(1000);colorcrazy();theaterChaseRainbow(50);comet();warpdrive();warpdrive();rainbowCycle(1);rainbow(5);rainbow(5);rainbow(5);colorWipe(strip.Color(255, 0, 0), 50); // RedcolorWipe(strip.Color(0, 255, 0), 50); // GreencolorWipe(strip.Color(0, 0, 255), 50); // Blue//////  cometr();//Tree light:////  warpdrive();//////  comet();/*// Some example procedures showing how to display to the pixels:colorWipe(strip.Color(255, 0, 0), 50); // RedcolorWipe(strip.Color(0, 255, 0), 50); // GreencolorWipe(strip.Color(0, 0, 255), 50); // Blue// Send a theater pixel chase in...theaterChase(strip.Color(127, 127, 127), 50); // WhitetheaterChase(strip.Color(127,   0,   0), 50); // RedtheaterChase(strip.Color(  0,   0, 127), 50); // Bluerainbow(20);rainbowCycle(20);theaterChaseRainbow(50);*/
}//Sub-----------------------------------------------------------------------//Comet
void comet() {for (uint16_t i = strip.numPixels(); i > 0; i--) {strip.setPixelColor(i, strip.Color(0, 0, 255));fadethemall(10);fadethemall(10);}
}void cometr() {for (uint16_t i = strip.numPixels(); i > 0; i--) {strip.setPixelColor(i, strip.Color(255, 0, 0));fadethemall(10);fadethemall(10);}
}//From top down white pulses
void warpdrive() {//Top Ledstrip.setPixelColor(60, strip.Color(255, 255, 255));strip.show();//fade a bitfor (int i = 0; i < 20; i++){fadethemall(20);}//8 Ringfor (int i = 52; i < 60; i++){strip.setPixelColor(i, strip.Color(255, 255, 255));}strip.show();//fade a bitfor (int i = 0; i < 20; i++){fadethemall(20);}//12 Ringfor (int i = 40; i < 52; i++){strip.setPixelColor(i, strip.Color(255, 255, 255));}strip.show();//fade a bitfor (int i = 0; i < 20; i++){fadethemall(20);}//16 Ringfor (int i = 24; i < 40; i++){strip.setPixelColor(i, strip.Color(255, 255, 255));}strip.show();//fade a bitfor (int i = 0; i < 20; i++){fadethemall(20);}//24 Ringfor (int i = 0; i < 24; i++){strip.setPixelColor(i, strip.Color(255, 255, 255));}strip.show();//fade a bitfor (int i = 0; i < 20; i++){fadethemall(20);}//Extra by John Kerrstrip.setPixelColor(60, strip.Color(0, 0, 0));strip.show();//fade a bitfor (int i = 0; i < 20; i++){fadethemall(20);}}//This reduces the brightness of all leds
void fadethemall(uint8_t wait) {for (uint16_t i = 0; i < strip.numPixels(); i++) {uint32_t color = strip.getPixelColor(i);int r;int g;int b;r = (uint8_t)(color >> 16);g = (uint8_t)(color >>  8);b = (uint8_t)color;if (r > 0){r = r - 1;}else{r = 0;}if (g > 0){g = g - 1;}else{g = 0;}if (b > 0){b = b - 1;}else{b = 0;}strip.setPixelColor(i, strip.Color(r, g, b));}strip.show();delay(wait);
}//This drives the WS2812 in a crazy pattern, fun!
void colorcrazy() {colorWipe(strip.Color(255, 0, 0), 25); // RedcolorWipe(strip.Color(0, 255, 0), 25); // GreencolorWipe(strip.Color(0, 0, 255), 25); // BluetheaterChaseRainbow(5);
}//This lights up the tree in green, then add the white "candles"
void tree() {colorWipe(strip.Color(0, 50, 0), 50); // Green//light "candles"//Show the S:for (int i = 0; i < 16; i++){strip.setPixelColor(pgm_read_word(&candles) - 1, strip.Color(255, 255, 255));strip.show();delay(50);}
}// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {for (uint16_t i = 0; i < strip.numPixels(); i++) {strip.setPixelColor(i, c);strip.show();delay(wait);}
}void rainbow(uint8_t wait) {uint16_t i, j;for (j = 0; j < 256; j++) {for (i = 0; i < strip.numPixels(); i++) {strip.setPixelColor(i, Wheel((i + j) & 255));}strip.show();delay(wait);}
}// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {uint16_t i, j;for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheelfor (i = 0; i < strip.numPixels(); i++) {strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));}strip.show();delay(wait);}
}//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {for (int j = 0; j < 10; j++) { //do 10 cycles of chasingfor (int q = 0; q < 3; q++) {for (int i = 0; i < strip.numPixels(); i = i + 3) {strip.setPixelColor(i + q, c);  //turn every third pixel on}strip.show();delay(wait);for (int i = 0; i < 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) {for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheelfor (int q = 0; q < 3; q++) {for (int i = 0; i < 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) {strip.setPixelColor(i + q, 0);      //turn every third pixel off}}}
}// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {WheelPos = 255 - WheelPos;if (WheelPos < 85) {return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);} else if (WheelPos < 170) {WheelPos -= 85;return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);} else {WheelPos -= 170;return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);}
}

Arduino实验场景图
在这里插入图片描述
在这里插入图片描述

这篇关于【雕爷学编程】Arduino动手做(124)---24位WS2812环形灯板2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/186728

相关文章

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

【编程底层思考】垃圾收集机制,GC算法,垃圾收集器类型概述

Java的垃圾收集(Garbage Collection,GC)机制是Java语言的一大特色,它负责自动管理内存的回收,释放不再使用的对象所占用的内存。以下是对Java垃圾收集机制的详细介绍: 一、垃圾收集机制概述: 对象存活判断:垃圾收集器定期检查堆内存中的对象,判断哪些对象是“垃圾”,即不再被任何引用链直接或间接引用的对象。内存回收:将判断为垃圾的对象占用的内存进行回收,以便重新使用。

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

深入理解RxJava:响应式编程的现代方式

在当今的软件开发世界中,异步编程和事件驱动的架构变得越来越重要。RxJava,作为响应式编程(Reactive Programming)的一个流行库,为Java和Android开发者提供了一种强大的方式来处理异步任务和事件流。本文将深入探讨RxJava的核心概念、优势以及如何在实际项目中应用它。 文章目录 💯 什么是RxJava?💯 响应式编程的优势💯 RxJava的核心概念

函数式编程思想

我们经常会用到各种各样的编程思想,例如面向过程、面向对象。不过笔者在该博客简单介绍一下函数式编程思想. 如果对函数式编程思想进行概括,就是f(x) = na(x) , y=uf(x)…至于其他的编程思想,可能是y=a(x)+b(x)+c(x)…,也有可能是y=f(x)=f(x)/a + f(x)/b+f(x)/c… 面向过程的指令式编程 面向过程,简单理解就是y=a(x)+b(x)+c(x)

arduino ide安装详细步骤

​ 大家好,我是程序员小羊! 前言: Arduino IDE 是一个专为编程 Arduino 微控制器设计的集成开发环境,使用起来非常方便。下面将介绍如何在不同平台上安装 Arduino IDE 的详细步骤,包括 Windows、Mac 和 Linux 系统。 一、在 Windows 上安装 Arduino IDE 1. 下载 Arduino IDE 打开 Arduino 官网

Java并发编程之——BlockingQueue(队列)

一、什么是BlockingQueue BlockingQueue即阻塞队列,从阻塞这个词可以看出,在某些情况下对阻塞队列的访问可能会造成阻塞。被阻塞的情况主要有如下两种: 1. 当队列满了的时候进行入队列操作2. 当队列空了的时候进行出队列操作123 因此,当一个线程试图对一个已经满了的队列进行入队列操作时,它将会被阻塞,除非有另一个线程做了出队列操作;同样,当一个线程试图对一个空

生信代码入门:从零开始掌握生物信息学编程技能

少走弯路,高效分析;了解生信云,访问 【生信圆桌x生信专用云服务器】 : www.tebteb.cc 介绍 生物信息学是一个高度跨学科的领域,结合了生物学、计算机科学和统计学。随着高通量测序技术的发展,海量的生物数据需要通过编程来进行处理和分析。因此,掌握生信编程技能,成为每一个生物信息学研究者的必备能力。 生信代码入门,旨在帮助初学者从零开始学习生物信息学中的编程基础。通过学习常用

rtmp流媒体编程相关整理2013(crtmpserver,rtmpdump,x264,faac)

转自:http://blog.163.com/zhujiatc@126/blog/static/1834638201392335213119/ 相关资料在线版(不定时更新,其实也不会很多,也许一两个月也不会改) http://www.zhujiatc.esy.es/crtmpserver/index.htm 去年在这进行rtmp相关整理,其实内容早有了,只是整理一下看着方