eagler8 发表于 2019-10-1 12:55:51

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目六:多彩流水灯变幻彩虹灯
Module      UNO
VCC   ——   5V
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 24

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400400 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
    }
}
}

eagler8 发表于 2019-10-1 13:03:08

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目七:多彩流水灯变幻彩虹灯之三
Module      UNO
VCC   ——   5V
GND——   GND
DI    ——   D6
*/

#include <Adafruit_NeoPixel.h>    //needed for the WS2812
#include <avr/pgmspace.h>         //needed for PROGMEM

#define PIN 6                  //Pin 1 is DATA In on the bottom Ring
#define BRIGHTNESS 24             // brightness reduced



//Lookup for the Candle light
const unsigned int candles[] PROGMEM =
{
15, 10, 48, 45, 36, 19, 59, 29, 5, 43, 41, 39, 24, 3, 61
};

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

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {
pinMode(PIN, OUTPUT);
strip.begin();
strip.setBrightness(BRIGHTNESS); // set brightness
strip.show(); // Initialize all pixels to 'off'
}

void loop() {
tree();
delay(1000);

colorcrazy();

theaterChaseRainbow(50);

comet();

warpdrive();
warpdrive();

rainbowCycle(1);

rainbow(5);
rainbow(5);
rainbow(5);


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

//
//
//cometr();
//Tree light:

//
//warpdrive();
//
//
//comet();


/*
    // Some example procedures showing how to display to the pixels:
    colorWipe(strip.Color(255, 0, 0), 50); // Red
    colorWipe(strip.Color(0, 255, 0), 50); // Green
    colorWipe(strip.Color(0, 0, 255), 50); // Blue
    // 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);
*/
}

//Sub-----------------------------------------------------------------------

//Comet
void comet() {
for (uint16_t i = strip.numPixels(); i > 0; i--) {
    strip.setPixelColor(i, strip.Color(0, 0, 255));
    fadethemall(10);
    fadethemall(10);
}
}

void cometr() {
for (uint16_t i = strip.numPixels(); i > 0; i--) {
    strip.setPixelColor(i, strip.Color(255, 0, 0));
    fadethemall(10);
    fadethemall(10);
}
}


//From top down white pulses
void warpdrive() {

//Top Led
strip.setPixelColor(60, strip.Color(255, 255, 255));
strip.show();
//fade a bit
for (int i = 0; i < 20; i++)
{
    fadethemall(20);
}
//8 Ring
for (int i = 52; i < 60; i++)
{
    strip.setPixelColor(i, strip.Color(255, 255, 255));
}
strip.show();
//fade a bit
for (int i = 0; i < 20; i++)
{
    fadethemall(20);
}
//12 Ring
for (int i = 40; i < 52; i++)
{
    strip.setPixelColor(i, strip.Color(255, 255, 255));
}
strip.show();
//fade a bit
for (int i = 0; i < 20; i++)
{
    fadethemall(20);
}
//16 Ring
for (int i = 24; i < 40; i++)
{
    strip.setPixelColor(i, strip.Color(255, 255, 255));
}
strip.show();
//fade a bit
for (int i = 0; i < 20; i++)
{
    fadethemall(20);
}
//24 Ring
for (int i = 0; i < 24; i++)
{
    strip.setPixelColor(i, strip.Color(255, 255, 255));
}
strip.show();
//fade a bit
for (int i = 0; i < 20; i++)
{
    fadethemall(20);
}

//Extra by John Kerr
strip.setPixelColor(60, strip.Color(0, 0, 0));
strip.show();
//fade a bit
for (int i = 0; i < 20; i++)
{
    fadethemall(20);
}

}

//This reduces the brightness of all leds
void fadethemall(uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
    uint32_t color = strip.getPixelColor(i);
    int r;
    int g;
    int b;
    r = (uint8_t)(color >> 16);
    g = (uint8_t)(color >>8);
    b = (uint8_t)color;

    if (r > 0)
    {
      r = r - 1;
    }
    else
    {
      r = 0;
    }

    if (g > 0)
    {
      g = g - 1;
    }
    else
    {
      g = 0;
    }

    if (b > 0)
    {
      b = b - 1;
    }
    else
    {
      b = 0;
    }

    strip.setPixelColor(i, strip.Color(r, g, b));
}
strip.show();
delay(wait);
}

//This drives the WS2812 in a crazy pattern, fun!
void colorcrazy() {
colorWipe(strip.Color(255, 0, 0), 25); // Red
colorWipe(strip.Color(0, 255, 0), 25); // Green
colorWipe(strip.Color(0, 0, 255), 25); // Blue
theaterChaseRainbow(5);
}

//This lights up the tree in green, then add the white "candles"
void tree() {

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

//light "candles"
//Show the S:
for (int i = 0; i < 16; i++)
{
    strip.setPixelColor(pgm_read_word(&candles) - 1, strip.Color(255, 255, 255));
    strip.show();
    delay(50);
}
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
}
}

