8831浏览
查看: 8831|回复: 74

[项目] 【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏

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

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 可编程硬屏模块



【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 15:52:54

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十七:使用Adafruit_NeoPixel库的串口控制20种效果

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十七:使用Adafruit_NeoPixel库的串口控制20种效果
  5. */
  6. #include <Adafruit_NeoPixel.h>
  7. #define PIN 6 //LED'in Din pinini yazın
  8. #define NUM_LEDS 256 //Kaç tane LED'iniz varsa buraya yazın
  9. #define BRIGHTNESS 20
  10. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
  11. int mod;
  12. int lastmod;
  13. String veri;
  14. int randommod;
  15. int parlaklik = 0;
  16. void setup() {
  17.   Serial.begin(9600);
  18.   veri.reserve(5);
  19.   strip.begin();
  20.   strip.show();
  21.   strip.setBrightness(BRIGHTNESS); //亮度范围0-255
  22.   Serial.println("WS2812准备就绪");
  23.   Serial.println("串口输入1-21");
  24. }
  25. void loop() {
  26.   switch (mod) {
  27.     case 1:
  28.       Serial.println("RGBLoop");
  29.       RGBLoop();
  30.       break;
  31.     case 2:
  32.       Serial.println("Strobe");
  33.       Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
  34.       break;
  35.     case 3:
  36.       Serial.println("HalloweenEyes");
  37.       HalloweenEyes(0xff, 0x00, 0x00, 1, 4, true, random(5, 50), random(10, 50), random(50, 300));
  38.       break;
  39.     case 4:
  40.       Serial.println("NewKITT RightToLeft");
  41.       NewKITT(0xff, 0, 0, 8, 10, 50);
  42.       break;
  43.     case 5:
  44.       Serial.println("Twinkle");
  45.       Twinkle(0xff, 0, 0, 10, 100, false);
  46.       break;
  47.     case 6:
  48.       Serial.println("TwinkleRandom");
  49.       Twinkle(0xff, 0, 0, 10, 100, false);
  50.       break;
  51.     case 7:
  52.       Serial.println("Sparkle");
  53.       Sparkle(0xff, 0xff, 0xff, 0);
  54.       break;
  55.     case 8:
  56.       Serial.println("SnowSparkle");
  57.       SnowSparkle(0x10, 0x10, 0x10, 20, random(100, 1000));
  58.       break;
  59.     case 9:
  60.       Serial.println("RunningLights");
  61.       RunningLights(0xff, 0xff, 0x00, 50);
  62.       break;
  63.     case 10:
  64.       Serial.println("colorWipe");
  65.       colorWipe(0x00, 0xff, 0x00, 50);
  66.       colorWipe(0xff, 0x00, 0x00, 50);
  67.       break;
  68.     case 11:
  69.       Serial.println("rainbowCycle");
  70.       rainbowCycle(20);
  71.       break;
  72.     case 12:
  73.       Serial.println("theaterChase");
  74.       theaterChase(0xff, 0, 0, 50);
  75.       break;
  76.     case 13:
  77.       Serial.println("theaterChaseRainbow");
  78.       theaterChaseRainbow(50);
  79.       break;
  80.     case 14:
  81.       Serial.println("Fire");
  82.       Fire(55, 120, 15);
  83.       break;
  84.     case 15:
  85.       Serial.println("BouncingBalls");
  86.       meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
  87.       break;
  88.     case 16:
  89.       Serial.println("meteorRain");
  90.       meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
  91.       break;
  92.     case 17:
  93.       Serial.println("Red");
  94.       setAll(255, 0, 0);
  95.       break;
  96.     case 18:
  97.       Serial.println("Green");
  98.       setAll(0, 255, 0);
  99.       break;
  100.     case 19:
  101.       Serial.println("Blue");
  102.       setAll(0, 0, 255);
  103.       break;
  104.     case 20:
  105.       Serial.println("ON");
  106.       randommod = random(1, 19);
  107.       switch (randommod) {
  108.         case 1:
  109.           Serial.println("RGBLoop");
  110.           RGBLoop();
  111.           break;
  112.         case 2:
  113.           Serial.println("Strobe");
  114.           Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
  115.           break;
  116.         case 3:
  117.           Serial.println("HalloweenEyes");
  118.           HalloweenEyes(0xff, 0x00, 0x00, 1, 4, true, random(5, 50), random(10, 50), random(50, 300));
  119.           break;
  120.         case 4:
  121.           Serial.println("NewKITT RightToLeft");
  122.           NewKITT(0xff, 0, 0, 8, 10, 50);
  123.           break;
  124.         case 5:
  125.           Serial.println("Twinkle");
  126.           Twinkle(0xff, 0, 0, 10, 100, false);
  127.           break;
  128.         case 6:
  129.           Serial.println("TwinkleRandom");
  130.           Twinkle(0xff, 0, 0, 10, 100, false);
  131.           break;
  132.         case 7:
  133.           Serial.println("Sparkle");
  134.           Sparkle(0xff, 0xff, 0xff, 0);
  135.           break;
  136.         case 8:
  137.           Serial.println("SnowSparkle");
  138.           SnowSparkle(0x10, 0x10, 0x10, 20, random(100, 1000));
  139.           break;
  140.         case 9:
  141.           Serial.println("RunningLights");
  142.           RunningLights(0xff, 0xff, 0x00, 50);
  143.           break;
  144.         case 10:
  145.           Serial.println("colorWipe");
  146.           colorWipe(0x00, 0xff, 0x00, 50);
  147.           colorWipe(0xff, 0x00, 0x00, 50);
  148.           break;
  149.         case 11:
  150.           Serial.println("rainbowCycle");
  151.           rainbowCycle(20);
  152.           break;
  153.         case 12:
  154.           Serial.println("theaterChase");
  155.           theaterChase(0xff, 0, 0, 50);
  156.           break;
  157.         case 13:
  158.           Serial.println("theaterChaseRainbow");
  159.           theaterChaseRainbow(50);
  160.           break;
  161.         case 14:
  162.           Serial.println("Fire");
  163.           Fire(55, 120, 15);
  164.           break;
  165.         case 15:
  166.           Serial.println("BouncingBalls");
  167.           meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
  168.           break;
  169.         case 16:
  170.           Serial.println("meteorRain");
  171.           meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
  172.           break;
  173.         case 17:
  174.           Serial.println("Red");
  175.           setAll(255, 0, 0);
  176.           break;
  177.         case 18:
  178.           Serial.println("Green");
  179.           setAll(0, 255, 0);
  180.           break;
  181.         case 19:
  182.           Serial.println("Blue");
  183.           setAll(0, 0, 255);
  184.           break;
  185.       }
  186.       break;
  187.     case 21:
  188.       Serial.println("OFF");
  189.       setAll(0, 0, 0);
  190.       break;
  191.     default:
  192.       //Serial.println("Red");
  193.       setAll(255, 0, 0);
  194.       break;
  195.   }
  196. }
  197. void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
  198.   setAll(0, 0, 0);
  199.   for (int i = 0; i < NUM_LEDS + NUM_LEDS; i++) {
  200.     // fade brightness all LEDs one step
  201.     for (int j = 0; j < NUM_LEDS; j++) {
  202.       if ( (!meteorRandomDecay) || (random(10) > 5) ) {
  203.         fadeToBlack(j, meteorTrailDecay );
  204.       }
  205.       if (serialEvent() != false) break;
  206.     }
  207.     // draw meteor
  208.     for (int j = 0; j < meteorSize; j++) {
  209.       if ( ( i - j < NUM_LEDS) && (i - j >= 0) ) {
  210.         setPixel(i - j, red, green, blue);
  211.       }
  212.       if (serialEvent() != false) break;
  213.     }
  214.     showStrip();
  215.     delay(SpeedDelay);
  216.     if (serialEvent() != false) break;
  217.   }
  218. }
  219. void fadeToBlack(int ledNo, byte fadeValue) {
  220. #ifdef ADAFRUIT_NEOPIXEL_H
  221.   // NeoPixel
  222.   uint32_t oldColor;
  223.   uint8_t r, g, b;
  224.   int value;
  225.   oldColor = strip.getPixelColor(ledNo);
  226.   r = (oldColor & 0x00ff0000UL) >> 16;
  227.   g = (oldColor & 0x0000ff00UL) >> 8;
  228.   b = (oldColor & 0x000000ffUL);
  229.   r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 256);
  230.   g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 256);
  231.   b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 256);
  232.   strip.setPixelColor(ledNo, r, g, b);
  233. #endif
  234. #ifndef ADAFRUIT_NEOPIXEL_H
  235.   // FastLED
  236.   leds[ledNo].fadeToBlackBy( fadeValue );
  237. #endif
  238. }
  239. void BouncingBalls(byte red, byte green, byte blue, int BallCount) {
  240.   float Gravity = -9.81;
  241.   int StartHeight = 1;
  242.   float Height[BallCount];
  243.   float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
  244.   float ImpactVelocity[BallCount];
  245.   float TimeSinceLastBounce[BallCount];
  246.   int   Position[BallCount];
  247.   long  ClockTimeSinceLastBounce[BallCount];
  248.   float Dampening[BallCount];
  249.   for (int i = 0 ; i < BallCount ; i++) {
  250.     ClockTimeSinceLastBounce[i] = millis();
  251.     Height[i] = StartHeight;
  252.     Position[i] = 0;
  253.     ImpactVelocity[i] = ImpactVelocityStart;
  254.     TimeSinceLastBounce[i] = 0;
  255.     Dampening[i] = 0.90 - float(i) / pow(BallCount, 2);
  256.     if (serialEvent() != false) break;
  257.   }
  258.   while (true) {
  259.     for (int i = 0 ; i < BallCount ; i++) {
  260.       TimeSinceLastBounce[i] =  millis() - ClockTimeSinceLastBounce[i];
  261.       Height[i] = 0.5 * Gravity * pow( TimeSinceLastBounce[i] / 1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i] / 1000;
  262.       if ( Height[i] < 0 ) {
  263.         Height[i] = 0;
  264.         ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
  265.         ClockTimeSinceLastBounce[i] = millis();
  266.         if ( ImpactVelocity[i] < 0.01 ) {
  267.           ImpactVelocity[i] = ImpactVelocityStart;
  268.         }
  269.       }
  270.       Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
  271.       if (serialEvent() != false) break;
  272.     }
  273.     for (int i = 0 ; i < BallCount ; i++) {
  274.       setPixel(Position[i], red, green, blue);
  275.       if (serialEvent() != false) break;
  276.     }
  277.     showStrip();
  278.     setAll(0, 0, 0);
  279.     if (serialEvent() != false) break;
  280.   }
  281. }
  282. void Fire(int Cooling, int Sparking, int SpeedDelay) {
  283.   static byte heat[NUM_LEDS];
  284.   int cooldown;
  285.   for ( int i = 0; i < NUM_LEDS; i++) {
  286.     cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
  287.     if (cooldown > heat[i]) {
  288.       heat[i] = 0;
  289.     } else {
  290.       heat[i] = heat[i] - cooldown;
  291.     }
  292.     if (serialEvent() != false) break;
  293.   }
  294.   for ( int k = NUM_LEDS - 1; k >= 2; k--) {
  295.     heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  296.     if (serialEvent() != false) break;
  297.   }
  298.   if ( random(255) < Sparking ) {
  299.     int y = random(7);
  300.     heat[y] = heat[y] + random(160, 255);
  301.   }
  302.   for ( int j = 0; j < NUM_LEDS; j++) {
  303.     setPixelHeatColor(j, heat[j] );
  304.     if (serialEvent() != false) break;
  305.   }
  306.   showStrip();
  307.   delay(SpeedDelay);
  308. }
  309. void setPixelHeatColor (int Pixel, byte temperature) {
  310.   byte t192 = round((temperature / 255.0) * 191);
  311.   byte heatramp = t192 & 0x3F; // 0..63
  312.   heatramp <<= 2; // scale up to 0..252
  313.   if ( t192 > 0x80) {                    // hottest
  314.     setPixel(Pixel, 255, 255, heatramp);
  315.   } else if ( t192 > 0x40 ) {            // middle
  316.     setPixel(Pixel, 255, heatramp, 0);
  317.   } else {                               // coolest
  318.     setPixel(Pixel, heatramp, 0, 0);
  319.   }
  320. }
  321. void theaterChaseRainbow(int SpeedDelay) {
  322.   byte *c;
  323.   for (int j = 0; j < 256; j++) {
  324.     for (int q = 0; q < 3; q++) {
  325.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  326.         c = Wheel( (i + j) % 255);
  327.         setPixel(i + q, *c, *(c + 1), *(c + 2));
  328.         if (serialEvent() != false) break;
  329.       }
  330.       showStrip();
  331.       delay(SpeedDelay);
  332.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  333.         setPixel(i + q, 0, 0, 0);
  334.         if (serialEvent() != false) break;
  335.       }
  336.       if (serialEvent() != false) break;
  337.     }
  338.     if (serialEvent() != false) break;
  339.   }
  340. }
  341. void theaterChase(byte red, byte green, byte blue, int SpeedDelay) {
  342.   for (int j = 0; j < 10; j++) {
  343.     for (int q = 0; q < 3; q++) {
  344.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  345.         setPixel(i + q, red, green, blue);
  346.         if (serialEvent() != false) break;
  347.       }
  348.       showStrip();
  349.       delay(SpeedDelay);
  350.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  351.         setPixel(i + q, 0, 0, 0);
  352.         if (serialEvent() != false) break;
  353.       }
  354.       if (serialEvent() != false) break;
  355.     }
  356.     if (serialEvent() != false) break;
  357.   }
  358. }
  359. void rainbowCycle(int SpeedDelay) {
  360.   byte *c;
  361.   uint16_t i, j;
  362.   for (j = 0; j < 256 * 5; j++) {
  363.     for (i = 0; i < NUM_LEDS; i++) {
  364.       c = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
  365.       setPixel(i, *c, *(c + 1), *(c + 2));
  366.       if (serialEvent() != false) break;
  367.     }
  368.     showStrip();
  369.     delay(SpeedDelay);
  370.     if (serialEvent() != false) break;
  371.   }
  372. }
  373. byte * Wheel(byte WheelPos) {
  374.   static byte c[3];
  375.   if (WheelPos < 85) {
  376.     c[0] = WheelPos * 3;
  377.     c[1] = 255 - WheelPos * 3;
  378.     c[2] = 0;
  379.   } else if (WheelPos < 170) {
  380.     WheelPos -= 85;
  381.     c[0] = 255 - WheelPos * 3;
  382.     c[1] = 0;
  383.     c[2] = WheelPos * 3;
  384.   } else {
  385.     WheelPos -= 170;
  386.     c[0] = 0;
  387.     c[1] = WheelPos * 3;
  388.     c[2] = 255 - WheelPos * 3;
  389.   }
  390.   return c;
  391. }
  392. void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  393.   for (uint16_t i = 0; i < NUM_LEDS; i++) {
  394.     setPixel(i, red, green, blue);
  395.     showStrip();
  396.     delay(SpeedDelay);
  397.     if (serialEvent() != false) break;
  398.   }
  399. }
  400. void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
  401.   int Position = 0;
  402.   for (int j = 0; j < NUM_LEDS * 2; j++)
  403.   {
  404.     Position++; // = 0; //Position + Rate;
  405.     for (int i = 0; i < NUM_LEDS; i++) {
  406.       // sine wave, 3 offset waves make a rainbow!
  407.       //float level = sin(i+Position) * 127 + 128;
  408.       //setPixel(i,level,0,0);
  409.       //float level = sin(i+Position) * 127 + 128;
  410.       setPixel(i, ((sin(i + Position) * 127 + 128) / 255)*red,
  411.                ((sin(i + Position) * 127 + 128) / 255)*green,
  412.                ((sin(i + Position) * 127 + 128) / 255)*blue);
  413.       if (serialEvent() != false) break;
  414.     }
  415.     showStrip();
  416.     delay(WaveDelay);
  417.     if (serialEvent() != false) break;
  418.   }
  419. }
  420. void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {
  421.   setAll(red, green, blue);
  422.   int Pixel = random(NUM_LEDS);
  423.   setPixel(Pixel, 0xff, 0xff, 0xff);
  424.   showStrip();
  425.   delay(SparkleDelay);
  426.   setPixel(Pixel, red, green, blue);
  427.   showStrip();
  428.   delay(SpeedDelay);
  429. }
  430. void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  431.   int Pixel = random(NUM_LEDS);
  432.   setPixel(Pixel, red, green, blue);
  433.   showStrip();
  434.   delay(SpeedDelay);
  435.   setPixel(Pixel, 0, 0, 0);
  436. }
  437. void TwinkleRandom(int Count, int SpeedDelay, boolean OnlyOne) {
  438.   setAll(0, 0, 0);
  439.   for (int i = 0; i < Count; i++) {
  440.     setPixel(random(NUM_LEDS), random(0, 255), random(0, 255), random(0, 255));
  441.     showStrip();
  442.     delay(SpeedDelay);
  443.     if (OnlyOne) {
  444.       setAll(0, 0, 0);
  445.     }
  446.     if (serialEvent() != false) break;
  447.   }
  448.   delay(SpeedDelay);
  449. }
  450. void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
  451.   setAll(0, 0, 0);
  452.   for (int i = 0; i < Count; i++) {
  453.     setPixel(random(NUM_LEDS), red, green, blue);
  454.     showStrip();
  455.     delay(SpeedDelay);
  456.     if (OnlyOne) {
  457.       setAll(0, 0, 0);
  458.     }
  459.     if (serialEvent() != false) break;
  460.   }
  461.   delay(SpeedDelay);
  462. }
  463. void NewKITT(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  464.   RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  465.   LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  466.   /*OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  467.     CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  468.     LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  469.     RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  470.     OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  471.     CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);*/
  472. }
  473. void CenterToOutside(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  474.   for (int i = ((NUM_LEDS - EyeSize) / 2); i >= 0; i--) {
  475.     setAll(0, 0, 0);
  476.     setPixel(i, red / 10, green / 10, blue / 10);
  477.     for (int j = 1; j <= EyeSize; j++) {
  478.       setPixel(i + j, red, green, blue);
  479.       if (serialEvent() != false) break;
  480.     }
  481.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  482.     setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
  483.     for (int j = 1; j <= EyeSize; j++) {
  484.       setPixel(NUM_LEDS - i - j, red, green, blue);
  485.       if (serialEvent() != false) break;
  486.     }
  487.     setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);
  488.     showStrip();
  489.     delay(SpeedDelay);
  490.     if (serialEvent() != false) break;
  491.   }
  492.   delay(ReturnDelay);
  493. }
  494. void OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  495.   for (int i = 0; i <= ((NUM_LEDS - EyeSize) / 2); i++) {
  496.     setAll(0, 0, 0);
  497.     setPixel(i, red / 10, green / 10, blue / 10);
  498.     for (int j = 1; j <= EyeSize; j++) {
  499.       setPixel(i + j, red, green, blue);
  500.       if (serialEvent() != false) break;
  501.     }
  502.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  503.     setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
  504.     for (int j = 1; j <= EyeSize; j++) {
  505.       setPixel(NUM_LEDS - i - j, red, green, blue);
  506.       if (serialEvent() != false) break;
  507.     }
  508.     setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);
  509.     showStrip();
  510.     delay(SpeedDelay);
  511.     if (serialEvent() != false) break;
  512.   }
  513.   delay(ReturnDelay);
  514. }
  515. void LeftToRight(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  516.   for (int i = 0; i < NUM_LEDS - EyeSize - 2; i++) {
  517.     setAll(0, 0, 0);
  518.     setPixel(i, red / 10, green / 10, blue / 10);
  519.     for (int j = 1; j <= EyeSize; j++) {
  520.       setPixel(i + j, red, green, blue);
  521.       if (serialEvent() != false) break;;
  522.     }
  523.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  524.     showStrip();
  525.     delay(SpeedDelay);
  526.     if (serialEvent() != false) break;
  527.   }
  528.   delay(ReturnDelay);
  529. }
  530. void RightToLeft(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  531.   for (int i = NUM_LEDS - EyeSize - 2; i > 0; i--) {
  532.     setAll(0, 0, 0);
  533.     setPixel(i, red / 10, green / 10, blue / 10);
  534.     for (int j = 1; j <= EyeSize; j++) {
  535.       setPixel(i + j, red, green, blue);
  536.       if (serialEvent() != false) break;
  537.     }
  538.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  539.     showStrip();
  540.     delay(SpeedDelay);
  541.     if (serialEvent() != false) break;
  542.   }
  543.   delay(ReturnDelay);
  544. }
  545. void HalloweenEyes(byte red, byte green, byte blue, int EyeWidth, int EyeSpace, boolean Fade, int Steps, int FadeDelay, int EndPause) {
  546.   randomSeed(analogRead(0));
  547.   int i;
  548.   int StartPoint  = random( 0, NUM_LEDS - (2 * EyeWidth) - EyeSpace );
  549.   int Start2ndEye = StartPoint + EyeWidth + EyeSpace;
  550.   for (i = 0; i < EyeWidth; i++) {
  551.     setPixel(StartPoint + i, red, green, blue);
  552.     setPixel(Start2ndEye + i, red, green, blue);
  553.     if (serialEvent() != false) break;
  554.   }
  555.   showStrip();
  556.   if (Fade == true) {
  557.     float r, g, b;
  558.     for (int j = Steps; j >= 0; j--) {
  559.       r = j * (red / Steps);
  560.       g = j * (green / Steps);
  561.       b = j * (blue / Steps);
  562.       for (i = 0; i < EyeWidth; i++) {
  563.         setPixel(StartPoint + i, r, g, b);
  564.         setPixel(Start2ndEye + i, r, g, b);
  565.         if (serialEvent() != false) break;
  566.       }
  567.       showStrip();
  568.       delay(FadeDelay);
  569.       if (serialEvent() != false) break;
  570.     }
  571.   }
  572.   setAll(0, 0, 0); // Set all black
  573.   delay(EndPause);
  574. }
  575. void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause) {
  576.   for (int j = 0; j < StrobeCount; j++) {
  577.     setAll(red, green, blue);
  578.     showStrip();
  579.     delay(FlashDelay);
  580.     setAll(0, 0, 0);
  581.     showStrip();
  582.     delay(FlashDelay);
  583.     if (serialEvent() != false) break;
  584.   }
  585.   delay(EndPause);
  586. }
  587. void RGBLoop() {
  588.   for (int j = 0; j < 3; j++ ) {
  589.     // Fade IN
  590.     for (int k = 0; k < 256; k++) {
  591.       switch (j) {
  592.         case 0: setAll(k, 0, 0); if (serialEvent() != false) break; break;
  593.         case 1: setAll(0, k, 0); if (serialEvent() != false) break; break;
  594.         case 2: setAll(0, 0, k); if (serialEvent() != false) break; break;
  595.       }
  596.       showStrip();
  597.       if (serialEvent() != false)break;
  598.     }
  599.     // Fade OUT
  600.     for (int k = 255; k >= 0; k--) {
  601.       switch (j) {
  602.         case 0: setAll(k, 0, 0); if (serialEvent() != false) break; break;
  603.         case 1: setAll(0, k, 0); if (serialEvent() != false) break; break;
  604.         case 2: setAll(0, 0, k); if (serialEvent() != false) break; break;
  605.       }
  606.       showStrip();
  607.       if (serialEvent() != false)break;
  608.     }
  609.     if (serialEvent() != false)break;
  610.   }
  611. }
  612. void showStrip() {
  613. #ifdef ADAFRUIT_NEOPIXEL_H
  614.   // NeoPixel
  615.   strip.show();
  616. #endif
  617. #ifndef ADAFRUIT_NEOPIXEL_H
  618.   // FastLED
  619.   FastLED.show();
  620. #endif
  621. }
  622. void setPixel(int Pixel, byte red, byte green, byte blue) {
  623. #ifdef ADAFRUIT_NEOPIXEL_H
  624.   // NeoPixel
  625.   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
  626. #endif
  627. #ifndef ADAFRUIT_NEOPIXEL_H
  628.   // FastLED
  629.   leds[Pixel].r = red;
  630.   leds[Pixel].g = green;
  631.   leds[Pixel].b = blue;
  632. #endif
  633. }
  634. void setAll(byte red, byte green, byte blue) {
  635.   for (int i = 0; i < NUM_LEDS; i++ ) {
  636.     setPixel(i, red, green, blue);
  637.   }
  638.   showStrip();
  639. }
  640. bool serialEvent() {
  641.   if (Serial.available()) {
  642.     veri = Serial.readString();
  643.     if (veri.charAt(0) == 'b') {
  644.       veri = veri.substring(1, 4);
  645.       parlaklik = veri.toInt();
  646.       parlaklik = map(parlaklik, 0, 100, 0, 255);
  647.       strip.setBrightness(parlaklik);
  648.     }
  649.     else {
  650.       mod = veri.toInt();
  651.       return true;
  652.     }
  653.   }
  654.   return false;
  655. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 11:43:04

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十二:TwinkleFOX-淡入淡出的闪烁“假日”灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十二:TwinkleFOX-淡入淡出的闪烁“假日”灯
  5. */
  6. #include "FastLED.h"
  7. #define NUM_LEDS      256
  8. #define LED_TYPE   WS2811
  9. #define COLOR_ORDER   GRB
  10. #define DATA_PIN        6
  11. //#define CLK_PIN       4
  12. #define VOLTS          12
  13. #define MAX_MA       4000
  14. //  TwinkleFOX: Twinkling 'holiday' lights that fade in and out.
  15. //  Colors are chosen from a palette; a few palettes are provided.
  16. //
  17. //  This December 2015 implementation improves on the December 2014 version
  18. //  in several ways:
  19. //  - smoother fading, compatible with any colors and any palettes
  20. //  - easier control of twinkle speed and twinkle density
  21. //  - supports an optional 'background color'
  22. //  - takes even less RAM: zero RAM overhead per pixel
  23. //  - illustrates a couple of interesting techniques (uh oh...)
  24. //
  25. //  The idea behind this (new) implementation is that there's one
  26. //  basic, repeating pattern that each pixel follows like a waveform:
  27. //  The brightness rises from 0..255 and then falls back down to 0.
  28. //  The brightness at any given point in time can be determined as
  29. //  as a function of time, for example:
  30. //    brightness = sine( time ); // a sine wave of brightness over time
  31. //
  32. //  So the way this implementation works is that every pixel follows
  33. //  the exact same wave function over time.  In this particular case,
  34. //  I chose a sawtooth triangle wave (triwave8) rather than a sine wave,
  35. //  but the idea is the same: brightness = triwave8( time ).
  36. //
  37. //  Of course, if all the pixels used the exact same wave form, and
  38. //  if they all used the exact same 'clock' for their 'time base', all
  39. //  the pixels would brighten and dim at once -- which does not look
  40. //  like twinkling at all.
  41. //
  42. //  So to achieve random-looking twinkling, each pixel is given a
  43. //  slightly different 'clock' signal.  Some of the clocks run faster,
  44. //  some run slower, and each 'clock' also has a random offset from zero.
  45. //  The net result is that the 'clocks' for all the pixels are always out
  46. //  of sync from each other, producing a nice random distribution
  47. //  of twinkles.
  48. //
  49. //  The 'clock speed adjustment' and 'time offset' for each pixel
  50. //  are generated randomly.  One (normal) approach to implementing that
  51. //  would be to randomly generate the clock parameters for each pixel
  52. //  at startup, and store them in some arrays.  However, that consumes
  53. //  a great deal of precious RAM, and it turns out to be totally
  54. //  unnessary!  If the random number generate is 'seeded' with the
  55. //  same starting value every time, it will generate the same sequence
  56. //  of values every time.  So the clock adjustment parameters for each
  57. //  pixel are 'stored' in a pseudo-random number generator!  The PRNG
  58. //  is reset, and then the first numbers out of it are the clock
  59. //  adjustment parameters for the first pixel, the second numbers out
  60. //  of it are the parameters for the second pixel, and so on.
  61. //  In this way, we can 'store' a stable sequence of thousands of
  62. //  random clock adjustment parameters in literally two bytes of RAM.
  63. //
  64. //  There's a little bit of fixed-point math involved in applying the
  65. //  clock speed adjustments, which are expressed in eighths.  Each pixel's
  66. //  clock speed ranges from 8/8ths of the system clock (i.e. 1x) to
  67. //  23/8ths of the system clock (i.e. nearly 3x).
  68. //
  69. //  On a basic Arduino Uno or Leonardo, this code can twinkle 300+ pixels
  70. //  smoothly at over 50 updates per seond.
  71. //
  72. //  -Mark Kriegsman, December 2015
  73. CRGBArray<NUM_LEDS> leds;
  74. // Overall twinkle speed.
  75. // 0 (VERY slow) to 8 (VERY fast).
  76. // 4, 5, and 6 are recommended, default is 4.
  77. #define TWINKLE_SPEED 4
  78. // Overall twinkle density.
  79. // 0 (NONE lit) to 8 (ALL lit at once).
  80. // Default is 5.
  81. #define TWINKLE_DENSITY 5
  82. // How often to change color palettes.
  83. #define SECONDS_PER_PALETTE  30
  84. // Also: toward the bottom of the file is an array
  85. // called "ActivePaletteList" which controls which color
  86. // palettes are used; you can add or remove color palettes
  87. // from there freely.
  88. // Background color for 'unlit' pixels
  89. // Can be set to CRGB::Black if desired.
  90. CRGB gBackgroundColor = CRGB::Black;
  91. // Example of dim incandescent fairy light background color
  92. // CRGB gBackgroundColor = CRGB(CRGB::FairyLight).nscale8_video(16);
  93. // If AUTO_SELECT_BACKGROUND_COLOR is set to 1,
  94. // then for any palette where the first two entries
  95. // are the same, a dimmed version of that color will
  96. // automatically be used as the background color.
  97. #define AUTO_SELECT_BACKGROUND_COLOR 0
  98. // If COOL_LIKE_INCANDESCENT is set to 1, colors will
  99. // fade out slighted 'reddened', similar to how
  100. // incandescent bulbs change color as they get dim down.
  101. #define COOL_LIKE_INCANDESCENT 1
  102. CRGBPalette16 gCurrentPalette;
  103. CRGBPalette16 gTargetPalette;
  104. void setup() {
  105.   delay( 1000 ); //safety startup delay
  106.   FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
  107.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
  108.   .setCorrection(TypicalLEDStrip);
  109.   FastLED.setBrightness(23);
  110.   chooseNextColorPalette(gTargetPalette);
  111. }
  112. void loop()
  113. {
  114.   EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
  115.     chooseNextColorPalette( gTargetPalette );
  116.   }
  117.   EVERY_N_MILLISECONDS( 10 ) {
  118.     nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 12);
  119.   }
  120.   drawTwinkles( leds);
  121.   FastLED.show();
  122. }
  123. //  This function loops over each pixel, calculates the
  124. //  adjusted 'clock' that this pixel should use, and calls
  125. //  "CalculateOneTwinkle" on each pixel.  It then displays
  126. //  either the twinkle color of the background color,
  127. //  whichever is brighter.
  128. void drawTwinkles( CRGBSet& L)
  129. {
  130.   // "PRNG16" is the pseudorandom number generator
  131.   // It MUST be reset to the same starting value each time
  132.   // this function is called, so that the sequence of 'random'
  133.   // numbers that it generates is (paradoxically) stable.
  134.   uint16_t PRNG16 = 11337;
  135.   uint32_t clock32 = millis();
  136.   // Set up the background color, "bg".
  137.   // if AUTO_SELECT_BACKGROUND_COLOR == 1, and the first two colors of
  138.   // the current palette are identical, then a deeply faded version of
  139.   // that color is used for the background color
  140.   CRGB bg;
  141.   if ( (AUTO_SELECT_BACKGROUND_COLOR == 1) &&
  142.        (gCurrentPalette[0] == gCurrentPalette[1] )) {
  143.     bg = gCurrentPalette[0];
  144.     uint8_t bglight = bg.getAverageLight();
  145.     if ( bglight > 64) {
  146.       bg.nscale8_video( 16); // very bright, so scale to 1/16th
  147.     } else if ( bglight > 16) {
  148.       bg.nscale8_video( 64); // not that bright, so scale to 1/4th
  149.     } else {
  150.       bg.nscale8_video( 86); // dim, scale to 1/3rd.
  151.     }
  152.   } else {
  153.     bg = gBackgroundColor; // just use the explicitly defined background color
  154.   }
  155.   uint8_t backgroundBrightness = bg.getAverageLight();
  156.   for ( CRGB& pixel : L) {
  157.     PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
  158.     uint16_t myclockoffset16 = PRNG16; // use that number as clock offset
  159.     PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
  160.     // use that number as clock speed adjustment factor (in 8ths, from 8/8ths to 23/8ths)
  161.     uint8_t myspeedmultiplierQ5_3 =  ((((PRNG16 & 0xFF) >> 4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
  162.     uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
  163.     uint8_t  myunique8 = PRNG16 >> 8; // get 'salt' value for this pixel
  164.     // We now have the adjusted 'clock' for this pixel, now we call
  165.     // the function that computes what color the pixel should be based
  166.     // on the "brightness = f( time )" idea.
  167.     CRGB c = computeOneTwinkle( myclock30, myunique8);
  168.     uint8_t cbright = c.getAverageLight();
  169.     int16_t deltabright = cbright - backgroundBrightness;
  170.     if ( deltabright >= 32 || (!bg)) {
  171.       // If the new pixel is significantly brighter than the background color,
  172.       // use the new color.
  173.       pixel = c;
  174.     } else if ( deltabright > 0 ) {
  175.       // If the new pixel is just slightly brighter than the background color,
  176.       // mix a blend of the new color and the background color
  177.       pixel = blend( bg, c, deltabright * 8);
  178.     } else {
  179.       // if the new pixel is not at all brighter than the background color,
  180.       // just use the background color.
  181.       pixel = bg;
  182.     }
  183.   }
  184. }
  185. //  This function takes a time in pseudo-milliseconds,
  186. //  figures out brightness = f( time ), and also hue = f( time )
  187. //  The 'low digits' of the millisecond time are used as
  188. //  input to the brightness wave function.
  189. //  The 'high digits' are used to select a color, so that the color
  190. //  does not change over the course of the fade-in, fade-out
  191. //  of one cycle of the brightness wave function.
  192. //  The 'high digits' are also used to determine whether this pixel
  193. //  should light at all during this cycle, based on the TWINKLE_DENSITY.
  194. CRGB computeOneTwinkle( uint32_t ms, uint8_t salt)
  195. {
  196.   uint16_t ticks = ms >> (8 - TWINKLE_SPEED);
  197.   uint8_t fastcycle8 = ticks;
  198.   uint16_t slowcycle16 = (ticks >> 8) + salt;
  199.   slowcycle16 += sin8( slowcycle16);
  200.   slowcycle16 =  (slowcycle16 * 2053) + 1384;
  201.   uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8);
  202.   uint8_t bright = 0;
  203.   if ( ((slowcycle8 & 0x0E) / 2) < TWINKLE_DENSITY) {
  204.     bright = attackDecayWave8( fastcycle8);
  205.   }
  206.   uint8_t hue = slowcycle8 - salt;
  207.   CRGB c;
  208.   if ( bright > 0) {
  209.     c = ColorFromPalette( gCurrentPalette, hue, bright, NOBLEND);
  210.     if ( COOL_LIKE_INCANDESCENT == 1 ) {
  211.       coolLikeIncandescent( c, fastcycle8);
  212.     }
  213.   } else {
  214.     c = CRGB::Black;
  215.   }
  216.   return c;
  217. }
  218. // This function is like 'triwave8', which produces a
  219. // symmetrical up-and-down triangle sawtooth waveform, except that this
  220. // function produces a triangle wave with a faster attack and a slower decay:
  221. //
  222. //     / \
  223. //    /     \
  224. //   /         \
  225. //  /             \
  226. //
  227. uint8_t attackDecayWave8( uint8_t i)
  228. {
  229.   if ( i < 86) {
  230.     return i * 3;
  231.   } else {
  232.     i -= 86;
  233.     return 255 - (i + (i / 2));
  234.   }
  235. }
  236. // This function takes a pixel, and if its in the 'fading down'
  237. // part of the cycle, it adjusts the color a little bit like the
  238. // way that incandescent bulbs fade toward 'red' as they dim.
  239. void coolLikeIncandescent( CRGB& c, uint8_t phase)
  240. {
  241.   if ( phase < 128) return;
  242.   uint8_t cooling = (phase - 128) >> 4;
  243.   c.g = qsub8( c.g, cooling);
  244.   c.b = qsub8( c.b, cooling * 2);
  245. }
  246. // A mostly red palette with green accents and white trim.
  247. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  248. const TProgmemRGBPalette16 RedGreenWhite_p FL_PROGMEM =
  249. { CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red,
  250.   CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red,
  251.   CRGB::Red, CRGB::Red, CRGB::Gray, CRGB::Gray,
  252.   CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green
  253. };
  254. // A mostly (dark) green palette with red berries.
  255. #define Holly_Green 0x00580c
  256. #define Holly_Red   0xB00402
  257. const TProgmemRGBPalette16 Holly_p FL_PROGMEM =
  258. { Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  259.   Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  260.   Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  261.   Holly_Green, Holly_Green, Holly_Green, Holly_Red
  262. };
  263. // A red and white striped palette
  264. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  265. const TProgmemRGBPalette16 RedWhite_p FL_PROGMEM =
  266. { CRGB::Red,  CRGB::Red,  CRGB::Red,  CRGB::Red,
  267.   CRGB::Gray, CRGB::Gray, CRGB::Gray, CRGB::Gray,
  268.   CRGB::Red,  CRGB::Red,  CRGB::Red,  CRGB::Red,
  269.   CRGB::Gray, CRGB::Gray, CRGB::Gray, CRGB::Gray
  270. };
  271. // A mostly blue palette with white accents.
  272. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  273. const TProgmemRGBPalette16 BlueWhite_p FL_PROGMEM =
  274. { CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  275.   CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  276.   CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  277.   CRGB::Blue, CRGB::Gray, CRGB::Gray, CRGB::Gray
  278. };
  279. // A pure "fairy light" palette with some brightness variations
  280. #define HALFFAIRY ((CRGB::FairyLight & 0xFEFEFE) / 2)
  281. #define QUARTERFAIRY ((CRGB::FairyLight & 0xFCFCFC) / 4)
  282. const TProgmemRGBPalette16 FairyLight_p FL_PROGMEM =
  283. { CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight,
  284.   HALFFAIRY,        HALFFAIRY,        CRGB::FairyLight, CRGB::FairyLight,
  285.   QUARTERFAIRY,     QUARTERFAIRY,     CRGB::FairyLight, CRGB::FairyLight,
  286.   CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight
  287. };
  288. // A palette of soft snowflakes with the occasional bright one
  289. const TProgmemRGBPalette16 Snow_p FL_PROGMEM =
  290. { 0x304048, 0x304048, 0x304048, 0x304048,
  291.   0x304048, 0x304048, 0x304048, 0x304048,
  292.   0x304048, 0x304048, 0x304048, 0x304048,
  293.   0x304048, 0x304048, 0x304048, 0xE0F0FF
  294. };
  295. // A palette reminiscent of large 'old-school' C9-size tree lights
  296. // in the five classic colors: red, orange, green, blue, and white.
  297. #define C9_Red    0xB80400
  298. #define C9_Orange 0x902C02
  299. #define C9_Green  0x046002
  300. #define C9_Blue   0x070758
  301. #define C9_White  0x606820
  302. const TProgmemRGBPalette16 RetroC9_p FL_PROGMEM =
  303. { C9_Red,    C9_Orange, C9_Red,    C9_Orange,
  304.   C9_Orange, C9_Red,    C9_Orange, C9_Red,
  305.   C9_Green,  C9_Green,  C9_Green,  C9_Green,
  306.   C9_Blue,   C9_Blue,   C9_Blue,
  307.   C9_White
  308. };
  309. // A cold, icy pale blue palette
  310. #define Ice_Blue1 0x0C1040
  311. #define Ice_Blue2 0x182080
  312. #define Ice_Blue3 0x5080C0
  313. const TProgmemRGBPalette16 Ice_p FL_PROGMEM =
  314. {
  315.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  316.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  317.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  318.   Ice_Blue2, Ice_Blue2, Ice_Blue2, Ice_Blue3
  319. };
  320. // Add or remove palette names from this list to control which color
  321. // palettes are used, and in what order.
  322. const TProgmemRGBPalette16* ActivePaletteList[] = {
  323.   &RetroC9_p,
  324.   &BlueWhite_p,
  325.   &RainbowColors_p,
  326.   &FairyLight_p,
  327.   &RedGreenWhite_p,
  328.   &PartyColors_p,
  329.   &RedWhite_p,
  330.   &Snow_p,
  331.   &Holly_p,
  332.   &Ice_p
  333. };
  334. // Advance to the next color palette in the list (above).
  335. void chooseNextColorPalette( CRGBPalette16& pal)
  336. {
  337.   const uint8_t numberOfPalettes = sizeof(ActivePaletteList) / sizeof(ActivePaletteList[0]);
  338.   static uint8_t whichPalette = -1;
  339.   whichPalette = addmod8( whichPalette, 1, numberOfPalettes);
  340.   pal = *(ActivePaletteList[whichPalette]);
  341. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 14:31:41

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十四:将噪声数据转换为 LED 阵列中的动态颜色

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十四:将噪声数据转换为 LED 阵列中的动态颜色
  5. */
  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define BRIGHTNESS  26
  9. #define LED_TYPE    WS2811
  10. #define COLOR_ORDER GRB
  11. // Params for width and height
  12. const uint8_t kMatrixWidth  = 8;
  13. const uint8_t kMatrixHeight = 32;
  14. // Param for different pixel layouts
  15. const bool    kMatrixSerpentineLayout = true;
  16. // This example combines two features of FastLED to produce a remarkable range of
  17. // effects from a relatively small amount of code.  This example combines FastLED's
  18. // color palette lookup functions with FastLED's Perlin noise generator, and
  19. // the combination is extremely powerful.
  20. //
  21. // You might want to look at the "ColorPalette" and "Noise" examples separately
  22. // if this example code seems daunting.
  23. //
  24. //
  25. // The basic setup here is that for each frame, we generate a new array of
  26. // 'noise' data, and then map it onto the LED matrix through a color palette.
  27. //
  28. // Periodically, the color palette is changed, and new noise-generation parameters
  29. // are chosen at the same time.  In this example, specific noise-generation
  30. // values have been selected to match the given color palettes; some are faster,
  31. // or slower, or larger, or smaller than others, but there's no reason these
  32. // parameters can't be freely mixed-and-matched.
  33. //
  34. // In addition, this example includes some fast automatic 'data smoothing' at
  35. // lower noise speeds to help produce smoother animations in those cases.
  36. //
  37. // The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are
  38. // used, as well as some 'hand-defined' ones, and some proceedurally generated
  39. // palettes.
  40. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  41. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  42. // The leds
  43. CRGB leds[kMatrixWidth * kMatrixHeight];
  44. // The 16 bit version of our coordinates
  45. static uint16_t x;
  46. static uint16_t y;
  47. static uint16_t z;
  48. // We're using the x/y dimensions to map to the x/y pixels on the matrix.  We'll
  49. // use the z-axis for "time".  speed determines how fast time moves forward.  Try
  50. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  51. // water.
  52. uint16_t speed = 20; // speed is set dynamically once we've started up
  53. // Scale determines how far apart the pixels in our noise matrix are.  Try
  54. // changing these values around to see how it affects the motion of the display.  The
  55. // higher the value of scale, the more "zoomed out" the noise iwll be.  A value
  56. // of 1 will be so zoomed in, you'll mostly see solid colors.
  57. uint16_t scale = 30; // scale is set dynamically once we've started up
  58. // This is the array that we keep our computed noise values in
  59. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  60. CRGBPalette16 currentPalette( PartyColors_p );
  61. uint8_t       colorLoop = 1;
  62. void setup() {
  63.   delay(3000);
  64.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  65.   FastLED.setBrightness(BRIGHTNESS);
  66.   // Initialize our coordinates to some random values
  67.   x = random16();
  68.   y = random16();
  69.   z = random16();
  70. }
  71. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  72. void fillnoise8() {
  73.   // If we're runing at a low "speed", some 8-bit artifacts become visible
  74.   // from frame-to-frame.  In order to reduce this, we can do some fast data-smoothing.
  75.   // The amount of data smoothing we're doing depends on "speed".
  76.   uint8_t dataSmoothing = 0;
  77.   if ( speed < 50) {
  78.     dataSmoothing = 200 - (speed * 4);
  79.   }
  80.   for (int i = 0; i < MAX_DIMENSION; i++) {
  81.     int ioffset = scale * i;
  82.     for (int j = 0; j < MAX_DIMENSION; j++) {
  83.       int joffset = scale * j;
  84.       uint8_t data = inoise8(x + ioffset, y + joffset, z);
  85.       // The range of the inoise8 function is roughly 16-238.
  86.       // These two operations expand those values out to roughly 0..255
  87.       // You can comment them out if you want the raw noise data.
  88.       data = qsub8(data, 16);
  89.       data = qadd8(data, scale8(data, 39));
  90.       if ( dataSmoothing ) {
  91.         uint8_t olddata = noise[i][j];
  92.         uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  93.         data = newdata;
  94.       }
  95.       noise[i][j] = data;
  96.     }
  97.   }
  98.   z += speed;
  99.   // apply slow drift to X and Y, just for visual variation.
  100.   x += speed / 8;
  101.   y -= speed / 16;
  102. }
  103. void mapNoiseToLEDsUsingPalette()
  104. {
  105.   static uint8_t ihue = 0;
  106.   for (int i = 0; i < kMatrixWidth; i++) {
  107.     for (int j = 0; j < kMatrixHeight; j++) {
  108.       // We use the value at the (i,j) coordinate in the noise
  109.       // array for our brightness, and the flipped value from (j,i)
  110.       // for our pixel's index into the color palette.
  111.       uint8_t index = noise[j][i];
  112.       uint8_t bri =   noise[i][j];
  113.       // if this palette is a 'loop', add a slowly-changing base value
  114.       if ( colorLoop) {
  115.         index += ihue;
  116.       }
  117.       // brighten up, as the color palette itself often contains the
  118.       // light/dark dynamic range desired
  119.       if ( bri > 127 ) {
  120.         bri = 255;
  121.       } else {
  122.         bri = dim8_raw( bri * 2);
  123.       }
  124.       CRGB color = ColorFromPalette( currentPalette, index, bri);
  125.       leds[XY(i, j)] = color;
  126.     }
  127.   }
  128.   ihue += 1;
  129. }
  130. void loop() {
  131.   // Periodically choose a new palette, speed, and scale
  132.   ChangePaletteAndSettingsPeriodically();
  133.   // generate noise data
  134.   fillnoise8();
  135.   // convert the noise data to colors in the LED array
  136.   // using the current palette
  137.   mapNoiseToLEDsUsingPalette();
  138.   FastLED.show();
  139.   // delay(10);
  140. }
  141. // There are several different palettes of colors demonstrated here.
  142. //
  143. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  144. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  145. //
  146. // Additionally, you can manually define your own color palettes, or you can write
  147. // code that creates color palettes on the fly.
  148. // 1 = 5 sec per palette
  149. // 2 = 10 sec per palette
  150. // etc
  151. #define HOLD_PALETTES_X_TIMES_AS_LONG 1
  152. void ChangePaletteAndSettingsPeriodically()
  153. {
  154.   uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60;
  155.   static uint8_t lastSecond = 99;
  156.   if ( lastSecond != secondHand) {
  157.     lastSecond = secondHand;
  158.     if ( secondHand ==  0)  {
  159.       currentPalette = RainbowColors_p;
  160.       speed = 20;
  161.       scale = 30;
  162.       colorLoop = 1;
  163.     }
  164.     if ( secondHand ==  5)  {
  165.       SetupPurpleAndGreenPalette();
  166.       speed = 10;
  167.       scale = 50;
  168.       colorLoop = 1;
  169.     }
  170.     if ( secondHand == 10)  {
  171.       SetupBlackAndWhiteStripedPalette();
  172.       speed = 20;
  173.       scale = 30;
  174.       colorLoop = 1;
  175.     }
  176.     if ( secondHand == 15)  {
  177.       currentPalette = ForestColors_p;
  178.       speed =  8;
  179.       scale = 120;
  180.       colorLoop = 0;
  181.     }
  182.     if ( secondHand == 20)  {
  183.       currentPalette = CloudColors_p;
  184.       speed =  4;
  185.       scale = 30;
  186.       colorLoop = 0;
  187.     }
  188.     if ( secondHand == 25)  {
  189.       currentPalette = LavaColors_p;
  190.       speed =  8;
  191.       scale = 50;
  192.       colorLoop = 0;
  193.     }
  194.     if ( secondHand == 30)  {
  195.       currentPalette = OceanColors_p;
  196.       speed = 20;
  197.       scale = 90;
  198.       colorLoop = 0;
  199.     }
  200.     if ( secondHand == 35)  {
  201.       currentPalette = PartyColors_p;
  202.       speed = 20;
  203.       scale = 30;
  204.       colorLoop = 1;
  205.     }
  206.     if ( secondHand == 40)  {
  207.       SetupRandomPalette();
  208.       speed = 20;
  209.       scale = 20;
  210.       colorLoop = 1;
  211.     }
  212.     if ( secondHand == 45)  {
  213.       SetupRandomPalette();
  214.       speed = 50;
  215.       scale = 50;
  216.       colorLoop = 1;
  217.     }
  218.     if ( secondHand == 50)  {
  219.       SetupRandomPalette();
  220.       speed = 90;
  221.       scale = 90;
  222.       colorLoop = 1;
  223.     }
  224.     if ( secondHand == 55)  {
  225.       currentPalette = RainbowStripeColors_p;
  226.       speed = 30;
  227.       scale = 20;
  228.       colorLoop = 1;
  229.     }
  230.   }
  231. }
  232. // This function generates a random palette that's a gradient
  233. // between four different colors.  The first is a dim hue, the second is
  234. // a bright hue, the third is a bright pastel, and the last is
  235. // another bright hue.  This gives some visual bright/dark variation
  236. // which is more interesting than just a gradient of different hues.
  237. void SetupRandomPalette()
  238. {
  239.   currentPalette = CRGBPalette16(
  240.                      CHSV( random8(), 255, 32),
  241.                      CHSV( random8(), 255, 255),
  242.                      CHSV( random8(), 128, 255),
  243.                      CHSV( random8(), 255, 255));
  244. }
  245. // This function sets up a palette of black and white stripes,
  246. // using code.  Since the palette is effectively an array of
  247. // sixteen CRGB colors, the various fill_* functions can be used
  248. // to set them up.
  249. void SetupBlackAndWhiteStripedPalette()
  250. {
  251.   // 'black out' all 16 palette entries...
  252.   fill_solid( currentPalette, 16, CRGB::Black);
  253.   // and set every fourth one to white.
  254.   currentPalette[0] = CRGB::White;
  255.   currentPalette[4] = CRGB::White;
  256.   currentPalette[8] = CRGB::White;
  257.   currentPalette[12] = CRGB::White;
  258. }
  259. // This function sets up a palette of purple and green stripes.
  260. void SetupPurpleAndGreenPalette()
  261. {
  262.   CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  263.   CRGB green  = CHSV( HUE_GREEN, 255, 255);
  264.   CRGB black  = CRGB::Black;
  265.   currentPalette = CRGBPalette16(
  266.                      green,  green,  black,  black,
  267.                      purple, purple, black,  black,
  268.                      green,  green,  black,  black,
  269.                      purple, purple, black,  black );
  270. }
  271. //
  272. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  273. //
  274. uint16_t XY( uint8_t x, uint8_t y)
  275. {
  276.   uint16_t i;
  277.   if ( kMatrixSerpentineLayout == false) {
  278.     i = (y * kMatrixWidth) + x;
  279.   }
  280.   if ( kMatrixSerpentineLayout == true) {
  281.     if ( y & 0x01) {
  282.       // Odd rows run backwards
  283.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  284.       i = (y * kMatrixWidth) + reverseX;
  285.     } else {
  286.       // Even rows run forwards
  287.       i = (y * kMatrixWidth) + x;
  288.     }
  289.   }
  290.   return i;
  291. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-21 18:05:13

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1


WS2812B主要特点

智能反接保护,电源反接不会损坏IC。
IC控制电路与LED点光源公用一个电源。
控制电路与RGB芯片集成在一个5050封装的元器件中,构成一个完整的外控像素点。
内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
内置上电复位和掉电复位电路。
每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。
串行级联接口,能通过一根信号线完成数据的接收与解码。
任意两点传传输距离在不超过5米时无需增加任何电路。
当刷新速率30帧/秒时,级联数不小于1024点。
数据发送速度可达800Kbps。
光的颜色高度一致,性价比高。

主要应用领域
LED全彩发光字灯串,LED全彩模组, LED全彩软灯条硬灯条,LED护栏管。
LED点光源,LED像素屏,LED异形屏,各种电子产品,电器设备跑马灯。
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-21 18:10:27

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-21 18:14:53

WS2812B灯屏电原理参考图


【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-21 18:30:19

实验涉及到的几个WS2812B相关库
安装FastLED库,工具—管理库—搜索FastLED—安装
安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
安装Adafruit_NeoPixel库,
下载https://github.com/adafruit/Adafruit_NeoPixel

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-21 18:47:48

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 10:15:02

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之一:LED循环绿色快闪测试

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之一:LED循环绿色快闪测试
  5. */
  6. #include <Adafruit_NeoPixel.h>
  7. #define PIN 6
  8. #define MAX_LED 255
  9. #define ADD true
  10. #define SUB false
  11. int val = 0;
  12. boolean stat = ADD;
  13. Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, PIN, NEO_RGB + NEO_KHZ800 );
  14. void setup() {
  15.   strip.begin();
  16.   strip.show();
  17. }
  18. void loop() {
  19.   uint8_t i, a = 0;
  20.   uint32_t color = strip.Color(255, 0, 0);
  21.   while (a < 256)
  22.   {
  23.     for (i = 0; i < 255; i++)
  24.     {
  25.       if (i == a) strip.setPixelColor(i, color);
  26.       else strip.setPixelColor(i, 0);
  27.     }
  28.     strip.show();
  29.     delay(2);
  30.     a++;
  31.   }
  32. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 10:21:49

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 10:34:00

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之二:全屏LED呼吸灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之二:全屏LED呼吸灯
  5. */
  6. #include <FastLED.h>
  7. #define LED_PIN     6     
  8. #define NUM_LEDS    256   
  9. CRGB leds[NUM_LEDS];
  10. void setup() {
  11.   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  12.   FastLED.setBrightness(20);  
  13. }
  14. int h = 0;
  15. void loop() {
  16.   for (int i = 0; i < NUM_LEDS; i++) {
  17.     leds[i] = CHSV( h, 255, 255);
  18.     FastLED.show();
  19.   }
  20.   delay(1);
  21.   h = (h + 3) % 255;
  22. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 10:35:46

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 11:44:57

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之三:简单的三色流水灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之三:简单的三色流水灯
  5. */
  6. #include <FastLED.h>
  7. #define LED_PIN    6
  8. #define NUM_LEDS   256
  9. CRGB leds[NUM_LEDS];
  10. void setup() {
  11.   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  12.   FastLED.setBrightness(250);  
  13. }
  14. void loop() {
  15.   for (int i = 0; i < NUM_LEDS; i++) {
  16.     leds[i] = CRGB::Red;
  17.     FastLED.show();
  18.     delay(1);
  19.     leds[i] = CRGB::Black;
  20.   }
  21.     for (int i = NUM_LEDS; i > 0; i--) {
  22.     leds[i] = CRGB::Blue;
  23.     FastLED.show();
  24.     delay(1);
  25.     leds[i] = CRGB::Black;
  26.   }
  27.     for (int i = 0; i < NUM_LEDS; i++) {
  28.     leds[i] = CRGB::Green;
  29.     FastLED.show();
  30.     delay(1);
  31.     leds[i] = CRGB::Black;
  32.   }
  33.       for (int i = NUM_LEDS; i > 0; i--) {
  34.     leds[i] = CRGB::Blue;
  35.     FastLED.show();
  36.     delay(1);
  37.     leds[i] = CRGB::Black;
  38.   }
  39. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 11:56:00

实验场景图  动态图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 13:47:02

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之四:循环快扫红绿蓝色LED满屏流水彩虹灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之四:循环快扫红绿蓝色LED满屏流水彩虹灯
  5. */
  6. #include <Adafruit_NeoPixel.h>
  7. #define PIN 6
  8. #define BRIGHTNESS 256
  9. Adafruit_NeoPixel strip = Adafruit_NeoPixel(256, PIN, NEO_GRB + NEO_KHZ800);
  10. void setup() {
  11.   strip.setBrightness(30);
  12.   strip.begin();
  13.   strip.show();
  14. }
  15. void loop() {
  16.   colorWipe(strip.Color(150, 0, 0), 50); // Red
  17.   colorWipe(strip.Color(0, 150, 0), 50); // Green
  18.   colorWipe(strip.Color(0, 0, 150), 50); // Blue
  19.   colorWipe(strip.Color(150, 150, 150), 50); // BlueWite
  20.   rainbowCycle(1);
  21. }
  22. void colorWipe(uint32_t c, uint8_t wait) {
  23.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  24.     strip.setPixelColor(i, c);
  25.     strip.show();
  26.     delay(3);
  27.   }
  28. }
  29. void rainbow(uint8_t wait) {
  30.   uint16_t i, j;
  31.   for (j = 0; j < 256; j++) {
  32.     for (i = 0; i < strip.numPixels(); i++) {
  33.       strip.setPixelColor(i, Wheel((i + j) & 255 ));
  34.     }
  35.     strip.show();
  36.     delay(3);
  37.   }
  38. }
  39. void rainbowCycle(uint8_t wait) {
  40.   uint16_t i, j;
  41.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  42.     for (i = 0; i < strip.numPixels(); i++) {
  43.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  44.     }
  45.     strip.show();
  46.     delay(3);
  47.   }
  48. }
  49. uint32_t Wheel(byte WheelPos) {
  50.   if (WheelPos < 85) {
  51.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  52.   } else if (WheelPos < 170) {
  53.     WheelPos -= 85;
  54.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  55.   } else {
  56.     WheelPos -= 170;
  57.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  58.   }
  59. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 13:48:46

实验场景图  动态图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 13:50:59

实验场景图  动态图2

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 15:03:37

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之五:255位循环流水变幻呼吸灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之五:255位循环流水变幻呼吸灯
  5. */
  6. // NeoPixel test program showing use of the WHITE channel for RGBW
  7. // pixels only (won't look correct on regular RGB NeoPixel strips).
  8. #include <Adafruit_NeoPixel.h>
  9. #ifdef __AVR__
  10. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  11. #endif
  12. // Which pin on the Arduino is connected to the NeoPixels?
  13. // On a Trinket or Gemma we suggest changing this to 1:
  14. #define LED_PIN     6
  15. // How many NeoPixels are attached to the Arduino?
  16. #define LED_COUNT  255
  17. // NeoPixel brightness, 0 (min) to 255 (max)
  18. #define BRIGHTNESS 50
  19. // Declare our NeoPixel strip object:
  20. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
  21. // Argument 1 = Number of pixels in NeoPixel strip
  22. // Argument 2 = Arduino pin number (most are valid)
  23. // Argument 3 = Pixel type flags, add together as needed:
  24. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  25. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  26. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  27. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  28. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  29. void setup() {
  30.   // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  31.   // Any other board, you can remove this part (but no harm leaving it):
  32. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  33.   clock_prescale_set(clock_div_1);
  34. #endif
  35.   // END of Trinket-specific code.
  36.   strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  37.   strip.show();            // Turn OFF all pixels ASAP
  38.   strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  39. }
  40. void loop() {
  41.   // Fill along the length of the strip in various colors...
  42.   colorWipe(strip.Color(255,   0,   0)     , 50); // Red
  43.   colorWipe(strip.Color(  0, 255,   0)     , 50); // Green
  44.   colorWipe(strip.Color(  0,   0, 255)     , 50); // Blue
  45.   colorWipe(strip.Color(  0,   0,   0, 255), 50); // True white (not RGB white)
  46.   whiteOverRainbow(75, 5);
  47.   pulseWhite(5);
  48.   rainbowFade2White(3, 3, 1);
  49. }
  50. // Fill strip pixels one after another with a color. Strip is NOT cleared
  51. // first; anything there will be covered pixel by pixel. Pass in color
  52. // (as a single 'packed' 32-bit value, which you can get by calling
  53. // strip.Color(red, green, blue) as shown in the loop() function above),
  54. // and a delay time (in milliseconds) between pixels.
  55. void colorWipe(uint32_t color, int wait) {
  56.   for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  57.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  58.     strip.show();                          //  Update strip to match
  59.     delay(4);                           //  Pause for a moment
  60.   }
  61. }
  62. void whiteOverRainbow(int whiteSpeed, int whiteLength) {
  63.   if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
  64.   int      head          = whiteLength - 1;
  65.   int      tail          = 0;
  66.   int      loops         = 3;
  67.   int      loopNum       = 0;
  68.   uint32_t lastTime      = millis();
  69.   uint32_t firstPixelHue = 0;
  70.   for(;;) { // Repeat forever (or until a 'break' or 'return')
  71.     for(int i=0; i<strip.numPixels(); i++) {  // For each pixel in strip...
  72.       if(((i >= tail) && (i <= head)) ||      //  If between head & tail...
  73.          ((tail > head) && ((i >= tail) || (i <= head)))) {
  74.         strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
  75.       } else {                                             // else set rainbow
  76.         int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  77.         strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  78.       }
  79.     }
  80.     strip.show(); // Update strip with new contents
  81.     // There's no delay here, it just runs full-tilt until the timer and
  82.     // counter combination below runs out.
  83.     firstPixelHue += 40; // Advance just a little along the color wheel
  84.     if((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
  85.       if(++head >= strip.numPixels()) {      // Advance head, wrap around
  86.         head = 0;
  87.         if(++loopNum >= loops) return;
  88.       }
  89.       if(++tail >= strip.numPixels()) {      // Advance tail, wrap around
  90.         tail = 0;
  91.       }
  92.       lastTime = millis();                   // Save time of last movement
  93.     }
  94.   }
  95. }
  96. void pulseWhite(uint8_t wait) {
  97.   for(int j=0; j<256; j++) { // Ramp up from 0 to 255
  98.     // Fill entire strip with white at gamma-corrected brightness level 'j':
  99.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  100.     strip.show();
  101.     delay(4);
  102.   }
  103.   for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
  104.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  105.     strip.show();
  106.     delay(4);
  107.   }
  108. }
  109. void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {
  110.   int fadeVal=0, fadeMax=100;
  111.   // Hue of first pixel runs 'rainbowLoops' complete loops through the color
  112.   // wheel. Color wheel has a range of 65536 but it's OK if we roll over, so
  113.   // just count from 0 to rainbowLoops*65536, using steps of 256 so we
  114.   // advance around the wheel at a decent clip.
  115.   for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536;
  116.     firstPixelHue += 256) {
  117.     for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  118.       // Offset pixel hue by an amount to make one full revolution of the
  119.       // color wheel (range of 65536) along the length of the strip
  120.       // (strip.numPixels() steps):
  121.       uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  122.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  123.       // optionally add saturation and value (brightness) (each 0 to 255).
  124.       // Here we're using just the three-argument variant, though the
  125.       // second value (saturation) is a constant 255.
  126.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
  127.         255 * fadeVal / fadeMax)));
  128.     }
  129.     strip.show();
  130.     delay(4);
  131.     if(firstPixelHue < 65536) {                              // First loop,
  132.       if(fadeVal < fadeMax) fadeVal++;                       // fade in
  133.     } else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,
  134.       if(fadeVal > 0) fadeVal--;                             // fade out
  135.     } else {
  136.       fadeVal = fadeMax; // Interim loop, make sure fade is at max
  137.     }
  138.   }
  139.   for(int k=0; k<whiteLoops; k++) {
  140.     for(int j=0; j<256; j++) { // Ramp up 0 to 255
  141.       // Fill entire strip with white at gamma-corrected brightness level 'j':
  142.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  143.       strip.show();
  144.     }
  145.     delay(20); // Pause 1 second
  146.     for(int j=255; j>=0; j--) { // Ramp down 255 to 0
  147.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  148.       strip.show();
  149.     }
  150.   }
  151.   delay(10); // Pause 1/2 second
  152. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 15:08:34

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 15:10:32

