【雕爷学编程】Arduino动手做(138)---64位WS2812点阵屏模块2

news2024/11/25 19:38:10

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

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏

在这里插入图片描述
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
安装Adafruit_NeoPixel库,
下载https://learn.adafruit.com/adafr … ibrary-installation

程序之六:复合流水彩虹灯
实验接线
Module UNO
VCC —— 3.3V
GND —— GND
DI —— D6

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏

 安装NeoPixel库,工具—管理库—搜索NeoPixel—安装

 安装Adafruit_NeoPixel库,

 下载https://learn.adafruit.com/adafr ... ibrary-installation

 程序之六:复合流水彩虹灯

 实验接线

 Module  UNO

 VCC —— 3.3V

 GND —— GND

 DI —— D6

*/

#include <Adafruit_NeoPixel.h>

#define PIN 6

#define BRIGHTNESS 64

Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);

void setup() {

 strip.setBrightness(BRIGHTNESS);

 strip.begin();

 strip.show();

}

void loop() {

 colorWipe(strip.Color(150, 0, 0), 50); // Red

 colorWipe(strip.Color(0, 150, 0), 50); // Green

 colorWipe(strip.Color(0, 0, 150), 50); // Blue

 colorWipe(strip.Color(150, 150, 150), 50); // BlueWite

 rainbowCycle(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 wheel

  for (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】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏

程序之七:复合飘逸彩虹满屏灯

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
  安装Adafruit_NeoPixel库,
  下载https://learn.adafruit.com/adafr ... ibrary-installation

  程序之七:复合飘逸彩虹满屏灯
  实验接线
  Module    UNO
  VCC —— 3.3V
  GND —— GND
  DI  ——  D6
*/

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6

// 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, 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() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code

  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Some example procedures showing how to display to the pixels:
  colorWipe(strip.Color(255, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 255, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 255), 50); // Blue
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
  // Send a theater pixel chase in...
  theaterChase(strip.Color(127, 127, 127), 50); // White
  theaterChase(strip.Color(127, 0, 0), 50); // Red
  theaterChase(strip.Color(0, 0, 127), 50); // Blue

  rainbow(20);
  rainbowCycle(20);
  theaterChaseRainbow(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 wheel
    for(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 chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t 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 wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t 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 (uint16_t 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);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏

程序之八:复合彩虹滚动流水灯

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
  安装Adafruit_NeoPixel库,
  下载https://learn.adafruit.com/adafr ... ibrary-installation

  程序之八:复合彩虹滚动流水灯
  实验接线
  Module    UNO
  VCC —— 3.3V
  GND —— GND
  DI  ——  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 64

// 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 ASAP
  strip.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); // Red
  colorWipe(strip.Color(  0, 255,   0), 50); // Green
  colorWipe(strip.Color(  0,   0, 255), 50); // Blue

  // Do a theater marquee effect in various colors...
  theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  theaterChase(strip.Color(127,   0,   0), 50); // Red, half brightness
  theaterChase(strip.Color(  0,   0, 127), 50); // Blue, half brightness

  rainbow(10);             // Flowing rainbow cycle along the whole strip
  theaterChaseRainbow(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 match
    delay(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 contents
      delay(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 contents
    delay(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 -> RGB
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show();                // Update strip with new contents
      delay(wait);                 // Pause for a moment
      firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
    }
  }
}

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏

程序之九:按键控制进入下段彩灯程序

实验接线

Module UNO

VCC —— 3.3V

GND —— GND

DI —— D6

ws —— D2

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏

 安装NeoPixel库,工具—管理库—搜索NeoPixel—安装

 安装Adafruit_NeoPixel库,

 下载https://learn.adafruit.com/adafr ... ibrary-installation

 程序之九:按键控制进入下段彩灯程序

 实验接线

 Module  UNO

 VCC —— 3.3V

 GND —— GND

 DI —— D6

 ws —— D2

*/

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__

#include <avr/power.h> // Required for 16 MHz Adafruit Trinket

#endif

// Digital IO pin connected to the button. This will be driven with a

// pull-up resistor so the switch pulls the pin to ground momentarily.

// On a high -> low transition the button press logic will execute.

#define BUTTON_PIN  2

#define PIXEL_PIN  7 // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 64 // Number of NeoPixels

// Declare our NeoPixel strip object:

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_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)

boolean oldState = HIGH;

int   mode   = 0;  // Currently-active animation mode, 0-9

void setup() {

 pinMode(BUTTON_PIN, INPUT_PULLUP);

 strip.begin(); // Initialize NeoPixel strip object (REQUIRED)

 strip.show(); // Initialize all pixels to 'off'

}

void loop() {

 // Get current button state.

 boolean newState = digitalRead(BUTTON_PIN);

 // Check if state changed from high to low (button press).

 if ((newState == LOW) && (oldState == HIGH)) {

  // Short delay to debounce button.

  delay(20);

  // Check if button is still low after debounce.

  newState = digitalRead(BUTTON_PIN);

  if (newState == LOW) {   // Yes, still low

   if (++mode > 8) mode = 0; // Advance to next mode, wrap around after #8

   switch (mode) {     // Start the new animation...

    case 0:

     colorWipe(strip.Color( 0,  0,  0), 50);  // Black/off

     break;

    case 1:

     colorWipe(strip.Color(255,  0,  0), 50);  // Red

     break;

    case 2:

     colorWipe(strip.Color( 0, 255,  0), 50);  // Green

     break;

    case 3:

     colorWipe(strip.Color( 0,  0, 255), 50);  // Blue

     break;

    case 4:

     theaterChase(strip.Color(127, 127, 127), 50); // White

     break;

    case 5:

     theaterChase(strip.Color(127,  0,  0), 50); // Red

     break;

    case 6:

     theaterChase(strip.Color( 0,  0, 127), 50); // Blue

     break;

    case 7:

     rainbow(10);

     break;

    case 8:

     theaterChaseRainbow(50);

     break;

   }

  }

 }

 // Set the last-read button state to the old state.

 oldState = newState;

}

// 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 match

  delay(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 contents

   delay(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 3 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 3*65536. Adding 256 to firstPixelHue each time

 // means we'll make 3*65536/256 = 768 passes through this outer loop:

 for (long firstPixelHue = 0; firstPixelHue < 3 * 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 contents

  delay(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 -> RGB

    strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'

   }

   strip.show();        // Update strip with new contents

   delay(wait);         // Pause for a moment

   firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames

  }

 }

}

视频——程序之六:复合流水彩虹灯

https://v.youku.com/v_show/id_XNDU2ODUwMTE2NA==.html

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏

项目十三:颜色调色板

实验开源代码

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

  实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏

 项目十三:颜色调色板

 实验接线

 Module  UNO

 VCC —— 3.3V

 GND —— GND

 DI —— D6

*/

#include <FastLED.h>

#define LED_PIN   6

#define NUM_LEDS  64

#define BRIGHTNESS 23

#define LED_TYPE  WS2811

#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100 //定义每秒更新数

// This example shows several ways to set up and use 'palettes' of colors

// with FastLED.

//

// These compact palettes provide an easy way to re-colorize your

// animation on the fly, quickly, easily, and with low overhead.

//

// USING palettes is MUCH simpler in practice than in theory, so first just

// run this sketch, and watch the pretty lights as you then read through

// the code. Although this sketch has eight (or more) different color schemes,

// the entire sketch compiles down to about 6.5K on AVR.

//

// FastLED provides a few pre-configured color palettes, and makes it

// extremely easy to make up your own color schemes with palettes.

//

// Some notes on the more abstract 'theory and practice' of

// FastLED compact palettes are at the bottom of this file.

CRGBPalette16 currentPalette;

TBlendType  currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;

extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;

void setup() {

 delay( 3000 ); // power-up safety delay

 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; /* motion speed */

 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;

 }

}

// 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. All are shown here.

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;

  }

 }

}

