回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

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

头像 驴友花雕 2023.07.13 71 1

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

 

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

 

0.jpg
 

通过将 LED 串分成段(最多 10 个)并独立编程每个段,可以创建更复杂的效果。使用setSegment()函数对每个段的模式、颜色、速度和方向(正常或反向)进行编程:

 

setSegment(segment index, start LED, stop LED, mode, color, speed, reverse);

 

请注意,某些效果使用不止一种颜色(最多三种),并通过指定颜色数组进行编程:

 

setSegment(segment index, start LED, stop LED, mode, colors[], speed, reverse);


//将 LED 串分成两个独立的部分

uint32_t colours[] = {RED, GREEN};
ws2812fx.setSegment( 0 , 0 , (LED_COUNT/ 2 )- 1 , FX_MODE_BLINK, colors, 1000 , false );
ws2812fx.setSegment( 1 , LED_COUNT/ 2 , LED_COUNT- 1 , FX_MODE_BLINK, COLORS(ORANGE, PURPLE), 1000 , false );

 

 

Arduino实验场景图
 

32.jpg

内置效果清单
静态- 不闪烁。只是普通的旧静态灯。
闪烁- 正常闪烁。50% 开/关时间。
呼吸- 众所周知的 i-Devices 的“待机呼吸”。固定速度。
颜色擦除- 依次点亮所有 LED。然后按顺序关闭它们。重复。
颜色擦除反转 - 与 Color Wipe 相同,但交换开/关颜色。
颜色擦除反向 - 依次点亮所有 LED。然后以相反的顺序关闭它们。重复。
颜色擦除反向反向 - 与 上条相同,除了交换开/关颜色。
随机颜色擦除- 将所有 LED 依次变为随机颜色。然后用另一种颜色重新开始。
随机颜色- 以一种随机颜色点亮所有 LED。然后将它们切换到下一个随机颜色。
单动态- 以随机颜色点亮每个 LED。将一个随机的 LED 一个接一个地更改为随机颜色。
多动态- 以随机颜色点亮每个 LED。同时将所有 LED 更改为新的随机颜色。
彩虹- 通过彩虹一次循环所有 LED。
彩虹循环- 在整个 LED 串上循环彩虹。
扫描- 来回运行单个像素。
双扫描- 以相反的方向来回运行两个像素。
淡入淡出- 使 LED 灯再次亮起和(几乎)熄灭。
剧院追逐 - 剧院式爬行灯。受 Adafruit 示例的启发。
剧院追逐彩虹- 具有彩虹效果的剧院式爬行灯。受 Adafruit 示例的启发。
行车灯- 带平滑正弦过渡的行车灯效果。
闪烁- 使多个 LED 闪烁、重置、重复。
闪烁随机 - 以随机颜色闪烁几个 LED,重置,重复。
闪烁淡入淡出 - 闪烁几个 LED,然后逐渐消失。
19 / 5000
翻译结果
闪烁淡入淡出随机  - 以随机颜色闪烁几个 LED,然后逐渐消失。
闪烁- 一次闪烁一个 LED。
Flash Sparkle - 以所选颜色点亮所有 LED。随机闪烁单个白色像素。
Hyper Sparkle - 像闪光一样。更多的闪光。
频闪- 经典频闪效果。
频闪彩虹- 经典频闪效果。骑自行车穿过彩虹。
Multi Strobe - 具有不同频闪次数和暂停的频闪效果,由速度设置控制。
闪烁彩虹- 经典闪烁效果。骑自行车穿过彩虹。
Chase White - 在白色上运行的颜色。
Chase Color - 白色在颜色上运行。
Chase Random - 白色运行,然后是随机颜色。
Chase Rainbow - 白色在彩虹上奔跑。
Chase Flash - 白色闪烁彩色。
Chase Flash Random - 白色闪烁,然后是随机颜色。
Chase Rainbow White - 运行在白色的彩虹。
Chase Blackout - 黑色在彩色上运行。
Chase Blackout Rainbow - 黑色在彩虹上奔跑。
随机颜色扫描- 从条带的开始和结束交替引入随机颜色。
运行颜色- 交替运行的颜色/白色像素。
Running Red Blue - 交替运行红色/蓝色像素。
随机运行- 随机彩色像素运行。
Larson 扫描仪- KITT
Comet - 从一端发射彗星。
烟花- 烟花火花。
Fireworks Random - 随机彩色烟花火花。
圣诞快乐- 交替运行绿色/红色像素。
火焰闪烁- 火焰闪烁效果。就像在狂风中。
Fire Flicker (soft) - 火焰闪烁效果。跑得更慢/更软。
Fire Flicker (intense) - 火焰闪烁效果。颜色范围更广。
Circus Combustus - 交替运行的白色/红色/黑色像素。
万圣节- 交替运行橙色/紫色像素。
双色追逐- 两个 LED 在背景色上运行。
三色追逐- 交替运行三个彩色像素。
TwinkleFOX - 灯光随机淡入淡出。
通过 63.自定义- 最多八个用户创建的自定义效果。


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

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

 项目三十六:使用Adafruit_NeoPixel库的串口控制20种效果

 实验开源代码
 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验一百四十六:64位WS2812B 8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目三十六:使用Adafruit_NeoPixel库的串口控制20种效果
*/

#include <Adafruit_NeoPixel.h>
#define PIN 6 //LED'in Din pinini yazın
#define NUM_LEDS 64 //Kaç tane LED'iniz varsa buraya yazın
#define BRIGHTNESS 20

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
int mod;
int lastmod;
String veri;
int randommod;
int parlaklik = 0;

void setup() {
  Serial.begin(9600);
  veri.reserve(5);
  strip.begin();
  strip.show();
  strip.setBrightness(BRIGHTNESS); //亮度范围0-255
  Serial.println("WS2812准备就绪");
  Serial.println("串口输入1-21");
}

void loop() {
  switch (mod) {
    case 1:
      Serial.println("RGBLoop");
      RGBLoop();
      break;
    case 2:
      Serial.println("Strobe");
      Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
      break;
    case 3:
      Serial.println("HalloweenEyes");
      HalloweenEyes(0xff, 0x00, 0x00, 1, 4, true, random(5, 50), random(10, 50), random(50, 300));
      break;
    case 4:
      Serial.println("NewKITT RightToLeft");
      NewKITT(0xff, 0, 0, 8, 10, 50);
      break;
    case 5:
      Serial.println("Twinkle");
      Twinkle(0xff, 0, 0, 10, 100, false);
      break;
    case 6:
      Serial.println("TwinkleRandom");
      Twinkle(0xff, 0, 0, 10, 100, false);
      break;
    case 7:
      Serial.println("Sparkle");
      Sparkle(0xff, 0xff, 0xff, 0);
      break;
    case 8:
      Serial.println("SnowSparkle");
      SnowSparkle(0x10, 0x10, 0x10, 20, random(100, 1000));
      break;
    case 9:
      Serial.println("RunningLights");
      RunningLights(0xff, 0xff, 0x00, 50);
      break;
    case 10:
      Serial.println("colorWipe");
      colorWipe(0x00, 0xff, 0x00, 50);
      colorWipe(0xff, 0x00, 0x00, 50);
      break;
    case 11:
      Serial.println("rainbowCycle");
      rainbowCycle(20);
      break;
    case 12:
      Serial.println("theaterChase");
      theaterChase(0xff, 0, 0, 50);
      break;
    case 13:
      Serial.println("theaterChaseRainbow");
      theaterChaseRainbow(50);
      break;
    case 14:
      Serial.println("Fire");
      Fire(55, 120, 15);
      break;
    case 15:
      Serial.println("BouncingBalls");
      meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
      break;
    case 16:
      Serial.println("meteorRain");
      meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
      break;
    case 17:
      Serial.println("Red");
      setAll(255, 0, 0);
      break;
    case 18:
      Serial.println("Green");
      setAll(0, 255, 0);
      break;
    case 19:
      Serial.println("Blue");
      setAll(0, 0, 255);
      break;
    case 20:
      Serial.println("ON");
      randommod = random(1, 19);
      switch (randommod) {
        case 1:
          Serial.println("RGBLoop");
          RGBLoop();
          break;
        case 2:
          Serial.println("Strobe");
          Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
          break;
        case 3:
          Serial.println("HalloweenEyes");
          HalloweenEyes(0xff, 0x00, 0x00, 1, 4, true, random(5, 50), random(10, 50), random(50, 300));
          break;
        case 4:
          Serial.println("NewKITT RightToLeft");
          NewKITT(0xff, 0, 0, 8, 10, 50);
          break;
        case 5:
          Serial.println("Twinkle");
          Twinkle(0xff, 0, 0, 10, 100, false);
          break;
        case 6:
          Serial.println("TwinkleRandom");
          Twinkle(0xff, 0, 0, 10, 100, false);
          break;
        case 7:
          Serial.println("Sparkle");
          Sparkle(0xff, 0xff, 0xff, 0);
          break;
        case 8:
          Serial.println("SnowSparkle");
          SnowSparkle(0x10, 0x10, 0x10, 20, random(100, 1000));
          break;
        case 9:
          Serial.println("RunningLights");
          RunningLights(0xff, 0xff, 0x00, 50);
          break;
        case 10:
          Serial.println("colorWipe");
          colorWipe(0x00, 0xff, 0x00, 50);
          colorWipe(0xff, 0x00, 0x00, 50);
          break;
        case 11:
          Serial.println("rainbowCycle");
          rainbowCycle(20);
          break;
        case 12:
          Serial.println("theaterChase");
          theaterChase(0xff, 0, 0, 50);
          break;
        case 13:
          Serial.println("theaterChaseRainbow");
          theaterChaseRainbow(50);
          break;
        case 14:
          Serial.println("Fire");
          Fire(55, 120, 15);
          break;
        case 15:
          Serial.println("BouncingBalls");
          meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
          break;
        case 16:
          Serial.println("meteorRain");
          meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
          break;
        case 17:
          Serial.println("Red");
          setAll(255, 0, 0);
          break;
        case 18:
          Serial.println("Green");
          setAll(0, 255, 0);
          break;
        case 19:
          Serial.println("Blue");
          setAll(0, 0, 255);
          break;
      }
      break;
    case 21:
      Serial.println("OFF");
      setAll(0, 0, 0);
      break;
    default:
      //Serial.println("Red");
      setAll(255, 0, 0);
      break;
  }
}
void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
  setAll(0, 0, 0);

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


    // fade brightness all LEDs one step
    for (int j = 0; j < NUM_LEDS; j++) {
      if ( (!meteorRandomDecay) || (random(10) > 5) ) {
        fadeToBlack(j, meteorTrailDecay );
      }
      if (serialEvent() != false) break;
    }

    // draw meteor
    for (int j = 0; j < meteorSize; j++) {
      if ( ( i - j < NUM_LEDS) && (i - j >= 0) ) {
        setPixel(i - j, red, green, blue);
      }
      if (serialEvent() != false) break;
    }

    showStrip();
    delay(SpeedDelay);
    if (serialEvent() != false) break;
  }
}

void fadeToBlack(int ledNo, byte fadeValue) {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  uint32_t oldColor;
  uint8_t r, g, b;
  int value;

  oldColor = strip.getPixelColor(ledNo);
  r = (oldColor & 0x00ff0000UL) >> 16;
  g = (oldColor & 0x0000ff00UL) >> 8;
  b = (oldColor & 0x000000ffUL);

  r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 256);
  g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 256);
  b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 256);

  strip.setPixelColor(ledNo, r, g, b);
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[ledNo].fadeToBlackBy( fadeValue );
#endif
}
void BouncingBalls(byte red, byte green, byte blue, int BallCount) {
  float Gravity = -9.81;
  int StartHeight = 1;

  float Height[BallCount];
  float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
  float ImpactVelocity[BallCount];
  float TimeSinceLastBounce[BallCount];
  int   Position[BallCount];
  long  ClockTimeSinceLastBounce[BallCount];
  float Dampening[BallCount];

  for (int i = 0 ; i < BallCount ; i++) {
    ClockTimeSinceLastBounce[i] = millis();
    Height[i] = StartHeight;
    Position[i] = 0;
    ImpactVelocity[i] = ImpactVelocityStart;
    TimeSinceLastBounce[i] = 0;
    Dampening[i] = 0.90 - float(i) / pow(BallCount, 2);
    if (serialEvent() != false) break;
  }

  while (true) {
    for (int i = 0 ; i < BallCount ; i++) {
      TimeSinceLastBounce[i] =  millis() - ClockTimeSinceLastBounce[i];
      Height[i] = 0.5 * Gravity * pow( TimeSinceLastBounce[i] / 1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i] / 1000;

      if ( Height[i] < 0 ) {
        Height[i] = 0;
        ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
        ClockTimeSinceLastBounce[i] = millis();

        if ( ImpactVelocity[i] < 0.01 ) {
          ImpactVelocity[i] = ImpactVelocityStart;
        }
      }
      Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
      if (serialEvent() != false) break;
    }

    for (int i = 0 ; i < BallCount ; i++) {
      setPixel(Position[i], red, green, blue);
      if (serialEvent() != false) break;
    }

    showStrip();
    setAll(0, 0, 0);
    if (serialEvent() != false) break;
  }
}
void Fire(int Cooling, int Sparking, int SpeedDelay) {
  static byte heat[NUM_LEDS];
  int cooldown;
  for ( int i = 0; i < NUM_LEDS; i++) {
    cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);

    if (cooldown > heat[i]) {
      heat[i] = 0;
    } else {
      heat[i] = heat[i] - cooldown;
    }
    if (serialEvent() != false) break;
  }
  for ( int k = NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
    if (serialEvent() != false) break;
  }
  if ( random(255) < Sparking ) {
    int y = random(7);
    heat[y] = heat[y] + random(160, 255);
  }

  for ( int j = 0; j < NUM_LEDS; j++) {
    setPixelHeatColor(j, heat[j] );
    if (serialEvent() != false) break;
  }

  showStrip();
  delay(SpeedDelay);
}

void setPixelHeatColor (int Pixel, byte temperature) {
  byte t192 = round((temperature / 255.0) * 191);

  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252
  if ( t192 > 0x80) {                    // hottest
    setPixel(Pixel, 255, 255, heatramp);
  } else if ( t192 > 0x40 ) {            // middle
    setPixel(Pixel, 255, heatramp, 0);
  } else {                               // coolest
    setPixel(Pixel, heatramp, 0, 0);
  }
}
void theaterChaseRainbow(int SpeedDelay) {
  byte *c;

  for (int j = 0; j < 256; j++) {
    for (int q = 0; q < 3; q++) {
      for (int i = 0; i < NUM_LEDS; i = i + 3) {
        c = Wheel( (i + j) % 255);
        setPixel(i + q, *c, *(c + 1), *(c + 2));
        if (serialEvent() != false) break;
      }
      showStrip();

      delay(SpeedDelay);

      for (int i = 0; i < NUM_LEDS; i = i + 3) {
        setPixel(i + q, 0, 0, 0);
        if (serialEvent() != false) break;
      }
      if (serialEvent() != false) break;
    }
    if (serialEvent() != false) break;
  }
}
void theaterChase(byte red, byte green, byte blue, int SpeedDelay) {
  for (int j = 0; j < 10; j++) {
    for (int q = 0; q < 3; q++) {
      for (int i = 0; i < NUM_LEDS; i = i + 3) {
        setPixel(i + q, red, green, blue);
        if (serialEvent() != false) break;
      }
      showStrip();

      delay(SpeedDelay);

      for (int i = 0; i < NUM_LEDS; i = i + 3) {
        setPixel(i + q, 0, 0, 0);
        if (serialEvent() != false) break;
      }
      if (serialEvent() != false) break;
    }
    if (serialEvent() != false) break;
  }
}
void rainbowCycle(int SpeedDelay) {
  byte *c;
  uint16_t i, j;

  for (j = 0; j < 256 * 5; j++) {
    for (i = 0; i < NUM_LEDS; i++) {
      c = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
      setPixel(i, *c, *(c + 1), *(c + 2));
      if (serialEvent() != false) break;
    }
    showStrip();
    delay(SpeedDelay);
    if (serialEvent() != false) break;
  }
}

byte * Wheel(byte WheelPos) {
  static byte c[3];

  if (WheelPos < 85) {
    c[0] = WheelPos * 3;
    c[1] = 255 - WheelPos * 3;
    c[2] = 0;
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    c[0] = 255 - WheelPos * 3;
    c[1] = 0;
    c[2] = WheelPos * 3;
  } else {
    WheelPos -= 170;
    c[0] = 0;
    c[1] = WheelPos * 3;
    c[2] = 255 - WheelPos * 3;
  }

  return c;
}
void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for (uint16_t i = 0; i < NUM_LEDS; i++) {
    setPixel(i, red, green, blue);
    showStrip();
    delay(SpeedDelay);
    if (serialEvent() != false) break;
  }
}
void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
  int Position = 0;

  for (int j = 0; j < NUM_LEDS * 2; j++)
  {
    Position++; // = 0; //Position + Rate;
    for (int i = 0; i < NUM_LEDS; i++) {
      // sine wave, 3 offset waves make a rainbow!
      //float level = sin(i+Position) * 127 + 128;
      //setPixel(i,level,0,0);
      //float level = sin(i+Position) * 127 + 128;
      setPixel(i, ((sin(i + Position) * 127 + 128) / 255)*red,
               ((sin(i + Position) * 127 + 128) / 255)*green,
               ((sin(i + Position) * 127 + 128) / 255)*blue);
      if (serialEvent() != false) break;
    }

    showStrip();
    delay(WaveDelay);
    if (serialEvent() != false) break;
  }
}
void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {
  setAll(red, green, blue);
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel, 0xff, 0xff, 0xff);
  showStrip();
  delay(SparkleDelay);
  setPixel(Pixel, red, green, blue);
  showStrip();
  delay(SpeedDelay);
}
void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel, red, green, blue);
  showStrip();
  delay(SpeedDelay);
  setPixel(Pixel, 0, 0, 0);
}
void TwinkleRandom(int Count, int SpeedDelay, boolean OnlyOne) {
  setAll(0, 0, 0);

  for (int i = 0; i < Count; i++) {
    setPixel(random(NUM_LEDS), random(0, 255), random(0, 255), random(0, 255));
    showStrip();
    delay(SpeedDelay);
    if (OnlyOne) {
      setAll(0, 0, 0);
    }
    if (serialEvent() != false) break;
  }

  delay(SpeedDelay);
}
void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
  setAll(0, 0, 0);

  for (int i = 0; i < Count; i++) {
    setPixel(random(NUM_LEDS), red, green, blue);
    showStrip();
    delay(SpeedDelay);
    if (OnlyOne) {
      setAll(0, 0, 0);
    }
    if (serialEvent() != false) break;
  }

  delay(SpeedDelay);
}
void NewKITT(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  /*OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
    CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
    LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
    RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
    OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
    CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);*/
}

void CenterToOutside(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for (int i = ((NUM_LEDS - EyeSize) / 2); i >= 0; i--) {
    setAll(0, 0, 0);

    setPixel(i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      setPixel(i + j, red, green, blue);
      if (serialEvent() != false) break;
    }
    setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);

    setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      setPixel(NUM_LEDS - i - j, red, green, blue);
      if (serialEvent() != false) break;
    }
    setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);

    showStrip();
    delay(SpeedDelay);
    if (serialEvent() != false) break;
  }
  delay(ReturnDelay);
}

void OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for (int i = 0; i <= ((NUM_LEDS - EyeSize) / 2); i++) {
    setAll(0, 0, 0);

    setPixel(i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      setPixel(i + j, red, green, blue);
      if (serialEvent() != false) break;
    }
    setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);

    setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      setPixel(NUM_LEDS - i - j, red, green, blue);
      if (serialEvent() != false) break;
    }
    setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);

    showStrip();
    delay(SpeedDelay);
    if (serialEvent() != false) break;
  }
  delay(ReturnDelay);
}

void LeftToRight(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for (int i = 0; i < NUM_LEDS - EyeSize - 2; i++) {
    setAll(0, 0, 0);
    setPixel(i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      setPixel(i + j, red, green, blue);
      if (serialEvent() != false) break;;
    }
    setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
    showStrip();
    delay(SpeedDelay);
    if (serialEvent() != false) break;
  }
  delay(ReturnDelay);
}

void RightToLeft(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for (int i = NUM_LEDS - EyeSize - 2; i > 0; i--) {
    setAll(0, 0, 0);
    setPixel(i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      setPixel(i + j, red, green, blue);
      if (serialEvent() != false) break;
    }
    setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
    showStrip();
    delay(SpeedDelay);
    if (serialEvent() != false) break;
  }
  delay(ReturnDelay);
}
void HalloweenEyes(byte red, byte green, byte blue, int EyeWidth, int EyeSpace, boolean Fade, int Steps, int FadeDelay, int EndPause) {
  randomSeed(analogRead(0));

  int i;
  int StartPoint  = random( 0, NUM_LEDS - (2 * EyeWidth) - EyeSpace );
  int Start2ndEye = StartPoint + EyeWidth + EyeSpace;

  for (i = 0; i < EyeWidth; i++) {
    setPixel(StartPoint + i, red, green, blue);
    setPixel(Start2ndEye + i, red, green, blue);
    if (serialEvent() != false) break;
  }

  showStrip();

  if (Fade == true) {
    float r, g, b;

    for (int j = Steps; j >= 0; j--) {
      r = j * (red / Steps);
      g = j * (green / Steps);
      b = j * (blue / Steps);

      for (i = 0; i < EyeWidth; i++) {
        setPixel(StartPoint + i, r, g, b);
        setPixel(Start2ndEye + i, r, g, b);
        if (serialEvent() != false) break;
      }

      showStrip();
      delay(FadeDelay);
      if (serialEvent() != false) break;
    }
  }

  setAll(0, 0, 0); // Set all black

  delay(EndPause);
}
void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause) {
  for (int j = 0; j < StrobeCount; j++) {
    setAll(red, green, blue);
    showStrip();
    delay(FlashDelay);
    setAll(0, 0, 0);
    showStrip();
    delay(FlashDelay);
    if (serialEvent() != false) break;
  }

  delay(EndPause);
}
void RGBLoop() {
  for (int j = 0; j < 3; j++ ) {
    // Fade IN
    for (int k = 0; k < 256; k++) {
      switch (j) {
        case 0: setAll(k, 0, 0); if (serialEvent() != false) break; break;
        case 1: setAll(0, k, 0); if (serialEvent() != false) break; break;
        case 2: setAll(0, 0, k); if (serialEvent() != false) break; break;
      }
      showStrip();
      if (serialEvent() != false)break;
    }
    // Fade OUT
    for (int k = 255; k >= 0; k--) {
      switch (j) {
        case 0: setAll(k, 0, 0); if (serialEvent() != false) break; break;
        case 1: setAll(0, k, 0); if (serialEvent() != false) break; break;
        case 2: setAll(0, 0, k); if (serialEvent() != false) break; break;
      }
      showStrip();
      if (serialEvent() != false)break;
    }
    if (serialEvent() != false)break;
  }
}
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
  for (int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}
bool serialEvent() {
  if (Serial.available()) {
    veri = Serial.readString();
    if (veri.charAt(0) == 'b') {
      veri = veri.substring(1, 4);
      parlaklik = veri.toInt();
      parlaklik = map(parlaklik, 0, 100, 0, 255);
      strip.setBrightness(parlaklik);
    }
    else {
      mod = veri.toInt();
      return true;
    }
  }
  return false;
}

实验串口返回情况
 

33.jpg

串口输入20,随机显示不同效果
 

34.jpg

使用PC端控制软件,有二种控制WS2812B的方式
1、连接串口后,在底部窗口输入1-21,转换显示效果

2、点击16个效果按钮
 

35.jpg

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

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

  项目三十七:简单的眨眼效果

  实验开源代码


 

代码
/*

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

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

 项目三十七:简单的眨眼效果

*/

#include <WS2812.h>

WS2812 LED(64); // 64 LED

     

cRGB value;

void setup() {

    LED.setOutput(6); // Digital Pin 6

}