实验场景图 2

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 15:26:34

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之六:多彩流水灯变幻彩虹灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之六:多彩流水灯变幻彩虹灯
  5. */
  6. #include <Adafruit_NeoPixel.h>    //needed for the WS2812
  7. #include <avr/pgmspace.h>         //needed for PROGMEM
  8. #define PIN 6                    //Pin 1 is DATA In on the bottom Ring
  9. #define BRIGHTNESS 255             // brightness reduced
  10. //Lookup for the Candle light
  11. const unsigned int candles[] PROGMEM =
  12. {
  13.   15, 10, 48, 45, 36, 19, 59, 29, 5, 43, 41, 39, 24, 3, 61
  14. };
  15. Adafruit_NeoPixel strip = Adafruit_NeoPixel(255, PIN, NEO_GRB + NEO_KHZ800);
  16. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  17. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  18. // and minimize distance between Arduino and first pixel.  Avoid connecting
  19. // on a live circuit...if you must, connect GND first.
  20. void setup() {
  21.   pinMode(PIN, OUTPUT);
  22.   strip.begin();
  23.   strip.setBrightness(BRIGHTNESS); // set brightness
  24.   strip.show(); // Initialize all pixels to 'off'
  25. }
  26. void loop() {
  27.   tree();
  28.   delay(100);
  29.   colorcrazy();
  30.   theaterChaseRainbow(50);
  31.   comet();
  32.   warpdrive();
  33.   warpdrive();
  34.   rainbowCycle(1);
  35.   rainbow(5);
  36.   rainbow(5);
  37.   rainbow(5);
  38.   colorWipe(strip.Color(255, 0, 0), 50); // Red
  39.   colorWipe(strip.Color(0, 255, 0), 50); // Green
  40.   colorWipe(strip.Color(0, 0, 255), 50); // Blue
  41.   //
  42.   //
  43.   //  cometr();
  44.   //Tree light:
  45.   //
  46.   //  warpdrive();
  47.   //
  48.   //
  49.   //  comet();
  50.   /*
  51.     // Some example procedures showing how to display to the pixels:
  52.     colorWipe(strip.Color(255, 0, 0), 50); // Red
  53.     colorWipe(strip.Color(0, 255, 0), 50); // Green
  54.     colorWipe(strip.Color(0, 0, 255), 50); // Blue
  55.     // Send a theater pixel chase in...
  56.     theaterChase(strip.Color(127, 127, 127), 50); // White
  57.     theaterChase(strip.Color(127,   0,   0), 50); // Red
  58.     theaterChase(strip.Color(  0,   0, 127), 50); // Blue
  59.     rainbow(20);
  60.     rainbowCycle(20);
  61.     theaterChaseRainbow(50);
  62.   */
  63. }
  64. //Sub-----------------------------------------------------------------------
  65. //Comet
  66. void comet() {
  67.   for (uint16_t i = strip.numPixels(); i > 0; i--) {
  68.     strip.setPixelColor(i, strip.Color(0, 0, 255));
  69.     fadethemall(10);
  70.     fadethemall(10);
  71.   }
  72. }
  73. void cometr() {
  74.   for (uint16_t i = strip.numPixels(); i > 0; i--) {
  75.     strip.setPixelColor(i, strip.Color(255, 0, 0));
  76.     fadethemall(10);
  77.     fadethemall(10);
  78.   }
  79. }
  80. //From top down white pulses
  81. void warpdrive() {
  82.   //Top Led
  83.   strip.setPixelColor(60, strip.Color(255, 255, 255));
  84.   strip.show();
  85.   //fade a bit
  86.   for (int i = 0; i < 20; i++)
  87.   {
  88.     fadethemall(20);
  89.   }
  90.   //8 Ring
  91.   for (int i = 52; i < 60; i++)
  92.   {
  93.     strip.setPixelColor(i, strip.Color(255, 255, 255));
  94.   }
  95.   strip.show();
  96.   //fade a bit
  97.   for (int i = 0; i < 20; i++)
  98.   {
  99.     fadethemall(20);
  100.   }
  101.   //12 Ring
  102.   for (int i = 40; i < 52; i++)
  103.   {
  104.     strip.setPixelColor(i, strip.Color(255, 255, 255));
  105.   }
  106.   strip.show();
  107.   //fade a bit
  108.   for (int i = 0; i < 20; i++)
  109.   {
  110.     fadethemall(20);
  111.   }
  112.   //16 Ring
  113.   for (int i = 24; i < 40; i++)
  114.   {
  115.     strip.setPixelColor(i, strip.Color(255, 255, 255));
  116.   }
  117.   strip.show();
  118.   //fade a bit
  119.   for (int i = 0; i < 20; i++)
  120.   {
  121.     fadethemall(20);
  122.   }
  123.   //24 Ring
  124.   for (int i = 0; i < 24; i++)
  125.   {
  126.     strip.setPixelColor(i, strip.Color(255, 255, 255));
  127.   }
  128.   strip.show();
  129.   //fade a bit
  130.   for (int i = 0; i < 20; i++)
  131.   {
  132.     fadethemall(20);
  133.   }
  134.   //Extra by John Kerr
  135.   strip.setPixelColor(60, strip.Color(0, 0, 0));
  136.   strip.show();
  137.   //fade a bit
  138.   for (int i = 0; i < 20; i++)
  139.   {
  140.     fadethemall(20);
  141.   }
  142. }
  143. //This reduces the brightness of all leds
  144. void fadethemall(uint8_t wait) {
  145.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  146.     uint32_t color = strip.getPixelColor(i);
  147.     int r;
  148.     int g;
  149.     int b;
  150.     r = (uint8_t)(color >> 16);
  151.     g = (uint8_t)(color >>  8);
  152.     b = (uint8_t)color;
  153.     if (r > 0)
  154.     {
  155.       r = r - 1;
  156.     }
  157.     else
  158.     {
  159.       r = 0;
  160.     }
  161.     if (g > 0)
  162.     {
  163.       g = g - 1;
  164.     }
  165.     else
  166.     {
  167.       g = 0;
  168.     }
  169.     if (b > 0)
  170.     {
  171.       b = b - 1;
  172.     }
  173.     else
  174.     {
  175.       b = 0;
  176.     }
  177.     strip.setPixelColor(i, strip.Color(r, g, b));
  178.   }
  179.   strip.show();
  180.   delay(20);
  181. }
  182. //This drives the WS2812 in a crazy pattern, fun!
  183. void colorcrazy() {
  184.   colorWipe(strip.Color(255, 0, 0), 25); // Red
  185.   colorWipe(strip.Color(0, 255, 0), 25); // Green
  186.   colorWipe(strip.Color(0, 0, 255), 25); // Blue
  187.   theaterChaseRainbow(5);
  188. }
  189. //This lights up the tree in green, then add the white "candles"
  190. void tree() {
  191.   colorWipe(strip.Color(0, 50, 0), 50); // Green
  192.   //light "candles"
  193.   //Show the S:
  194.   for (int i = 0; i < 16; i++)
  195.   {
  196.     strip.setPixelColor(pgm_read_word(&candles) - 1, strip.Color(255, 255, 255));
  197.     strip.show();
  198.     delay(20);
  199.   }
  200. }
  201. // Fill the dots one after the other with a color
  202. void colorWipe(uint32_t c, uint8_t wait) {
  203.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  204.     strip.setPixelColor(i, c);
  205.     strip.show();
  206.     delay(20);
  207.   }
  208. }
  209. void rainbow(uint8_t wait) {
  210.   uint16_t i, j;
  211.   for (j = 0; j < 256; j++) {
  212.     for (i = 0; i < strip.numPixels(); i++) {
  213.       strip.setPixelColor(i, Wheel((i + j) & 255));
  214.     }
  215.     strip.show();
  216.     delay(20);
  217.   }
  218. }
  219. // Slightly different, this makes the rainbow equally distributed throughout
  220. void rainbowCycle(uint8_t wait) {
  221.   uint16_t i, j;
  222.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  223.     for (i = 0; i < strip.numPixels(); i++) {
  224.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  225.     }
  226.     strip.show();
  227.     delay(20);
  228.   }
  229. }
  230. //Theatre-style crawling lights.
  231. void theaterChase(uint32_t c, uint8_t wait) {
  232.   for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
  233.     for (int q = 0; q < 3; q++) {
  234.       for (int i = 0; i < strip.numPixels(); i = i + 3) {
  235.         strip.setPixelColor(i + q, c);  //turn every third pixel on
  236.       }
  237.       strip.show();
  238.       delay(20);
  239.       for (int i = 0; i < strip.numPixels(); i = i + 3) {
  240.         strip.setPixelColor(i + q, 0);      //turn every third pixel off
  241.       }
  242.     }
  243.   }
  244. }
  245. //Theatre-style crawling lights with rainbow effect
  246. void theaterChaseRainbow(uint8_t wait) {
  247.   for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheel
  248.     for (int q = 0; q < 3; q++) {
  249.       for (int i = 0; i < strip.numPixels(); i = i + 3) {
  250.         strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
  251.       }
  252.       strip.show();
  253.       delay(20);
  254.       for (int i = 0; i < strip.numPixels(); i = i + 3) {
  255.         strip.setPixelColor(i + q, 0);      //turn every third pixel off
  256.       }
  257.     }
  258.   }
  259. }
  260. // Input a value 0 to 255 to get a color value.
  261. // The colours are a transition r - g - b - back to r.
  262. uint32_t Wheel(byte WheelPos) {
  263.   WheelPos = 255 - WheelPos;
  264.   if (WheelPos < 85) {
  265.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  266.   } else if (WheelPos < 170) {
  267.     WheelPos -= 85;
  268.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  269.   } else {
  270.     WheelPos -= 170;
  271.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  272.   }
  273. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 15:28:05

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 15:46:19

实验开源图形编程(Mind+、编玩边学)

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

1234下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail