【雕爷学编程】Arduino动手做(138)---64位WS2812点阵屏模块5
37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
项目二十四:将噪声数据转换为 LED 阵列中的动态颜色
实验开源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | /* 【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真) 实验一百四十六:64位WS2812B 8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 项目二十四:将噪声数据转换为 LED 阵列中的动态颜色 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include <FastLED.h> #define LED_PIN 6 #define BRIGHTNESS 26 #define LED_TYPE WS2811 #define COLOR_ORDER GRB // Params for width and height const uint8_t kMatrixWidth = 8; const uint8_t kMatrixHeight = 8; // Param for different pixel layouts const bool kMatrixSerpentineLayout = true ; // This example combines two features of FastLED to produce a remarkable range of // effects from a relatively small amount of code. This example combines FastLED's // color palette lookup functions with FastLED's Perlin noise generator, and // the combination is extremely powerful. // // You might want to look at the "ColorPalette" and "Noise" examples separately // if this example code seems daunting. // // // The basic setup here is that for each frame, we generate a new array of // 'noise' data, and then map it onto the LED matrix through a color palette. // // Periodically, the color palette is changed, and new noise-generation parameters // are chosen at the same time. In this example, specific noise-generation // values have been selected to match the given color palettes; some are faster, // or slower, or larger, or smaller than others, but there's no reason these // parameters can't be freely mixed-and-matched. // // In addition, this example includes some fast automatic 'data smoothing' at // lower noise speeds to help produce smoother animations in those cases. // // The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are // used, as well as some 'hand-defined' ones, and some proceedurally generated // palettes. #define NUM_LEDS (kMatrixWidth * kMatrixHeight) #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight) // The leds CRGB leds[kMatrixWidth * kMatrixHeight]; // The 16 bit version of our coordinates static uint16_t x; static uint16_t y; static uint16_t z; // We're using the x/y dimensions to map to the x/y pixels on the matrix. We'll // use the z-axis for "time". speed determines how fast time moves forward. Try // 1 for a very slow moving effect, or 60 for something that ends up looking like // water. uint16_t speed = 20; // speed is set dynamically once we've started up // Scale determines how far apart the pixels in our noise matrix are. Try // changing these values around to see how it affects the motion of the display. The // higher the value of scale, the more "zoomed out" the noise iwll be. A value // of 1 will be so zoomed in, you'll mostly see solid colors. uint16_t scale = 30; // scale is set dynamically once we've started up // This is the array that we keep our computed noise values in uint8_t noise[MAX_DIMENSION][MAX_DIMENSION]; CRGBPalette16 currentPalette( PartyColors_p ); uint8_t colorLoop = 1; void setup() { delay(3000); FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); FastLED.setBrightness(BRIGHTNESS); // Initialize our coordinates to some random values x = random16(); y = random16(); z = random16(); } // Fill the x/y array of 8-bit noise values using the inoise8 function. void fillnoise8() { // If we're runing at a low "speed", some 8-bit artifacts become visible // from frame-to-frame. In order to reduce this, we can do some fast data-smoothing. // The amount of data smoothing we're doing depends on "speed". uint8_t dataSmoothing = 0; if ( speed < 50) { dataSmoothing = 200 - (speed * 4); } for ( int i = 0; i < MAX_DIMENSION; i++) { int ioffset = scale * i; for ( int j = 0; j < MAX_DIMENSION; j++) { int joffset = scale * j; uint8_t data = inoise8(x + ioffset, y + joffset, z); // The range of the inoise8 function is roughly 16-238. // These two operations expand those values out to roughly 0..255 // You can comment them out if you want the raw noise data. data = qsub8(data, 16); data = qadd8(data, scale8(data, 39)); if ( dataSmoothing ) { uint8_t olddata = noise[i][j]; uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing); data = newdata; } noise[i][j] = data; } } z += speed; // apply slow drift to X and Y, just for visual variation. x += speed / 8; y -= speed / 16; } void mapNoiseToLEDsUsingPalette() { static uint8_t ihue = 0; for ( int i = 0; i < kMatrixWidth; i++) { for ( int j = 0; j < kMatrixHeight; j++) { // We use the value at the (i,j) coordinate in the noise // array for our brightness, and the flipped value from (j,i) // for our pixel's index into the color palette. uint8_t index = noise[j][i]; uint8_t bri = noise[i][j]; // if this palette is a 'loop', add a slowly-changing base value if ( colorLoop) { index += ihue; } // brighten up, as the color palette itself often contains the // light/dark dynamic range desired if ( bri > 127 ) { bri = 255; } else { bri = dim8_raw( bri * 2); } CRGB color = ColorFromPalette( currentPalette, index, bri); leds[XY(i, j)] = color; } } ihue += 1; } void loop() { // Periodically choose a new palette, speed, and scale ChangePaletteAndSettingsPeriodically(); // generate noise data fillnoise8(); // convert the noise data to colors in the LED array // using the current palette mapNoiseToLEDsUsingPalette(); FastLED.show(); // delay(10); } // There are several different palettes of colors demonstrated here. // // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p, // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p. // // Additionally, you can manually define your own color palettes, or you can write // code that creates color palettes on the fly. // 1 = 5 sec per palette // 2 = 10 sec per palette // etc #define HOLD_PALETTES_X_TIMES_AS_LONG 1 void ChangePaletteAndSettingsPeriodically() { uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60; static uint8_t lastSecond = 99; if ( lastSecond != secondHand) { lastSecond = secondHand; if ( secondHand == 0) { currentPalette = RainbowColors_p; speed = 20; scale = 30; colorLoop = 1; } if ( secondHand == 5) { SetupPurpleAndGreenPalette(); speed = 10; scale = 50; colorLoop = 1; } if ( secondHand == 10) { SetupBlackAndWhiteStripedPalette(); speed = 20; scale = 30; colorLoop = 1; } if ( secondHand == 15) { currentPalette = ForestColors_p; speed = 8; scale = 120; colorLoop = 0; } if ( secondHand == 20) { currentPalette = CloudColors_p; speed = 4; scale = 30; colorLoop = 0; } if ( secondHand == 25) { currentPalette = LavaColors_p; speed = 8; scale = 50; colorLoop = 0; } if ( secondHand == 30) { currentPalette = OceanColors_p; speed = 20; scale = 90; colorLoop = 0; } if ( secondHand == 35) { currentPalette = PartyColors_p; speed = 20; scale = 30; colorLoop = 1; } if ( secondHand == 40) { SetupRandomPalette(); speed = 20; scale = 20; colorLoop = 1; } if ( secondHand == 45) { SetupRandomPalette(); speed = 50; scale = 50; colorLoop = 1; } if ( secondHand == 50) { SetupRandomPalette(); speed = 90; scale = 90; colorLoop = 1; } if ( secondHand == 55) { currentPalette = RainbowStripeColors_p; speed = 30; scale = 20; colorLoop = 1; } } } // This function generates a random palette that's a gradient // between four different colors. The first is a dim hue, the second is // a bright hue, the third is a bright pastel, and the last is // another bright hue. This gives some visual bright/dark variation // which is more interesting than just a gradient of different hues. void SetupRandomPalette() { currentPalette = CRGBPalette16( CHSV( random8(), 255, 32), CHSV( random8(), 255, 255), CHSV( random8(), 128, 255), CHSV( random8(), 255, 255)); } // This function sets up a palette of black and white stripes, // using code. Since the palette is effectively an array of // sixteen CRGB colors, the various fill_* functions can be used // to set them up. void SetupBlackAndWhiteStripedPalette() { // 'black out' all 16 palette entries... fill_solid( currentPalette, 16, CRGB::Black); // and set every fourth one to white. currentPalette[0] = CRGB::White; currentPalette[4] = CRGB::White; currentPalette[8] = CRGB::White; currentPalette[12] = CRGB::White; } // This function sets up a palette of purple and green stripes. void SetupPurpleAndGreenPalette() { CRGB purple = CHSV( HUE_PURPLE, 255, 255); CRGB green = CHSV( HUE_GREEN, 255, 255); CRGB black = CRGB::Black; currentPalette = CRGBPalette16( green, green, black, black, purple, purple, black, black, green, green, black, black, purple, purple, black, black ); } // // Mark's xy coordinate mapping code. See the XYMatrix for more information on it. // uint16_t XY( uint8_t x, uint8_t y) { uint16_t i; if ( kMatrixSerpentineLayout == false ) { i = (y * kMatrixWidth) + x; } if ( kMatrixSerpentineLayout == true ) { if ( y & 0x01) { // Odd rows run backwards uint8_t reverseX = (kMatrixWidth - 1) - x; i = (y * kMatrixWidth) + reverseX; } else { // Even rows run forwards i = (y * kMatrixWidth) + x; } } return i; } |
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
项目二十五: "太平洋"柔和的蓝绿色海浪
实验开源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | /* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 项目二十五: "太平洋"柔和的蓝绿色海浪 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #define FASTLED_ALLOW_INTERRUPTS 0 #include <FastLED.h> FASTLED_USING_NAMESPACE #define DATA_PIN 6 #define NUM_LEDS 64 #define MAX_POWER_MILLIAMPS 500 #define LED_TYPE WS2812B #define COLOR_ORDER GRB ////////////////////////////////////////////////////////////////////////// CRGB leds[NUM_LEDS]; void setup() { delay( 3000); // 3 second delay for boot recovery, and a moment of silence FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS) .setCorrection( TypicalLEDStrip ); FastLED.setBrightness(30); FastLED.setMaxPowerInVoltsAndMilliamps( 5, MAX_POWER_MILLIAMPS); } void loop() { EVERY_N_MILLISECONDS( 20) { pacifica_loop(); FastLED.show(); } } ////////////////////////////////////////////////////////////////////////// // // The code for this animation is more complicated than other examples, and // while it is "ready to run", and documented in general, it is probably not // the best starting point for learning. Nevertheless, it does illustrate some // useful techniques. // ////////////////////////////////////////////////////////////////////////// // // In this animation, there are four "layers" of waves of light. // // Each layer moves independently, and each is scaled separately. // // All four wave layers are added together on top of each other, and then // another filter is applied that adds "whitecaps" of brightness where the // waves line up with each other more. Finally, another pass is taken // over the led array to 'deepen' (dim) the blues and greens. // // The speed and scale and motion each layer varies slowly within independent // hand-chosen ranges, which is why the code has a lot of low-speed 'beatsin8' functions // with a lot of oddly specific numeric ranges. // // These three custom blue-green color palettes were inspired by the colors found in // the waters off the southern coast of California, https://goo.gl/maps/QQgd97jjHesHZVxQ7 // CRGBPalette16 pacifica_palette_1 = { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117, 0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x14554B, 0x28AA50 }; CRGBPalette16 pacifica_palette_2 = { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117, 0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x0C5F52, 0x19BE5F }; CRGBPalette16 pacifica_palette_3 = { 0x000208, 0x00030E, 0x000514, 0x00061A, 0x000820, 0x000927, 0x000B2D, 0x000C33, 0x000E39, 0x001040, 0x001450, 0x001860, 0x001C70, 0x002080, 0x1040BF, 0x2060FF }; void pacifica_loop() { // Increment the four "color index start" counters, one for each wave layer. // Each is incremented at a different speed, and the speeds vary over time. static uint16_t sCIStart1, sCIStart2, sCIStart3, sCIStart4; static uint32_t sLastms = 0; uint32_t ms = GET_MILLIS(); uint32_t deltams = ms - sLastms; sLastms = ms; uint16_t speedfactor1 = beatsin16(3, 179, 269); uint16_t speedfactor2 = beatsin16(4, 179, 269); uint32_t deltams1 = (deltams * speedfactor1) / 256; uint32_t deltams2 = (deltams * speedfactor2) / 256; uint32_t deltams21 = (deltams1 + deltams2) / 2; sCIStart1 += (deltams1 * beatsin88(1011, 10, 13)); sCIStart2 -= (deltams21 * beatsin88(777, 8, 11)); sCIStart3 -= (deltams1 * beatsin88(501, 5, 7)); sCIStart4 -= (deltams2 * beatsin88(257, 4, 6)); // Clear out the LED array to a dim background blue-green fill_solid( leds, NUM_LEDS, CRGB( 2, 6, 10)); // Render each of four layers, with different scales and speeds, that vary over time pacifica_one_layer( pacifica_palette_1, sCIStart1, beatsin16( 3, 11 * 256, 14 * 256), beatsin8( 10, 70, 130), 0 - beat16( 301) ); pacifica_one_layer( pacifica_palette_2, sCIStart2, beatsin16( 4, 6 * 256, 9 * 256), beatsin8( 17, 40, 80), beat16( 401) ); pacifica_one_layer( pacifica_palette_3, sCIStart3, 6 * 256, beatsin8( 9, 10, 38), 0 - beat16(503)); pacifica_one_layer( pacifica_palette_3, sCIStart4, 5 * 256, beatsin8( 8, 10, 28), beat16(601)); // Add brighter 'whitecaps' where the waves lines up more pacifica_add_whitecaps(); // Deepen the blues and greens a bit pacifica_deepen_colors(); } // Add one layer of waves into the led array void pacifica_one_layer( CRGBPalette16& p, uint16_t cistart, uint16_t wavescale, uint8_t bri, uint16_t ioff) { uint16_t ci = cistart; uint16_t waveangle = ioff; uint16_t wavescale_half = (wavescale / 2) + 20; for ( uint16_t i = 0; i < NUM_LEDS; i++) { waveangle += 250; uint16_t s16 = sin16( waveangle ) + 32768; uint16_t cs = scale16( s16 , wavescale_half ) + wavescale_half; ci += cs; uint16_t sindex16 = sin16( ci) + 32768; uint8_t sindex8 = scale16( sindex16, 240); CRGB c = ColorFromPalette( p, sindex8, bri, LINEARBLEND); leds[i] += c; } } // Add extra 'white' to areas where the four layers of light have lined up brightly void pacifica_add_whitecaps() { uint8_t basethreshold = beatsin8( 9, 55, 65); uint8_t wave = beat8( 7 ); for ( uint16_t i = 0; i < NUM_LEDS; i++) { uint8_t threshold = scale8( sin8( wave), 20) + basethreshold; wave += 7; uint8_t l = leds[i].getAverageLight(); if ( l > threshold) { uint8_t overage = l - threshold; uint8_t overage2 = qadd8( overage, overage); leds[i] += CRGB( overage, overage2, qadd8( overage2, overage2)); } } } // Deepen the blues and greens void pacifica_deepen_colors() { for ( uint16_t i = 0; i < NUM_LEDS; i++) { leds[i].blue = scale8( leds[i].blue, 145); leds[i].green = scale8( leds[i].green, 200); leds[i] |= CRGB( 2, 5, 7); } } |
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
项目二十六:简单的WS2812FX自动模式循环
实验开源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /* 【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程) 实验一百四十六:64位WS2812B 8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 项目二十六:简单的WS2812FX自动模式循环 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include <WS2812FX.h> #define LED_COUNT 64 #define LED_PIN 6 #define TIMER_MS 5000 // Parameter 1 = number of pixels in strip // Parameter 2 = Arduino pin number (most are valid) // Parameter 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) WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800); unsigned long last_change = 0; unsigned long now = 0; void setup() { ws2812fx.init(); ws2812fx.setBrightness(25); ws2812fx.setSpeed(1000); ws2812fx.setColor(0x007BFF); ws2812fx.setMode(FX_MODE_STATIC); ws2812fx.start(); } void loop() { now = millis(); ws2812fx.service(); if (now - last_change > TIMER_MS) { ws2812fx.setMode((ws2812fx.getMode() + 1) % ws2812fx.getModeCount()); last_change = now; } } |
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
项目二十七:模拟的火焰山动画
实验开源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | /* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 项目二十七:模拟的火焰山动画 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include "FastLED.h" // be sure to install and include the FastLED lib #include <WS2812FX.h> #define NUM_LEDS 64 #define LED_PIN 6 WS2812FX ws2812fx = WS2812FX(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); // declare global parameters used by Fire2012 bool gReverseDirection = false ; void setup() { Serial.begin(115200); // init WS2812FX to use a custom effect ws2812fx.init(); ws2812fx.setBrightness(25); ws2812fx.setColor(BLUE); ws2812fx.setSpeed(1000); ws2812fx.setMode(FX_MODE_CUSTOM); ws2812fx.setCustomMode(myCustomEffect); ws2812fx.start(); } void loop() { ws2812fx.service(); } // in the custom effect run the Fire2012 algorithm uint16_t myCustomEffect() { Fire2012(); // return the animation speed based on the ws2812fx speed setting return (ws2812fx.getSpeed() / NUM_LEDS); } /* paste in the Fire2012 code with a small edit at the end which uses the setPixelColor() function to copy the color data to the ws2812fx instance. */ // Fire2012 by Mark Kriegsman, July 2012 // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY //// // This basic one-dimensional 'fire' simulation works roughly as follows: // There's a underlying array of 'heat' cells, that model the temperature // at each point along the line. Every cycle through the simulation, // four steps are performed: // 1) All cells cool down a little bit, losing heat to the air // 2) The heat from each cell drifts 'up' and diffuses a little // 3) Sometimes randomly new 'sparks' of heat are added at the bottom // 4) The heat from each cell is rendered as a color into the leds array // The heat-to-color mapping uses a black-body radiation approximation. // // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot). // // This simulation scales it self a bit depending on NUM_LEDS; it should look // "OK" on anywhere from 20 to 100 LEDs without too much tweaking. // // I recommend running this simulation at anywhere from 30-100 frames per second, // meaning an interframe delay of about 10-35 milliseconds. // // Looks best on a high-density LED setup (60+ pixels/meter). // // // There are two main parameters you can play with to control the look and // feel of your fire: COOLING (used in step 1 above), and SPARKING (used // in step 3 above). // // COOLING: How much does the air cool as it rises? // Less cooling = taller flames. More cooling = shorter flames. // Default 50, suggested range 20-100 #define COOLING 55 // SPARKING: What chance (out of 255) is there that a new spark will be lit? // Higher chance = more roaring fire. Lower chance = more flickery fire. // Default 120, suggested range 50-200. #define SPARKING 120 void Fire2012() { // Array of temperature readings at each simulation cell static byte heat[NUM_LEDS]; // Step 1. Cool down every cell a little for ( int i = 0; i < NUM_LEDS; i++) { heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2)); } // Step 2. Heat from each cell drifts 'up' and diffuses a little for ( int k = NUM_LEDS - 1; k >= 2; k--) { heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; } // Step 3. Randomly ignite new 'sparks' of heat near the bottom if ( random8() < SPARKING ) { int y = random8(7); heat[y] = qadd8( heat[y], random8(160, 255) ); } // Step 4. Map from heat cells to LED colors for ( int j = 0; j < NUM_LEDS; j++) { CRGB color = HeatColor( heat[j]); int pixelnumber; if ( gReverseDirection ) { pixelnumber = (NUM_LEDS - 1) - j; } else { pixelnumber = j; } // **** modified for use with WS2812FX **** //leds[pixelnumber] = color; ws2812fx.setPixelColor(pixelnumber, color.red, color.green, color.blue); // **** modified for use with WS2812FX **** } } |
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
项目二十八:随机追逐的彗星效果
实验开源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | /* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 项目二十八:随机追逐的彗星效果 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include <WS2812FX.h> #define LED_COUNT 64 #define LED_PIN 6 WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); void setup() { Serial.begin(115200); ws2812fx.init(); ws2812fx.setBrightness(25); // segment 0 is the builtin comet effect ws2812fx.setSegment(0, 0, LED_COUNT / 2 - 1, FX_MODE_COMET, RED, 1000, false ); // segment 1 is our custom effect ws2812fx.setCustomMode(myCustomEffect); ws2812fx.setSegment(1, LED_COUNT / 2, LED_COUNT - 1, FX_MODE_CUSTOM, RED, 50, false ); ws2812fx.start(); } void loop() { ws2812fx.service(); } uint16_t myCustomEffect( void ) { // random chase WS2812FX::Segment* seg = ws2812fx.getSegment(); // get the current segment for (uint16_t i = seg->stop; i > seg->start; i--) { ws2812fx.setPixelColor(i, ws2812fx.getPixelColor(i - 1)); } uint32_t color = ws2812fx.getPixelColor(seg->start + 1); int r = random(6) != 0 ? (color >> 16 & 0xFF) : random(256); int g = random(6) != 0 ? (color >> 8 & 0xFF) : random(256); int b = random(6) != 0 ? (color & 0xFF) : random(256); ws2812fx.setPixelColor(seg->start, r, g, b); return seg->speed; // return the delay until the next animation step (in msec) } |
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
项目二十九:自定义效果的演示
实验开源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 项目二十九:自定义效果的演示 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include <WS2812FX.h> //include the custom effects //#include "custom/BlockDissolve.h" #include "custom/DualLarson.h" //#include "custom/MultiComet.h" //#include "custom/Oscillate.h" //#include "custom/RainbowLarson.h" //#include "custom/RandomChase.h" //#include "custom/TriFade.h" //#include "custom/VUMeter.h" #define LED_COUNT 64 #define LED_PIN 6 WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); void setup() { Serial.begin(115200); ws2812fx.init(); ws2812fx.setBrightness(25); // setup the custom effects uint8_t dualLarsonMode = ws2812fx.setCustomMode(F( "Dual Larson" ), dualLarson); uint32_t colors[] = {RED, BLUE, WHITE}; ws2812fx.setSegment(0, 0, LED_COUNT - 1, dualLarsonMode, colors, 2000, FADE_SLOW); ws2812fx.start(); } void loop() { ws2812fx.service(); } |
Arduino实验场景图
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性