void loop() {

    value.b = 255; value.g = 0; value.r = 0; // RGB Value -> Blue

    LED.set_crgb_at(0, value); // Set value at LED found at index 0

    LED.sync(); // Sends the value to the LED

    delay(500); // Wait 500 ms

     

    value.b = 0; value.g = 0; value.r = 255; // RGB Value -> Red

    LED.set_crgb_at(0, value); // Set value at LED found at index 0

    LED.sync(); // Sends the value to the LED

    delay(500); // Wait 500 ms

}

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

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

 项目三十八:满屏的彩虹效果

  实验开源代码


 

代码
/*

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

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

 项目三十八:满屏的彩虹效果

*/

// use the cRGB struct hsv method

#define USE_HSV

#include <WS2812.h>

#define LEDCount 64

#define outputPin 6

WS2812 LED(LEDCount); 

cRGB value;

int h = 0;  //stores 0 to 614

byte steps = 15; //number of hues we skip in a 360 range per update

byte sat = 255;

byte val = 127;

long sleep = 100; //delays between update

  

void setup() {

 LED.setOutput(outputPin);

}

void loop() {

 Cycle();

  

 for(int i = 0; i < LEDCount; i++)

 {

   LED.set_crgb_at(i, value);

 }

 // Sends the data to the LEDs

 LED.sync();

  

 delay(sleep);

}

void Cycle()

{

 value.SetHSV(h, sat, val);

  

 h += steps;  

 if(h > 360)

 {

   h %= 360;

 }

}

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

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

 项目三十九:以 R、G、B 顺序淡化 LED,演示更改颜色顺序的功能

  实验开源代码


 

代码
/*

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

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

项目三十九:以 R、G、B 顺序淡化 LED,演示更改颜色顺序的功能

*/

#include <WS2812.h>

#define outputPin 6 // Digital output pin (default: 6)

#define LEDCount 64  // Number of LEDs to drive (default: 64)

WS2812 LED(LEDCount);

cRGB value;

byte intensity;

byte sign;

void setup() {

 LED.setOutput(outputPin); // Digital Pin 7

 /* You may uncomment one of the following three lines to switch

  to a different data transmission sequence for your addressable LEDs.

  (These functions can be used at any point in your program as needed.)  */

 //LED.setColorOrderRGB(); // Uncomment for RGB color order

 //LED.setColorOrderBRG(); // Uncomment for BRG color order

 LED.setColorOrderGRB(); // Uncomment for GRB color order (Default; will be used if none other is defined.)

 intensity = 0;

 sign = 1;

}

void loop() {

 int i = 0;

 if (sign) {

  intensity++;

  if (intensity == 255)

   sign = 0;

 }

 else {

  intensity--;

  if (intensity == 0)

   sign = 1;

 }

 while (i < LEDCount) {

  if ((i % 3) == 0) { // First LED, and every third after that

   value.b = 0;

   value.g = 0;

   value.r = intensity; // RGB Value -> Red Only

   LED.set_crgb_at(i, value); // Set value at LED found at index 0

  }

  else if ((i % 3) == 1) { // Second LED, and every third after that

   value.b = 0;

   value.g = intensity;

   value.r = 0; // RGB Value -> Green Only

   LED.set_crgb_at(i, value); // Set value at LED found at index 0

  }

  else { // Third LED, and every third after that

   value.b = intensity;

   value.g = 0;

   value.r = 0; // RGB Value -> Blue Only

   LED.set_crgb_at(i, value); // Set value at LED found at index 0

  }

  i++;

 }

 LED.sync(); // Sends the data to the LEDs

 delay(50); // Wait (ms)

}

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

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

  项目四十:应用Adafruit_NeoPixel库的入门极简程序

  实验开源代码


 

代码
/*

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

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

项目四十:应用Adafruit_NeoPixel库的入门极简程序

*/

#include <Adafruit_NeoPixel.h>

#define PIN    6 //接脚

#define NUMPIXELS 64 //数量

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 100 //延时

void setup() {

 pixels.setBrightness(22);//亮度

 pixels.begin();//启动

}

void loop() {

 pixels.clear();

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

  pixels.setPixelColor(i, pixels.Color(50, 250, 0));

  pixels.show();

  delay(DELAYVAL);

 }

}

评论

user-avatar
  • hacker_

    hacker_2023.08.16

    666

    0
    icon 他的勋章
      展开更多