- /*
- 【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[i]) - 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);
- }
- }
复制代码
|