void rainbow(uint8_t wait) {
uint16_t i, j;

for (j = 0; j < 256; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i + j) & 255));
    }
    strip.show();
    delay(wait);
}
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;

for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on 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 (int i = 0; i < strip.numPixels(); i = i + 3) {
      strip.setPixelColor(i + q, c);//turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i = 0; i < strip.numPixels(); i = i + 3) {
      strip.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
}
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheel
    for (int q = 0; q < 3; q++) {
      for (int i = 0; i < strip.numPixels(); i = i + 3) {
      strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i = 0; i < strip.numPixels(); i = i + 3) {
      strip.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
}
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
    WheelPos -= 170;
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}

eagler8 发表于 2019-10-1 13:22:03

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目八:绿色到粉红色,也沿着环形移动
Module      UNO
VCC   ——   5V
GND——   GND
DI    ——   D6
*/

#include <PololuLedStrip.h>

// Create an ledStrip object and specify the pin it will use.
PololuLedStrip<6> ledStrip;

// Create a buffer for holding the colors (3 bytes per color).
#define LED_COUNT 60
rgb_color colors;

void setup()
{
}

void loop()
{
// Update the colors.
byte time = millis() >> 2;
for (uint16_t i = 0; i < LED_COUNT; i++)
{
    byte x = time - 8*i;
    colors = rgb_color(x, 255 - x, x);
}

// Write the colors to the LED strip.
ledStrip.write(colors, LED_COUNT);

delay(10);
}

eagler8 发表于 2019-10-1 13:26:00

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目九:一道移动的彩虹
Module      UNO
VCC   ——   5V
GND——   GND
DI    ——   D6
*/

#include <PololuLedStrip.h>

// Create an ledStrip object and specify the pin it will use.
PololuLedStrip<6> ledStrip;

// Create a buffer for holding the colors (3 bytes per color).
#define LED_COUNT 60
rgb_color colors;

void setup()
{
}

// Converts a color from HSV to RGB.
// h is hue, as a number between 0 and 360.
// s is the saturation, as a number between 0 and 255.
// v is the value, as a number between 0 and 255.
rgb_color hsvToRgb(uint16_t h, uint8_t s, uint8_t v)
{
    uint8_t f = (h % 60) * 255 / 60;
    uint8_t p = (255 - s) * (uint16_t)v / 255;
    uint8_t q = (255 - f * (uint16_t)s / 255) * (uint16_t)v / 255;
    uint8_t t = (255 - (255 - f) * (uint16_t)s / 255) * (uint16_t)v / 255;
    uint8_t r = 0, g = 0, b = 0;
    switch((h / 60) % 6){
      case 0: r = v; g = t; b = p; break;
      case 1: r = q; g = v; b = p; break;
      case 2: r = p; g = v; b = t; break;
      case 3: r = p; g = q; b = v; break;
      case 4: r = t; g = p; b = v; break;
      case 5: r = v; g = p; b = q; break;
    }
    return rgb_color(r, g, b);
}

void loop()
{
// Update the colors.
uint16_t time = millis() >> 2;
for(uint16_t i = 0; i < LED_COUNT; i++)
{
    byte x = (time >> 2) - (i << 3);
    colors = hsvToRgb((uint32_t)x * 359 / 256, 255, 255);
}

// Write the colors to the LED strip.
ledStrip.write(colors, LED_COUNT);

delay(10);
}

eagler8 发表于 2019-10-1 14:40:30

eagler8 发表于 2019-10-1 14:42:11

eagler8 发表于 2019-10-1 14:43:49

eagler8 发表于 2019-10-1 14:45:22

eagler8 发表于 2019-10-1 14:48:21

eagler8 发表于 2019-10-1 16:03:47

eagler8 发表于 2019-10-1 16:19:02

eagler8 发表于 2019-10-25 10:11:23

gk18965 发表于 2019-10-24 16:48
感谢分享。。。。

:)谢谢鼓励

xiao_y 发表于 2019-12-4 14:55:23

大神牛逼

eagler8 发表于 2019-12-5 19:25:10

xiao_y 发表于 2019-12-4 14:55
大神牛逼

晚上好,谢谢鼓励

xiao_y 发表于 2020-11-28 19:02:12

eagler8 发表于 2019-12-5 19:25
晚上好,谢谢鼓励

但是如果一个uno控制两个ws2812灯环做不同频率的流水动画,没有想到好办法实现。不知道大神有没有提示?谢谢
我的大致思路如下:按键1 按下,灯环1开始做黄色延时10ms的流水动画。按键2 按下,灯环1开始做红色延时20ms的流水动画。由于我使用的是for循环做的流水,导致如果for循环没有走完程序,将不能读取另外的控制。
请大神帮忙解惑,是我的程序需要改?还是我的这个思路构建有问题?谢谢
页: 1 [2] 3
查看完整版本: 【Arduino】108种传感器模块系列实验(131)--24位WS2812环形灯板