// This function fills the palette with totally random colors.

void SetupTotallyRandomPalette()

{

 for ( int i = 0; i < 16; ++i) {

  currentPalette[i] = CHSV( random8(), 255, random8());

 }

}

// 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 );

}

// This example shows how to set up a static color palette

// which is stored in PROGMEM (flash), which is almost always more

// plentiful than RAM. A static PROGMEM palette like this

// takes up 64 bytes of flash.

const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =

{

 CRGB::Red,

 CRGB::Gray, // 'white' is too bright compared to red and blue

 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

};

// Additional notes on FastLED compact palettes:

//

// Normally, in computer graphics, the palette (or "color lookup table")

// has 256 entries, each containing a specific 24-bit RGB color. You can then

// index into the color palette using a simple 8-bit (one byte) value.

// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino

// is quite possibly "too many" bytes.

//

// FastLED does offer traditional 256-element palettes, for setups that

// can afford the 768-byte cost in RAM.

//

// However, FastLED also offers a compact alternative. FastLED offers

// palettes that store 16 distinct entries, but can be accessed AS IF

// they actually have 256 entries; this is accomplished by interpolating

// between the 16 explicit entries to create fifteen intermediate palette

// entries between each pair.

//

// So for example, if you set the first two explicit entries of a compact

// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved

// the first sixteen entries from the virtual palette (of 256), you'd get

// Green, followed by a smooth gradient from green-to-blue, and then Blue.


实验场景图

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/746714.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

6.EFLFK(EFLK+kafka)

文章目录 EFLFK(EFLKkafka)zookeeper概述Zookeeper 特点数据结构和工作场景选举机制&#xff08;重要&#xff09;总结部署Zookeeper kafka为什么用消息队列&#xff08;MQ&#xff09;中间件使用消息队列的好处消息队列模式消息队列总结&#xff1a;kafka概述Kafka特性Kafka架…

Layui之选项卡案例 详细易懂

⭐ 本期精彩&#xff1a; 利用Layui框架实现动态选项卡 ⭐ 继上一篇已经实现了左边的树形菜单栏&#xff0c;这一关卡我们已通过&#xff0c;接下来就是实现右边的动态选项卡的关卡,上个关卡的效果及链接 ⭐ 链接&#xff1a;http://t.csdn.cn/tYccL 目录 ⭐ 本期精彩&#xf…

2023秋招,网络安全面试题

Hello&#xff0c;各位小伙伴&#xff0c;我作为一名网络安全工程师曾经在秋招中斩获&#x1f51f;个offer&#x1f33c;&#xff0c;并在国内知名互联网公司任职过的职场老油条&#xff0c;希望可以将我的面试的网络安全大厂面试题和好运分享给大家~ 转眼2023年秋招已经到了金…

E510-A1S-I0/A0插头式连接比例放大器

E510-A1S-U0/A1、E510-A1S-I4/A1、E510-A1S-U0/B2、E510-A1S-I4/C0、E510-A1S-U0/A2、E510-A1S-I0/A0、E510-A1S-U0/A3&#xff08;BEUEC&#xff09;插头式比例阀放大器内部装有一个STM微处理器接受两个来自外部输入的模拟量指令信号和三个设定参数的内置电位器模拟量&#xf…

前端 - 接口请求抓包 Status Canceled 分析

问题描述 上图是来自于百度的抓包请求分析状态&#xff0c;一般常见的有 status 200&#xff0c;500&#xff0c;404&#xff0c;504&#xff0c;403&#xff0c;302……但这次遇到一个 status canceled&#xff08;第一次遇见还真一脸懵~&#xff09; 原因分析 那响应statu…

MySQL 的内连接、左连接、右连接有有什么区别?

MySQL的连接主要分为内连接和外连接&#xff0c;外连接常⽤的有左连接、右连接。 inner join 内连接&#xff0c;在两张表进行连接查询时&#xff0c;只保留两张表中完全匹配的结果集&#xff1b;left join左连接在两张表进行连接查询时&#xff0c;会返回左表所有的行&#x…

安卓OpenCV开发(六)图片处理(1)

使用OpenCV进行图片处理&#xff08;1&#xff09; 基础知识可以回顾我之前写的文章&#xff1a; OpenCV导入 OpenCV人脸检测 OpenCV竖屏检测 OpenCV人脸识别 OpenCV小狗识别 本文将提供一下openCv处理图片的处理&#xff0c;文末附上代码链接。 &#xff08;1&#xff09;灰…

kali NetHunter刷入

准备 安卓frida逆向协议与分析第一章中提供的刷机教程&#xff0c;Nexus 5X手机一部&#xff0c;kali NetHunter镜像&#xff0c;kali Linux(因为很多命令需要LInux命令不能直接用windows) 命令开发包下载 adbfastboot https://developer.android.com/studio/releases/platf…

执行Spark应用架构需要经历哪几个阶段?

将程序切换到【Executors】Tab页面时&#xff0c;我们可以看到Spark Application运行到集群上时&#xff0c;由两部分组成&#xff1a;Driver Program和Executors。 从图中可以看到Spark Application运行到集群上时&#xff0c;由两部分组成&#xff1a;Driver Program和Execut…

Holoens证书过期

1. 删除Assets\WSATestCertificate.pfx证书文件。 2. 在Player Settings的Publishing Settings下找到Certificate,选择一个有效的证书。 3. 如果没有其他有效证书,需要生成一个新的测试证书: - Windows: MakeCert.exe -r -pe -n "CNTemporary Certificate" -ss MY …

融合ELO机制的销售能力评估方案

ELO排位赛算法 文章目录 ELO排位赛算法一. ELO机制二. 销售能力抽象为ELO排位赛设计2.1 基础设计2.2 存在问题 三. 优化措施3.1 如何解决新销售存在冷启动问题新老人动态K值调整假设检验衡量赢的程度 3.2 如何解决转化率存在不置信的问题ELO升级为MultiELO 一. ELO机制 ELO等级…

代码随想录day15

102. 二叉树的层序遍历 这里如果使用递归法会更好的理解&#xff0c;当我们储存每一层的答案数组的长度等于当前的level层数&#xff0c;说明我们这一层的节点都已经遍历完了&#xff0c;然后我们的下一次递归的level可以直接1。 递归三要素&#xff1a; 递归终止条件&#xf…

mpVue 微信小程序基于vant-weapp 组件的二次封装TForm 表单组件(适配移动端)

一、前言 1、mpVue微信小程序不支持动态组件&#xff08;<component> &#xff09; 2、mpVue微信小程序不支持动态属性及事件穿透&#xff08;$attrs和$listeners&#xff09; 3、mpVue微信小程序不支持render函数 二、最终效果 三、配置参数&#xff08;Attributes&…

云原生日志Agent/Aggregator

Loggie是一个基于Golang的轻量级、高性能、云原生日志采集Agent和中转处理Aggregator&#xff0c;支持多Pipeline和组件热插拔&#xff0c;提供了&#xff1a; ● 一栈式日志解决方案&#xff1a; 同时支持日志中转、过滤、解析、切分、日志报警等 ● 云原生的日志形态&#xf…

全网独家:修改CoreDNS禁用ipv6解析后的查询反馈信息

CoreDNS是用Go开发的&#xff0c;可以配置多种插件来扩展其功能&#xff0c;非常灵活高效&#xff0c;它是Kubernetes的默认DNS服务器。笔者的平台采用CoreDNS来作单独的高并发域名解析服务器&#xff0c;在禁用ipv6解析后应用侧遇到反馈信息识别问题&#xff0c;本例全网独家提…

MSP430F5529单片机,万年历,闹钟,数字时钟,温湿度dht11,蜂鸣器音乐

文章目录 功能硬件连接实物程序 功能 功能1 万年历显示2 按键修改时间3 按键修改闹钟4 闹钟到达 蜂鸣器鸣叫10秒自动关闭 闹钟是音乐5 温湿度显示6 温度到达28度以上蜂鸣器鸣叫 采用单片机内部的RTC模块走时。 按键1 切换选择年月日时分秒&#xff0c;靠按键1退出时间不会保存…

Openlayers实战:加载google地图

google地图在业界一直是标杆的存在, 其zoomlevel超级大,可看到非常精细的程度。 Openlayers中如何加载谷歌地图呢?同样是XYZ的挂载方式,根据参数的不同,可以显示不同状态的谷歌地图,如果矢量地图,瓦片遥感图片等。 效果图 源代码 /* * @Author: 大剑师兰特(xiaozhuan…

36. 合并两个排序的链表

目录 链接&#xff1a; 题目&#xff1a; 思路&#xff1a; 代码&#xff1a; 链接&#xff1a; 链接 题目&#xff1a; 输入两个递增排序的链表&#xff0c;合并这两个链表并使新链表中的结点仍然是按照递增排序的。 数据范围 链表长度 [0,500][0,500]。 样例 输入&#x…

第五章 爬虫小程序-实例43-获取网页内容

1、准备工作 用Python编写的访问网页的程序中&#xff0c;有很多是调用 requests 库模块中的函数来进行操作&#xff0c;这个库模块把网页请求和操作等多项功能进行了高度封装&#xff0c;使其可以轻而易举完成浏览器的许多操作。 requests库是第三方模块&#xff0c;需要进行…

【算法与数据结构】239、LeetCode滑动窗口最大值

文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析&#xff1a;这道题我们如果用暴力破解法需要 O ( n ∗ k ) O(n*k) O(n∗k)的复杂度。思索再三&#xff0c;我们需要…