【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
项目程序之八:多彩颜色调色板
-
-
- #include <FastLED.h>
-
- #define LED_PIN 6
- #define NUM_LEDS 256
- #define BRIGHTNESS 23
- #define LED_TYPE WS2811
- #define COLOR_ORDER GRB
- CRGB leds[NUM_LEDS];
-
- #define UPDATES_PER_SECOND 100 //定义每秒更新数
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- CRGBPalette16 currentPalette;
- TBlendType currentBlending;
-
- extern CRGBPalette16 myRedWhiteBluePalette;
- extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
-
-
- void setup() {
- delay( 3000 );
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- FastLED.setBrightness( BRIGHTNESS );
- currentPalette = RainbowColors_p;
- currentBlending = LINEARBLEND;
- }
-
-
- void loop()
- {
- ChangePalettePeriodically();
-
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
-
- FillLEDsFromPaletteColors( startIndex);
-
- FastLED.show();
- FastLED.delay(1000 / UPDATES_PER_SECOND);
- }
-
- void FillLEDsFromPaletteColors( uint8_t colorIndex)
- {
- uint8_t brightness = 255;
-
- for ( int i = 0; i < NUM_LEDS; ++i) {
- leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
- colorIndex += 3;
- }
- }
-
-
-
-
-
-
-
-
-
-
- void ChangePalettePeriodically()
- {
- uint8_t secondHand = (millis() / 1000) % 60;
- static uint8_t lastSecond = 99;
-
- if ( lastSecond != secondHand) {
- lastSecond = secondHand;
- if ( secondHand == 0) {
- currentPalette = RainbowColors_p;
- currentBlending = LINEARBLEND;
- }
- if ( secondHand == 10) {
- currentPalette = RainbowStripeColors_p;
- currentBlending = NOBLEND;
- }
- if ( secondHand == 15) {
- currentPalette = RainbowStripeColors_p;
- currentBlending = LINEARBLEND;
- }
- if ( secondHand == 20) {
- SetupPurpleAndGreenPalette();
- currentBlending = LINEARBLEND;
- }
- if ( secondHand == 25) {
- SetupTotallyRandomPalette();
- currentBlending = LINEARBLEND;
- }
- if ( secondHand == 30) {
- SetupBlackAndWhiteStripedPalette();
- currentBlending = NOBLEND;
- }
- if ( secondHand == 35) {
- SetupBlackAndWhiteStripedPalette();
- currentBlending = LINEARBLEND;
- }
- if ( secondHand == 40) {
- currentPalette = CloudColors_p;
- currentBlending = LINEARBLEND;
- }
- if ( secondHand == 45) {
- currentPalette = PartyColors_p;
- currentBlending = LINEARBLEND;
- }
- if ( secondHand == 50) {
- currentPalette = myRedWhiteBluePalette_p;
- currentBlending = NOBLEND;
- }
- if ( secondHand == 55) {
- currentPalette = myRedWhiteBluePalette_p;
- currentBlending = LINEARBLEND;
- }
- }
- }
-
-
- void SetupTotallyRandomPalette()
- {
- for ( int i = 0; i < 16; ++i) {
- currentPalette[i] = CHSV( random8(), 255, random8());
- }
- }
-
-
-
-
-
- void SetupBlackAndWhiteStripedPalette()
- {
-
- fill_solid( currentPalette, 16, CRGB::Black);
-
- currentPalette[0] = CRGB::White;
- currentPalette[4] = CRGB::White;
- currentPalette[8] = CRGB::White;
- currentPalette[12] = CRGB::White;
-
- }
-
-
- 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 );
- }
-
-
-
-
-
-
- const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
- {
- CRGB::Red,
- CRGB::Gray,
- CRGB::Blue,
- CRGB::Black,
-
- CRGB::Red,
- CRGB::Gray,
- CRGB::Blue,
- CRGB::Black,
-
- CRGB::Red,
- CRGB::Red,
- CRGB::Gray,
- CRGB::Gray,
- CRGB::Blue,
- CRGB::Blue,
- CRGB::Black,
- CRGB::Black
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|