驴友花雕
发表于 2022-10-23 11:13:14
实验场景图动态图
驴友花雕
发表于 2022-10-23 11:27:42
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十一:骄傲2015动画,不断变化的彩虹
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十一:骄傲2015动画,不断变化的彩虹
*/
#include "FastLED.h"
// Pride2015
// Animated, ever-changing rainbows.
// by Mark Kriegsman
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN 6
//#define CLK_PIN 4
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 256
#define BRIGHTNESS22
CRGB leds;
void setup() {
delay(1000); // 3 second delay for recovery
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
.setCorrection(TypicalLEDStrip)
.setDither(BRIGHTNESS < 255);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}
void loop()
{
pride();
FastLED.show();
}
// This function draws rainbows with an ever-changing,
// widely-varying set of parameters.
void pride()
{
static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;
uint8_t sat8 = beatsin88( 87, 220, 250);
uint8_t brightdepth = beatsin88( 341, 96, 224);
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;//gHue * 256;
uint16_t hueinc16 = beatsin88(113, 1, 3000);
uint16_t ms = millis();
uint16_t deltams = ms - sLastMillis ;
sLastMillis= ms;
sPseudotime += deltams * msmultiplier;
sHue16 += deltams * beatsin88( 400, 5,9);
uint16_t brightnesstheta16 = sPseudotime;
for( uint16_t i = 0 ; i < NUM_LEDS; i++) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;
brightnesstheta16+= brightnessthetainc16;
uint16_t b16 = sin16( brightnesstheta16) + 32768;
uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);
CRGB newcolor = CHSV( hue8, sat8, bri8);
uint16_t pixelnumber = i;
pixelnumber = (NUM_LEDS-1) - pixelnumber;
nblend( leds, newcolor, 64);
}
}
驴友花雕
发表于 2022-10-23 11:30:06
实验场景图
驴友花雕
发表于 2022-10-23 11:43:04
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十二:TwinkleFOX-淡入淡出的闪烁“假日”灯
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十二:TwinkleFOX-淡入淡出的闪烁“假日”灯
*/
#include "FastLED.h"
#define NUM_LEDS 256
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define DATA_PIN 6
//#define CLK_PIN 4
#define VOLTS 12
#define MAX_MA 4000
//TwinkleFOX: Twinkling 'holiday' lights that fade in and out.
//Colors are chosen from a palette; a few palettes are provided.
//
//This December 2015 implementation improves on the December 2014 version
//in several ways:
//- smoother fading, compatible with any colors and any palettes
//- easier control of twinkle speed and twinkle density
//- supports an optional 'background color'
//- takes even less RAM: zero RAM overhead per pixel
//- illustrates a couple of interesting techniques (uh oh...)
//
//The idea behind this (new) implementation is that there's one
//basic, repeating pattern that each pixel follows like a waveform:
//The brightness rises from 0..255 and then falls back down to 0.
//The brightness at any given point in time can be determined as
//as a function of time, for example:
// brightness = sine( time ); // a sine wave of brightness over time
//
//So the way this implementation works is that every pixel follows
//the exact same wave function over time.In this particular case,
//I chose a sawtooth triangle wave (triwave8) rather than a sine wave,
//but the idea is the same: brightness = triwave8( time ).
//
//Of course, if all the pixels used the exact same wave form, and
//if they all used the exact same 'clock' for their 'time base', all
//the pixels would brighten and dim at once -- which does not look
//like twinkling at all.
//
//So to achieve random-looking twinkling, each pixel is given a
//slightly different 'clock' signal.Some of the clocks run faster,
//some run slower, and each 'clock' also has a random offset from zero.
//The net result is that the 'clocks' for all the pixels are always out
//of sync from each other, producing a nice random distribution
//of twinkles.
//
//The 'clock speed adjustment' and 'time offset' for each pixel
//are generated randomly.One (normal) approach to implementing that
//would be to randomly generate the clock parameters for each pixel
//at startup, and store them in some arrays.However, that consumes
//a great deal of precious RAM, and it turns out to be totally
//unnessary!If the random number generate is 'seeded' with the
//same starting value every time, it will generate the same sequence
//of values every time.So the clock adjustment parameters for each
//pixel are 'stored' in a pseudo-random number generator!The PRNG
//is reset, and then the first numbers out of it are the clock
//adjustment parameters for the first pixel, the second numbers out
//of it are the parameters for the second pixel, and so on.
//In this way, we can 'store' a stable sequence of thousands of
//random clock adjustment parameters in literally two bytes of RAM.
//
//There's a little bit of fixed-point math involved in applying the
//clock speed adjustments, which are expressed in eighths.Each pixel's
//clock speed ranges from 8/8ths of the system clock (i.e. 1x) to
//23/8ths of the system clock (i.e. nearly 3x).
//
//On a basic Arduino Uno or Leonardo, this code can twinkle 300+ pixels
//smoothly at over 50 updates per seond.
//
//-Mark Kriegsman, December 2015
CRGBArray<NUM_LEDS> leds;
// Overall twinkle speed.
// 0 (VERY slow) to 8 (VERY fast).
// 4, 5, and 6 are recommended, default is 4.
#define TWINKLE_SPEED 4
// Overall twinkle density.
// 0 (NONE lit) to 8 (ALL lit at once).
// Default is 5.
#define TWINKLE_DENSITY 5
// How often to change color palettes.
#define SECONDS_PER_PALETTE30
// Also: toward the bottom of the file is an array
// called "ActivePaletteList" which controls which color
// palettes are used; you can add or remove color palettes
// from there freely.
// Background color for 'unlit' pixels
// Can be set to CRGB::Black if desired.
CRGB gBackgroundColor = CRGB::Black;
// Example of dim incandescent fairy light background color
// CRGB gBackgroundColor = CRGB(CRGB::FairyLight).nscale8_video(16);
// If AUTO_SELECT_BACKGROUND_COLOR is set to 1,
// then for any palette where the first two entries
// are the same, a dimmed version of that color will
// automatically be used as the background color.
#define AUTO_SELECT_BACKGROUND_COLOR 0
// If COOL_LIKE_INCANDESCENT is set to 1, colors will
// fade out slighted 'reddened', similar to how
// incandescent bulbs change color as they get dim down.
#define COOL_LIKE_INCANDESCENT 1
CRGBPalette16 gCurrentPalette;
CRGBPalette16 gTargetPalette;
void setup() {
delay( 1000 ); //safety startup delay
FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
.setCorrection(TypicalLEDStrip);
FastLED.setBrightness(23);
chooseNextColorPalette(gTargetPalette);
}
void loop()
{
EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
chooseNextColorPalette( gTargetPalette );
}
EVERY_N_MILLISECONDS( 10 ) {
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 12);
}
drawTwinkles( leds);
FastLED.show();
}
//This function loops over each pixel, calculates the
//adjusted 'clock' that this pixel should use, and calls
//"CalculateOneTwinkle" on each pixel.It then displays
//either the twinkle color of the background color,
//whichever is brighter.
void drawTwinkles( CRGBSet& L)
{
// "PRNG16" is the pseudorandom number generator
// It MUST be reset to the same starting value each time
// this function is called, so that the sequence of 'random'
// numbers that it generates is (paradoxically) stable.
uint16_t PRNG16 = 11337;
uint32_t clock32 = millis();
// Set up the background color, "bg".
// if AUTO_SELECT_BACKGROUND_COLOR == 1, and the first two colors of
// the current palette are identical, then a deeply faded version of
// that color is used for the background color
CRGB bg;
if ( (AUTO_SELECT_BACKGROUND_COLOR == 1) &&
(gCurrentPalette == gCurrentPalette )) {
bg = gCurrentPalette;
uint8_t bglight = bg.getAverageLight();
if ( bglight > 64) {
bg.nscale8_video( 16); // very bright, so scale to 1/16th
} else if ( bglight > 16) {
bg.nscale8_video( 64); // not that bright, so scale to 1/4th
} else {
bg.nscale8_video( 86); // dim, scale to 1/3rd.
}
} else {
bg = gBackgroundColor; // just use the explicitly defined background color
}
uint8_t backgroundBrightness = bg.getAverageLight();
for ( CRGB& pixel : L) {
PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
uint16_t myclockoffset16 = PRNG16; // use that number as clock offset
PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
// use that number as clock speed adjustment factor (in 8ths, from 8/8ths to 23/8ths)
uint8_t myspeedmultiplierQ5_3 =((((PRNG16 & 0xFF) >> 4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
uint8_tmyunique8 = PRNG16 >> 8; // get 'salt' value for this pixel
// We now have the adjusted 'clock' for this pixel, now we call
// the function that computes what color the pixel should be based
// on the "brightness = f( time )" idea.
CRGB c = computeOneTwinkle( myclock30, myunique8);
uint8_t cbright = c.getAverageLight();
int16_t deltabright = cbright - backgroundBrightness;
if ( deltabright >= 32 || (!bg)) {
// If the new pixel is significantly brighter than the background color,
// use the new color.
pixel = c;
} else if ( deltabright > 0 ) {
// If the new pixel is just slightly brighter than the background color,
// mix a blend of the new color and the background color
pixel = blend( bg, c, deltabright * 8);
} else {
// if the new pixel is not at all brighter than the background color,
// just use the background color.
pixel = bg;
}
}
}
//This function takes a time in pseudo-milliseconds,
//figures out brightness = f( time ), and also hue = f( time )
//The 'low digits' of the millisecond time are used as
//input to the brightness wave function.
//The 'high digits' are used to select a color, so that the color
//does not change over the course of the fade-in, fade-out
//of one cycle of the brightness wave function.
//The 'high digits' are also used to determine whether this pixel
//should light at all during this cycle, based on the TWINKLE_DENSITY.
CRGB computeOneTwinkle( uint32_t ms, uint8_t salt)
{
uint16_t ticks = ms >> (8 - TWINKLE_SPEED);
uint8_t fastcycle8 = ticks;
uint16_t slowcycle16 = (ticks >> 8) + salt;
slowcycle16 += sin8( slowcycle16);
slowcycle16 =(slowcycle16 * 2053) + 1384;
uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8);
uint8_t bright = 0;
if ( ((slowcycle8 & 0x0E) / 2) < TWINKLE_DENSITY) {
bright = attackDecayWave8( fastcycle8);
}
uint8_t hue = slowcycle8 - salt;
CRGB c;
if ( bright > 0) {
c = ColorFromPalette( gCurrentPalette, hue, bright, NOBLEND);
if ( COOL_LIKE_INCANDESCENT == 1 ) {
coolLikeIncandescent( c, fastcycle8);
}
} else {
c = CRGB::Black;
}
return c;
}
// This function is like 'triwave8', which produces a
// symmetrical up-and-down triangle sawtooth waveform, except that this
// function produces a triangle wave with a faster attack and a slower decay:
//
// / \
// / \
// / \
/// \
//
uint8_t attackDecayWave8( uint8_t i)
{
if ( i < 86) {
return i * 3;
} else {
i -= 86;
return 255 - (i + (i / 2));
}
}
// This function takes a pixel, and if its in the 'fading down'
// part of the cycle, it adjusts the color a little bit like the
// way that incandescent bulbs fade toward 'red' as they dim.
void coolLikeIncandescent( CRGB& c, uint8_t phase)
{
if ( phase < 128) return;
uint8_t cooling = (phase - 128) >> 4;
c.g = qsub8( c.g, cooling);
c.b = qsub8( c.b, cooling * 2);
}
// A mostly red palette with green accents and white trim.
// "CRGB::Gray" is used as white to keep the brightness more uniform.
const TProgmemRGBPalette16 RedGreenWhite_p FL_PROGMEM =
{ CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red,
CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red,
CRGB::Red, CRGB::Red, CRGB::Gray, CRGB::Gray,
CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green
};
// A mostly (dark) green palette with red berries.
#define Holly_Green 0x00580c
#define Holly_Red 0xB00402
const TProgmemRGBPalette16 Holly_p FL_PROGMEM =
{ Holly_Green, Holly_Green, Holly_Green, Holly_Green,
Holly_Green, Holly_Green, Holly_Green, Holly_Green,
Holly_Green, Holly_Green, Holly_Green, Holly_Green,
Holly_Green, Holly_Green, Holly_Green, Holly_Red
};
// A red and white striped palette
// "CRGB::Gray" is used as white to keep the brightness more uniform.
const TProgmemRGBPalette16 RedWhite_p FL_PROGMEM =
{ CRGB::Red,CRGB::Red,CRGB::Red,CRGB::Red,
CRGB::Gray, CRGB::Gray, CRGB::Gray, CRGB::Gray,
CRGB::Red,CRGB::Red,CRGB::Red,CRGB::Red,
CRGB::Gray, CRGB::Gray, CRGB::Gray, CRGB::Gray
};
// A mostly blue palette with white accents.
// "CRGB::Gray" is used as white to keep the brightness more uniform.
const TProgmemRGBPalette16 BlueWhite_p FL_PROGMEM =
{ CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
CRGB::Blue, CRGB::Gray, CRGB::Gray, CRGB::Gray
};
// A pure "fairy light" palette with some brightness variations
#define HALFFAIRY ((CRGB::FairyLight & 0xFEFEFE) / 2)
#define QUARTERFAIRY ((CRGB::FairyLight & 0xFCFCFC) / 4)
const TProgmemRGBPalette16 FairyLight_p FL_PROGMEM =
{ CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight,
HALFFAIRY, HALFFAIRY, CRGB::FairyLight, CRGB::FairyLight,
QUARTERFAIRY, QUARTERFAIRY, CRGB::FairyLight, CRGB::FairyLight,
CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight
};
// A palette of soft snowflakes with the occasional bright one
const TProgmemRGBPalette16 Snow_p FL_PROGMEM =
{ 0x304048, 0x304048, 0x304048, 0x304048,
0x304048, 0x304048, 0x304048, 0x304048,
0x304048, 0x304048, 0x304048, 0x304048,
0x304048, 0x304048, 0x304048, 0xE0F0FF
};
// A palette reminiscent of large 'old-school' C9-size tree lights
// in the five classic colors: red, orange, green, blue, and white.
#define C9_Red 0xB80400
#define C9_Orange 0x902C02
#define C9_Green0x046002
#define C9_Blue 0x070758
#define C9_White0x606820
const TProgmemRGBPalette16 RetroC9_p FL_PROGMEM =
{ C9_Red, C9_Orange, C9_Red, C9_Orange,
C9_Orange, C9_Red, C9_Orange, C9_Red,
C9_Green,C9_Green,C9_Green,C9_Green,
C9_Blue, C9_Blue, C9_Blue,
C9_White
};
// A cold, icy pale blue palette
#define Ice_Blue1 0x0C1040
#define Ice_Blue2 0x182080
#define Ice_Blue3 0x5080C0
const TProgmemRGBPalette16 Ice_p FL_PROGMEM =
{
Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
Ice_Blue2, Ice_Blue2, Ice_Blue2, Ice_Blue3
};
// Add or remove palette names from this list to control which color
// palettes are used, and in what order.
const TProgmemRGBPalette16* ActivePaletteList[] = {
&RetroC9_p,
&BlueWhite_p,
&RainbowColors_p,
&FairyLight_p,
&RedGreenWhite_p,
&PartyColors_p,
&RedWhite_p,
&Snow_p,
&Holly_p,
&Ice_p
};
// Advance to the next color palette in the list (above).
void chooseNextColorPalette( CRGBPalette16& pal)
{
const uint8_t numberOfPalettes = sizeof(ActivePaletteList) / sizeof(ActivePaletteList);
static uint8_t whichPalette = -1;
whichPalette = addmod8( whichPalette, 1, numberOfPalettes);
pal = *(ActivePaletteList);
}
驴友花雕
发表于 2022-10-23 11:45:27
实验场景图
驴友花雕
发表于 2022-10-23 14:13:51
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十三:随机不同像素布局的xy矩阵
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十三:随机不同像素布局的xy矩阵
*/
#include <FastLED.h>
// Params for width and height
const uint8_t kMatrixWidth = 8;
const uint8_t kMatrixHeight = 32;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
// Param for different pixel layouts
#define kMatrixSerpentineLayouttrue
// led array
CRGB leds;
// x,y, & time values
uint32_t x,y,v_time,hue_time,hxy;
// Play with the values of the variables below and see what kinds of effects they
// have!More octaves will make things slower.
// how many octaves to use for the brightness and hue functions
uint8_t octaves=1;
uint8_t hue_octaves=3;
// the 'distance' between points on the x and y axis
int xscale=57771;
int yscale=57771;
// the 'distance' between x/y points for the hue noise
int hue_scale=1;
// how fast we move through time & hue noise
int time_speed=1111;
int hue_speed=31;
// adjust these values to move along the x or y axis between frames
int x_speed=331;
int y_speed=1111;
void loop() {
// fill the led array 2/16-bit noise values
fill_2dnoise16(leds, kMatrixWidth, kMatrixHeight, kMatrixSerpentineLayout,
octaves,x,xscale,y,yscale,v_time,
hue_octaves,hxy,hue_scale,hxy,hue_scale,hue_time, false);
FastLED.show();
// adjust the intra-frame time values
x += x_speed;
y += y_speed;
v_time += time_speed;
hue_time += hue_speed;
// delay(50);
}
void setup() {
// initialize the x/y and time values
random16_set_seed(8934);
random16_add_entropy(analogRead(3));
Serial.begin(57600);
Serial.println("resetting!");
delay(3000);
FastLED.addLeds<WS2811,6,GRB>(leds,NUM_LEDS);
FastLED.setBrightness(36);
hxy = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
x = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
y = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
v_time = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
hue_time = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
}
驴友花雕
发表于 2022-10-23 14:18:59
实验场景图
驴友花雕
发表于 2022-10-23 14:20:59
驴友花雕
发表于 2022-10-23 14:31:41
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十四:将噪声数据转换为 LED 阵列中的动态颜色
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十四:将噪声数据转换为 LED 阵列中的动态颜色
*/
#include <FastLED.h>
#define LED_PIN 6
#define BRIGHTNESS26
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
// Params for width and height
const uint8_t kMatrixWidth= 8;
const uint8_t kMatrixHeight = 32;
// 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;
// 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;
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;
uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
data = newdata;
}
noise = 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;
uint8_t bri = noise;
// 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 = 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 = CRGB::White;
currentPalette = CRGB::White;
currentPalette = CRGB::White;
currentPalette = 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;
}
驴友花雕
发表于 2022-10-23 14:37:04
实验场景图动态图
驴友花雕
发表于 2022-10-23 14:56:35
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十五:随机追逐的彗星效果
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十五:随机追逐的彗星效果
*/
#include <WS2812FX.h>
#define LED_COUNT 256
#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)
}
驴友花雕
发表于 2022-10-23 14:58:23
实验场景图
驴友花雕
发表于 2022-10-23 15:28:33
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十六:WS2812FX库最简单的程序形式
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十六:WS2812FX库最简单的程序形式
*/
#include <WS2812FX.h> //导入库
#define LED_COUNT 256 //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(255); //设置亮度(0-255),可以控制总电流(重要!)
ws2812fx.setSpeed(200); // 设置速度
ws2812fx.setMode(FX_MODE_FIREWORKS_RANDOM);// 设置模式(内置63种模式)
ws2812fx.start(); //启动
}
void loop() {
ws2812fx.service(); //循环运行
}
驴友花雕
发表于 2022-10-23 15:31:02
实验场景图
驴友花雕
发表于 2022-10-23 15:35:31
本帖最后由 驴友花雕 于 2022-10-23 15:39 编辑
通过将 LED 串分成段(最多 10 个)并独立编程每个段,可以创建更复杂的效果。使用setSegment()函数对每个段的模式、颜色、速度和方向(正常或反向)进行编程:
[*]
[*]setSegment(segment index, start LED, stop LED, mode, color, speed, reverse);
请注意,某些效果使用不止一种颜色(最多三种),并通过指定颜色数组进行编程:
[*]
[*]setSegment(segment index, start LED, stop LED, mode, colors[], speed, reverse);
//将 LED 串分成两个独立的部分
uint32_t colours[] = {RED, GREEN};
ws2812fx.setSegment( 0 , 0 , (LED_COUNT/ 2 )- 1 , FX_MODE_BLINK, colors, 1000 , false );
ws2812fx.setSegment( 1 , LED_COUNT/ 2 , LED_COUNT- 1 , FX_MODE_BLINK, COLORS(ORANGE, PURPLE), 1000 , false );
驴友花雕
发表于 2022-10-23 15:41:52
内置效果清单
静态- 不闪烁。只是普通的旧静态灯。
闪烁- 正常闪烁。50% 开/关时间。
呼吸- 众所周知的 i-Devices 的“待机呼吸”。固定速度。
颜色擦除- 依次点亮所有 LED。然后按顺序关闭它们。重复。
颜色擦除反转 - 与 Color Wipe 相同,但交换开/关颜色。
颜色擦除反向 - 依次点亮所有 LED。然后以相反的顺序关闭它们。重复。
颜色擦除反向反向 - 与 上条相同,除了交换开/关颜色。
随机颜色擦除- 将所有 LED 依次变为随机颜色。然后用另一种颜色重新开始。
随机颜色- 以一种随机颜色点亮所有 LED。然后将它们切换到下一个随机颜色。
单动态- 以随机颜色点亮每个 LED。将一个随机的 LED 一个接一个地更改为随机颜色。
多动态- 以随机颜色点亮每个 LED。同时将所有 LED 更改为新的随机颜色。
彩虹- 通过彩虹一次循环所有 LED。
彩虹循环- 在整个 LED 串上循环彩虹。
扫描- 来回运行单个像素。
双扫描- 以相反的方向来回运行两个像素。
淡入淡出- 使 LED 灯再次亮起和(几乎)熄灭。
剧院追逐 - 剧院式爬行灯。受 Adafruit 示例的启发。
剧院追逐彩虹- 具有彩虹效果的剧院式爬行灯。受 Adafruit 示例的启发。
行车灯- 带平滑正弦过渡的行车灯效果。
闪烁- 使多个 LED 闪烁、重置、重复。
闪烁随机 - 以随机颜色闪烁几个 LED,重置,重复。
闪烁淡入淡出 - 闪烁几个 LED,然后逐渐消失。
19 / 5000
翻译结果
闪烁淡入淡出随机- 以随机颜色闪烁几个 LED,然后逐渐消失。
闪烁- 一次闪烁一个 LED。
Flash Sparkle - 以所选颜色点亮所有 LED。随机闪烁单个白色像素。
Hyper Sparkle - 像闪光一样。更多的闪光。
频闪- 经典频闪效果。
频闪彩虹- 经典频闪效果。骑自行车穿过彩虹。
Multi Strobe - 具有不同频闪次数和暂停的频闪效果,由速度设置控制。
闪烁彩虹- 经典闪烁效果。骑自行车穿过彩虹。
Chase White - 在白色上运行的颜色。
Chase Color - 白色在颜色上运行。
Chase Random - 白色运行,然后是随机颜色。
Chase Rainbow - 白色在彩虹上奔跑。
Chase Flash - 白色闪烁彩色。
Chase Flash Random - 白色闪烁,然后是随机颜色。
Chase Rainbow White - 运行在白色的彩虹。
Chase Blackout - 黑色在彩色上运行。
Chase Blackout Rainbow - 黑色在彩虹上奔跑。
随机颜色扫描- 从条带的开始和结束交替引入随机颜色。
运行颜色- 交替运行的颜色/白色像素。
Running Red Blue - 交替运行红色/蓝色像素。
随机运行- 随机彩色像素运行。
Larson 扫描仪- KITT
Comet - 从一端发射彗星。
烟花- 烟花火花。
Fireworks Random - 随机彩色烟花火花。
圣诞快乐- 交替运行绿色/红色像素。
火焰闪烁- 火焰闪烁效果。就像在狂风中。
Fire Flicker (soft) - 火焰闪烁效果。跑得更慢/更软。
Fire Flicker (intense) - 火焰闪烁效果。颜色范围更广。
Circus Combustus - 交替运行的白色/红色/黑色像素。
万圣节- 交替运行橙色/紫色像素。
双色追逐- 两个 LED 在背景色上运行。
三色追逐- 交替运行三个彩色像素。
TwinkleFOX - 灯光随机淡入淡出。
通过 63.自定义- 最多八个用户创建的自定义效果。
驴友花雕
发表于 2022-10-23 15:52:54
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十七:使用Adafruit_NeoPixel库的串口控制20种效果
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序十七:使用Adafruit_NeoPixel库的串口控制20种效果
*/
#include <Adafruit_NeoPixel.h>
#define PIN 6 //LED'in Din pinini yazın
#define NUM_LEDS 256 //Kaç tane LED'iniz varsa buraya yazın
#define BRIGHTNESS 20
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
int mod;
int lastmod;
String veri;
int randommod;
int parlaklik = 0;
void setup() {
Serial.begin(9600);
veri.reserve(5);
strip.begin();
strip.show();
strip.setBrightness(BRIGHTNESS); //亮度范围0-255
Serial.println("WS2812准备就绪");
Serial.println("串口输入1-21");
}
void loop() {
switch (mod) {
case 1:
Serial.println("RGBLoop");
RGBLoop();
break;
case 2:
Serial.println("Strobe");
Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
break;
case 3:
Serial.println("HalloweenEyes");
HalloweenEyes(0xff, 0x00, 0x00, 1, 4, true, random(5, 50), random(10, 50), random(50, 300));
break;
case 4:
Serial.println("NewKITT RightToLeft");
NewKITT(0xff, 0, 0, 8, 10, 50);
break;
case 5:
Serial.println("Twinkle");
Twinkle(0xff, 0, 0, 10, 100, false);
break;
case 6:
Serial.println("TwinkleRandom");
Twinkle(0xff, 0, 0, 10, 100, false);
break;
case 7:
Serial.println("Sparkle");
Sparkle(0xff, 0xff, 0xff, 0);
break;
case 8:
Serial.println("SnowSparkle");
SnowSparkle(0x10, 0x10, 0x10, 20, random(100, 1000));
break;
case 9:
Serial.println("RunningLights");
RunningLights(0xff, 0xff, 0x00, 50);
break;
case 10:
Serial.println("colorWipe");
colorWipe(0x00, 0xff, 0x00, 50);
colorWipe(0xff, 0x00, 0x00, 50);
break;
case 11:
Serial.println("rainbowCycle");
rainbowCycle(20);
break;
case 12:
Serial.println("theaterChase");
theaterChase(0xff, 0, 0, 50);
break;
case 13:
Serial.println("theaterChaseRainbow");
theaterChaseRainbow(50);
break;
case 14:
Serial.println("Fire");
Fire(55, 120, 15);
break;
case 15:
Serial.println("BouncingBalls");
meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
break;
case 16:
Serial.println("meteorRain");
meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
break;
case 17:
Serial.println("Red");
setAll(255, 0, 0);
break;
case 18:
Serial.println("Green");
setAll(0, 255, 0);
break;
case 19:
Serial.println("Blue");
setAll(0, 0, 255);
break;
case 20:
Serial.println("ON");
randommod = random(1, 19);
switch (randommod) {
case 1:
Serial.println("RGBLoop");
RGBLoop();
break;
case 2:
Serial.println("Strobe");
Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
break;
case 3:
Serial.println("HalloweenEyes");
HalloweenEyes(0xff, 0x00, 0x00, 1, 4, true, random(5, 50), random(10, 50), random(50, 300));
break;
case 4:
Serial.println("NewKITT RightToLeft");
NewKITT(0xff, 0, 0, 8, 10, 50);
break;
case 5:
Serial.println("Twinkle");
Twinkle(0xff, 0, 0, 10, 100, false);
break;
case 6:
Serial.println("TwinkleRandom");
Twinkle(0xff, 0, 0, 10, 100, false);
break;
case 7:
Serial.println("Sparkle");
Sparkle(0xff, 0xff, 0xff, 0);
break;
case 8:
Serial.println("SnowSparkle");
SnowSparkle(0x10, 0x10, 0x10, 20, random(100, 1000));
break;
case 9:
Serial.println("RunningLights");
RunningLights(0xff, 0xff, 0x00, 50);
break;
case 10:
Serial.println("colorWipe");
colorWipe(0x00, 0xff, 0x00, 50);
colorWipe(0xff, 0x00, 0x00, 50);
break;
case 11:
Serial.println("rainbowCycle");
rainbowCycle(20);
break;
case 12:
Serial.println("theaterChase");
theaterChase(0xff, 0, 0, 50);
break;
case 13:
Serial.println("theaterChaseRainbow");
theaterChaseRainbow(50);
break;
case 14:
Serial.println("Fire");
Fire(55, 120, 15);
break;
case 15:
Serial.println("BouncingBalls");
meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
break;
case 16:
Serial.println("meteorRain");
meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
break;
case 17:
Serial.println("Red");
setAll(255, 0, 0);
break;
case 18:
Serial.println("Green");
setAll(0, 255, 0);
break;
case 19:
Serial.println("Blue");
setAll(0, 0, 255);
break;
}
break;
case 21:
Serial.println("OFF");
setAll(0, 0, 0);
break;
default:
//Serial.println("Red");
setAll(255, 0, 0);
break;
}
}
void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
setAll(0, 0, 0);
for (int i = 0; i < NUM_LEDS + NUM_LEDS; i++) {
// fade brightness all LEDs one step
for (int j = 0; j < NUM_LEDS; j++) {
if ( (!meteorRandomDecay) || (random(10) > 5) ) {
fadeToBlack(j, meteorTrailDecay );
}
if (serialEvent() != false) break;
}
// draw meteor
for (int j = 0; j < meteorSize; j++) {
if ( ( i - j < NUM_LEDS) && (i - j >= 0) ) {
setPixel(i - j, red, green, blue);
}
if (serialEvent() != false) break;
}
showStrip();
delay(SpeedDelay);
if (serialEvent() != false) break;
}
}
void fadeToBlack(int ledNo, byte fadeValue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
uint32_t oldColor;
uint8_t r, g, b;
int value;
oldColor = strip.getPixelColor(ledNo);
r = (oldColor & 0x00ff0000UL) >> 16;
g = (oldColor & 0x0000ff00UL) >> 8;
b = (oldColor & 0x000000ffUL);
r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 256);
g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 256);
b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 256);
strip.setPixelColor(ledNo, r, g, b);
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds.fadeToBlackBy( fadeValue );
#endif
}
void BouncingBalls(byte red, byte green, byte blue, int BallCount) {
float Gravity = -9.81;
int StartHeight = 1;
float Height;
float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
float ImpactVelocity;
float TimeSinceLastBounce;
int Position;
longClockTimeSinceLastBounce;
float Dampening;
for (int i = 0 ; i < BallCount ; i++) {
ClockTimeSinceLastBounce = millis();
Height = StartHeight;
Position = 0;
ImpactVelocity = ImpactVelocityStart;
TimeSinceLastBounce = 0;
Dampening = 0.90 - float(i) / pow(BallCount, 2);
if (serialEvent() != false) break;
}
while (true) {
for (int i = 0 ; i < BallCount ; i++) {
TimeSinceLastBounce =millis() - ClockTimeSinceLastBounce;
Height = 0.5 * Gravity * pow( TimeSinceLastBounce / 1000 , 2.0 ) + ImpactVelocity * TimeSinceLastBounce / 1000;
if ( Height < 0 ) {
Height = 0;
ImpactVelocity = Dampening * ImpactVelocity;
ClockTimeSinceLastBounce = millis();
if ( ImpactVelocity < 0.01 ) {
ImpactVelocity = ImpactVelocityStart;
}
}
Position = round( Height * (NUM_LEDS - 1) / StartHeight);
if (serialEvent() != false) break;
}
for (int i = 0 ; i < BallCount ; i++) {
setPixel(Position, red, green, blue);
if (serialEvent() != false) break;
}
showStrip();
setAll(0, 0, 0);
if (serialEvent() != false) break;
}
}
void Fire(int Cooling, int Sparking, int SpeedDelay) {
static byte heat;
int cooldown;
for ( int i = 0; i < NUM_LEDS; i++) {
cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
if (cooldown > heat) {
heat = 0;
} else {
heat = heat - cooldown;
}
if (serialEvent() != false) break;
}
for ( int k = NUM_LEDS - 1; k >= 2; k--) {
heat = (heat + heat + heat) / 3;
if (serialEvent() != false) break;
}
if ( random(255) < Sparking ) {
int y = random(7);
heat = heat + random(160, 255);
}
for ( int j = 0; j < NUM_LEDS; j++) {
setPixelHeatColor(j, heat );
if (serialEvent() != false) break;
}
showStrip();
delay(SpeedDelay);
}
void setPixelHeatColor (int Pixel, byte temperature) {
byte t192 = round((temperature / 255.0) * 191);
byte heatramp = t192 & 0x3F; // 0..63
heatramp <<= 2; // scale up to 0..252
if ( t192 > 0x80) { // hottest
setPixel(Pixel, 255, 255, heatramp);
} else if ( t192 > 0x40 ) { // middle
setPixel(Pixel, 255, heatramp, 0);
} else { // coolest
setPixel(Pixel, heatramp, 0, 0);
}
}
void theaterChaseRainbow(int SpeedDelay) {
byte *c;
for (int j = 0; j < 256; j++) {
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
c = Wheel( (i + j) % 255);
setPixel(i + q, *c, *(c + 1), *(c + 2));
if (serialEvent() != false) break;
}
showStrip();
delay(SpeedDelay);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
setPixel(i + q, 0, 0, 0);
if (serialEvent() != false) break;
}
if (serialEvent() != false) break;
}
if (serialEvent() != false) break;
}
}
void theaterChase(byte red, byte green, byte blue, int SpeedDelay) {
for (int j = 0; j < 10; j++) {
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
setPixel(i + q, red, green, blue);
if (serialEvent() != false) break;
}
showStrip();
delay(SpeedDelay);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
setPixel(i + q, 0, 0, 0);
if (serialEvent() != false) break;
}
if (serialEvent() != false) break;
}
if (serialEvent() != false) break;
}
}
void rainbowCycle(int SpeedDelay) {
byte *c;
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) {
for (i = 0; i < NUM_LEDS; i++) {
c = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
setPixel(i, *c, *(c + 1), *(c + 2));
if (serialEvent() != false) break;
}
showStrip();
delay(SpeedDelay);
if (serialEvent() != false) break;
}
}
byte * Wheel(byte WheelPos) {
static byte c;
if (WheelPos < 85) {
c = WheelPos * 3;
c = 255 - WheelPos * 3;
c = 0;
} else if (WheelPos < 170) {
WheelPos -= 85;
c = 255 - WheelPos * 3;
c = 0;
c = WheelPos * 3;
} else {
WheelPos -= 170;
c = 0;
c = WheelPos * 3;
c = 255 - WheelPos * 3;
}
return c;
}
void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
for (uint16_t i = 0; i < NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
delay(SpeedDelay);
if (serialEvent() != false) break;
}
}
void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
int Position = 0;
for (int j = 0; j < NUM_LEDS * 2; j++)
{
Position++; // = 0; //Position + Rate;
for (int i = 0; i < NUM_LEDS; i++) {
// sine wave, 3 offset waves make a rainbow!
//float level = sin(i+Position) * 127 + 128;
//setPixel(i,level,0,0);
//float level = sin(i+Position) * 127 + 128;
setPixel(i, ((sin(i + Position) * 127 + 128) / 255)*red,
((sin(i + Position) * 127 + 128) / 255)*green,
((sin(i + Position) * 127 + 128) / 255)*blue);
if (serialEvent() != false) break;
}
showStrip();
delay(WaveDelay);
if (serialEvent() != false) break;
}
}
void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {
setAll(red, green, blue);
int Pixel = random(NUM_LEDS);
setPixel(Pixel, 0xff, 0xff, 0xff);
showStrip();
delay(SparkleDelay);
setPixel(Pixel, red, green, blue);
showStrip();
delay(SpeedDelay);
}
void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
int Pixel = random(NUM_LEDS);
setPixel(Pixel, red, green, blue);
showStrip();
delay(SpeedDelay);
setPixel(Pixel, 0, 0, 0);
}
void TwinkleRandom(int Count, int SpeedDelay, boolean OnlyOne) {
setAll(0, 0, 0);
for (int i = 0; i < Count; i++) {
setPixel(random(NUM_LEDS), random(0, 255), random(0, 255), random(0, 255));
showStrip();
delay(SpeedDelay);
if (OnlyOne) {
setAll(0, 0, 0);
}
if (serialEvent() != false) break;
}
delay(SpeedDelay);
}
void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
setAll(0, 0, 0);
for (int i = 0; i < Count; i++) {
setPixel(random(NUM_LEDS), red, green, blue);
showStrip();
delay(SpeedDelay);
if (OnlyOne) {
setAll(0, 0, 0);
}
if (serialEvent() != false) break;
}
delay(SpeedDelay);
}
void NewKITT(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
/*OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);*/
}
void CenterToOutside(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
for (int i = ((NUM_LEDS - EyeSize) / 2); i >= 0; i--) {
setAll(0, 0, 0);
setPixel(i, red / 10, green / 10, blue / 10);
for (int j = 1; j <= EyeSize; j++) {
setPixel(i + j, red, green, blue);
if (serialEvent() != false) break;
}
setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
for (int j = 1; j <= EyeSize; j++) {
setPixel(NUM_LEDS - i - j, red, green, blue);
if (serialEvent() != false) break;
}
setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);
showStrip();
delay(SpeedDelay);
if (serialEvent() != false) break;
}
delay(ReturnDelay);
}
void OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
for (int i = 0; i <= ((NUM_LEDS - EyeSize) / 2); i++) {
setAll(0, 0, 0);
setPixel(i, red / 10, green / 10, blue / 10);
for (int j = 1; j <= EyeSize; j++) {
setPixel(i + j, red, green, blue);
if (serialEvent() != false) break;
}
setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
for (int j = 1; j <= EyeSize; j++) {
setPixel(NUM_LEDS - i - j, red, green, blue);
if (serialEvent() != false) break;
}
setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);
showStrip();
delay(SpeedDelay);
if (serialEvent() != false) break;
}
delay(ReturnDelay);
}
void LeftToRight(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
for (int i = 0; i < NUM_LEDS - EyeSize - 2; i++) {
setAll(0, 0, 0);
setPixel(i, red / 10, green / 10, blue / 10);
for (int j = 1; j <= EyeSize; j++) {
setPixel(i + j, red, green, blue);
if (serialEvent() != false) break;;
}
setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
showStrip();
delay(SpeedDelay);
if (serialEvent() != false) break;
}
delay(ReturnDelay);
}
void RightToLeft(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
for (int i = NUM_LEDS - EyeSize - 2; i > 0; i--) {
setAll(0, 0, 0);
setPixel(i, red / 10, green / 10, blue / 10);
for (int j = 1; j <= EyeSize; j++) {
setPixel(i + j, red, green, blue);
if (serialEvent() != false) break;
}
setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
showStrip();
delay(SpeedDelay);
if (serialEvent() != false) break;
}
delay(ReturnDelay);
}
void HalloweenEyes(byte red, byte green, byte blue, int EyeWidth, int EyeSpace, boolean Fade, int Steps, int FadeDelay, int EndPause) {
randomSeed(analogRead(0));
int i;
int StartPoint= random( 0, NUM_LEDS - (2 * EyeWidth) - EyeSpace );
int Start2ndEye = StartPoint + EyeWidth + EyeSpace;
for (i = 0; i < EyeWidth; i++) {
setPixel(StartPoint + i, red, green, blue);
setPixel(Start2ndEye + i, red, green, blue);
if (serialEvent() != false) break;
}
showStrip();
if (Fade == true) {
float r, g, b;
for (int j = Steps; j >= 0; j--) {
r = j * (red / Steps);
g = j * (green / Steps);
b = j * (blue / Steps);
for (i = 0; i < EyeWidth; i++) {
setPixel(StartPoint + i, r, g, b);
setPixel(Start2ndEye + i, r, g, b);
if (serialEvent() != false) break;
}
showStrip();
delay(FadeDelay);
if (serialEvent() != false) break;
}
}
setAll(0, 0, 0); // Set all black
delay(EndPause);
}
void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause) {
for (int j = 0; j < StrobeCount; j++) {
setAll(red, green, blue);
showStrip();
delay(FlashDelay);
setAll(0, 0, 0);
showStrip();
delay(FlashDelay);
if (serialEvent() != false) break;
}
delay(EndPause);
}
void RGBLoop() {
for (int j = 0; j < 3; j++ ) {
// Fade IN
for (int k = 0; k < 256; k++) {
switch (j) {
case 0: setAll(k, 0, 0); if (serialEvent() != false) break; break;
case 1: setAll(0, k, 0); if (serialEvent() != false) break; break;
case 2: setAll(0, 0, k); if (serialEvent() != false) break; break;
}
showStrip();
if (serialEvent() != false)break;
}
// Fade OUT
for (int k = 255; k >= 0; k--) {
switch (j) {
case 0: setAll(k, 0, 0); if (serialEvent() != false) break; break;
case 1: setAll(0, k, 0); if (serialEvent() != false) break; break;
case 2: setAll(0, 0, k); if (serialEvent() != false) break; break;
}
showStrip();
if (serialEvent() != false)break;
}
if (serialEvent() != false)break;
}
}
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds.r = red;
leds.g = green;
leds.b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
bool serialEvent() {
if (Serial.available()) {
veri = Serial.readString();
if (veri.charAt(0) == 'b') {
veri = veri.substring(1, 4);
parlaklik = veri.toInt();
parlaklik = map(parlaklik, 0, 100, 0, 255);
strip.setBrightness(parlaklik);
}
else {
mod = veri.toInt();
return true;
}
}
return false;
}
驴友花雕
发表于 2022-10-23 15:57:17
实验串口返回情况
驴友花雕
发表于 2022-10-23 15:59:23
串口输入20,随机显示不同效果
驴友花雕
发表于 2022-10-23 16:01:28
使用PC端控制软件,有二种控制WS2812B的方式
1、连接串口后,在底部窗口输入1-21,转换显示效果
2、点击16个效果按钮
驴友花雕
发表于 2022-10-23 21:19:19
实验场景图