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

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

头像 驴友花雕 2023.07.13 18 1

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

 

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

 

32.jpg

 

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

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

  项目四十一:六十四位音乐频谱灯十六位音乐反应动态频谱灯

  实验开源代码


 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码 +图形编程 +仿真编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十一:六十四位音乐频谱灯十六位音乐反应动态频谱灯
*/

#include "FastLED.h"

#define OCTAVE 1 //   // Group buckets into octaves  (use the log output function LOG_OUT 1)
#define OCT_NORM 0 // Don't normalise octave intensities by number of bins
#define FHT_N 256 // set to 256 point fht
#include <FHT.h> // include the library
//int noise[] = {204,188,68,73,150,98,88,68}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}


// int noise[] = {204,190,108,85,65,65,55,60}; // noise for mega adk
int noise[] = {204, 195, 100, 90, 85, 80, 75, 75}; // noise for NANO
//int noise[] = {204,198,100,85,85,80,80,80};
float noise_fact[] = {15, 7, 1.5, 1, 1.2, 1.4, 1.7, 3}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}
float noise_fact_adj[] = {15, 7, 1.5, 1, 1.2, 1.4, 1.7, 3}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}


#define LED_PIN     6
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB


// Params for width and height
const uint8_t kMatrixWidth = 8;
const uint8_t kMatrixHeight = 8;//----------was 27
//#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define NUM_LEDS    64

CRGB leds[NUM_LEDS];

int counter2 = 0;



void setup() {
  Serial.begin(9600);
  delay(1000);
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness (33);
  fill_solid(leds, NUM_LEDS, CRGB::Black);
  FastLED.show();
  // TIMSK0 = 0; // turn off timer0 for lower jitter
  ADCSRA = 0xe5; // set the adc to free running mode
  ADMUX = 0x40; // use adc0
  DIDR0 = 0x01; // turn off the digital input for adc0

}




void loop() {
  int prev_j[8];
  int beat = 0;
  int prev_oct_j;
  int counter = 0;
  int prev_beat = 0;
  int led_index = 0;
  int saturation = 0;
  int saturation_prev = 0;
  int brightness = 0;
  int brightness_prev = 0;

  while (1) { // reduces jitter

    cli();  // UDRE interrupt slows this way down on arduino1.0

    for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fht_input[i] = k; // put real data into bins
    }
    fht_window(); // window the data for better frequency response
    fht_reorder(); // reorder the data before doing the fht
    fht_run(); // process the data in the fht
    fht_mag_octave(); // take the output of the fht  fht_mag_log()

    // every 50th loop, adjust the volume accourding to the value on A2 (Pot)
    if (counter >= 50) {
      ADMUX = 0x40 | (1 & 0x07); // set admux to look at Analogpin A1 - Master Volume


      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      delay(10);
      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      float master_volume = (k + 0.1) / 1000 + .75; // so the valu will be between ~0.5 and 1.---------------------+.75 was .5
      Serial.println (master_volume);


      for (int i = 1; i < 8; i++) {
        noise_fact_adj[i] = noise_fact[i] * master_volume;
      }

      ADMUX = 0x40 | (0 & 0x07); // set admux back to look at A0 analog pin (to read the microphone input
      counter = 0;
    }

    sei();
    counter++;


    // End of Fourier Transform code - output is stored in fht_oct_out[i].

    // i=0-7 frequency (octave) bins (don't use 0 or 1), fht_oct_out[1]= amplitude of frequency for bin 1
    // for loop a) removes background noise average and takes absolute value b) low / high pass filter as still very noisy
    // c) maps amplitude of octave to a colour between blue and red d) sets pixel colour to amplitude of each frequency (octave)

    for (int i = 1; i < 8; i++) {  // goes through each octave. skip the first 1, which is not useful

      int j;
      j = (fht_oct_out[i] - noise[i]); // take the pink noise average level out, take the asbolute value to avoid negative numbers
      if (j < 10) {
        j = 0;
      }
      j = j * noise_fact_adj[i];

      if (j < 10) {
        j = 0;
      }
      else {
        j = j * noise_fact_adj[i];
        if (j > 180) {
          if (i >= 7) {
            beat += 2;
          }
          else {
            beat += 1;
          }
        }
        j = j / 30;
        j = j * 30; // (force it to more discrete values)
      }

      prev_j[i] = j;

      //     Serial.print(j);
      //     Serial.print(" ");


      // this fills in 11 LED's with interpolated values between each of the 8 OCT values
      if (i >= 2) {
        led_index = 2 * i - 3;
        prev_oct_j = (j + prev_j[i - 1]) / 2;

        saturation = constrain(j + 50, 0, 255); //-----------50 was 30
        saturation_prev = constrain(prev_oct_j + 50, 0, 255);
        brightness = constrain(j, 0, 255);
        brightness_prev = constrain(prev_oct_j, 0, 255);
        if (brightness == 255) {
          saturation = 50;
          brightness = 200;
        }
        if (brightness_prev == 255) {
          saturation_prev = 50;
          brightness_prev = 200;
        }


        for (uint8_t y = 0; y < kMatrixHeight; y++) {
          leds[XY(led_index - 1, y)] = CHSV(j + y * 30, saturation, brightness);
          if (i > 2) {
            prev_oct_j = (j + prev_j[i - 1]) / 2;
            leds[ XY(led_index - 2, y)] = CHSV(prev_oct_j + y * 30, saturation_prev, brightness_prev);
          }
        }
      }
    }



    if (beat >= 7) {
      fill_solid(leds, NUM_LEDS, CRGB::Gray);
      FastLED.setBrightness(200);



    }
    else {
      if (prev_beat != beat) {
        FastLED.setBrightness(40 + beat * beat * 5);
        prev_beat = beat;
      }

    }

    FastLED.show();
    if (beat) {
      counter2 += ((beat + 4) / 2 - 2);
      if (counter2 < 0) {
        counter2 = 1000;
      }
      if (beat > 3 && beat < 7) {
        FastLED.delay (20);
      }
      beat = 0;
    }

    // Serial.println();
  }
}



// Param for different pixel layouts
const bool    kMatrixSerpentineLayout = false;
// Set 'kMatrixSerpentineLayout' to false if your pixels are
// laid out all running the same way, like this:

// Set 'kMatrixSerpentineLayout' to true if your pixels are
// laid out back-and-forth, like this:

uint16_t XY( uint8_t x, uint8_t y)
{
  uint16_t i;

  if ( kMatrixSerpentineLayout == false) {
    i = (y * kMatrixWidth) + x;
  }

  if ( kMatrixSerpentineLayout == true) {
    if ( y & 0x01) {
      // Odd rows run backwards
      uint8_t reverseX = (kMatrixWidth - 1) - x;
      i = (y * kMatrixWidth) + reverseX;

    } else {
      // Even rows run forwards
      i = (y * kMatrixWidth) + x;

    }
  }

  i = (i + counter2) % NUM_LEDS;
  return i;
}

项目四十一:六十四位音乐频谱灯十六位音乐反应动态频谱灯

  实验视频剪辑

https://v.youku.com/v_show/id_XNTgwODYyMzEwMA==.html?firsttime=0

 

37.jpg

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

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

  项目四十二:快速哈特利变换FHT音乐反应64位灯板

  实验开源代码


 

代码
/*

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

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

 项目四十二:快速哈特利变换FHT音乐反应64位灯板

*/

/*

 这是带有 FastLED 的 FHT 库的项目

 FHT 库位于 http://wiki.openmusiclabs.com/wiki/ArduinoFHT

 开始的例子是:

 https://github.com/TJC/arduino/blob/master/fhttest/fhttest.cpp

 注意:如果您使用的是由 3.3V 信号供电的麦克风,例如 Sparkfun MEMS 麦克风,则将 3.3V 连接到 AREF 引脚。

 还要确保取消对 analogReference(EXTERNAL); 的注释。 在设置()中。

 在线频率发生器 测试:http://onlinetonegenerator.com/frequency-sweep-generator.html

*/

#define qsubd(x, b) ((x>b)?wavebright:0)           // A digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.

#define qsuba(x, b) ((x>b)?x-b:0)              // Unsigned subtraction macro. if result <0, then => 0.

#define wavebright 128                    // qsubd result will be this value if subtraction is >0.

#include "FastLED.h"                     // FastLED library. Preferably the latest copy of FastLED 2.1.

#if FASTLED_VERSION < 3001000

#error "Requires FastLED 3.1 or later; check github for latest code."

#endif

// Fixed definitions cannot change on the fly.

#define LED_DT 6                       // Data pin to connect to the strip.

//#define LED_CK 11                       // Clock pin for APA102 or WS2801

#define COLOR_ORDER GRB                    // It's GRB for WS2812

#define LED_TYPE WS2812B                    // What kind of strip are you using (APA102, WS2801 or WS2812B)

#define NUM_LEDS 64                    // Number of LED's.

// Initialize changeable global variables.

uint8_t max_bright = 255;                   // Overall brightness definition. It can be changed on the fly.

struct CRGB leds[NUM_LEDS];                  // Initialize our LED array.

#define LOG_OUT 1

#define FHT_N 256                       // Set to 256 point fht.

#define inputPin A0

//#define potPin A4

#include <FHT.h>                       // FHT library

uint8_t hueinc = 0;                        // A hue increment value to make it rotate a bit.

uint8_t micmult = 25;

uint8_t fadetime = 900;

uint8_t noiseval = 25;                    // Increase this to reduce sensitivity. 30 seems best for quiet

void setup() {

 analogReference(EXTERNAL);                 // Connect 3.3V to AREF pin for any microphones using 3.3V

 Serial.begin(9600);                    // use the serial port

 LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);

 // LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);

 FastLED.setBrightness(max_bright);

 set_max_power_in_volts_and_milliamps(5, 500);        // FastLED Power management set at 5V, 500mA.

}

void loop() {

 //  noiseval = map(analogRead(potPin), 0, 1023, 16, 48);     // Adjust sensitivity of cutoff.

 EVERY_N_MILLISECONDS(13) {

  fhtsound();

 }

 show_at_max_brightness_for_power();

 Serial.println(LEDS.getFPS(), DEC);     // Display frames per second on the serial monitor.

 Serial.println(" ");     // Display frames per second on the serial monitor.

 Serial.println(analogRead(inputPin));    // print as an ASCII-encoded decimal     */

}

void fhtsound() {

 // hueinc++;                          // A cute little hue incrementer.

 GetFHT();                          // Let's take FHT_N samples and crunch 'em.

 for (int i = 0; i < NUM_LEDS; i++) {            // Run through the LED array.

  int tmp = qsuba(fht_log_out[2 * i + 2], noiseval);    // Get the sample and subtract the 'quiet' normalized values, but don't go < 0.

  if (tmp > (leds[i].r + leds[i].g + leds[i].b) / 2)     // Refresh an LED only when the intensity is low

   leds[i] = CHSV((i * 4) + tmp * micmult, 255, tmp * micmult); // Note how we really cranked up the tmp value to get BRIGHT LED's. Also increment the hue for fun.

  leds[i].nscale8(fadetime);                   // Let's fade the whole thing over time as well.

 }

} // fhtsound()

void GetFHT() {

 cli();

 for (int i = 0 ; i < FHT_N ; i++) fht_input[i] = analogRead(inputPin);

 sei();

 fht_window();                        // Window the data for better frequency response.

 fht_reorder();                       // Reorder the data before doing the fht.

 fht_run();                         // Process the data in the fht.

 fht_mag_log();

} // GetFHT()

  项目四十二:快速哈特利变换FHT音乐反应64位灯板


 实验视频剪辑

https://v.youku.com/v_show/id_XNTgwODY2NzkzMg==.html?spm=a2hcb.playlsit.page.1

 

 


【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>

#include <math.h>

#define N_PIXELS 64

#define MIC_PIN  A0

#define LED_PIN  6

#define SAMPLE_WINDOW  5

#define PEAK_HANG 24

#define PEAK_FALL 4

#define INPUT_FLOOR 10

#define INPUT_CEILING 50

byte peak = 16;

unsigned int sample;

byte Count = 0;

byte HangCount = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {

 Serial.begin(9600);

 analogReference(EXTERNAL);

 strip.setBrightness(22);

 strip.show();

 strip.begin();

}

float fscale( float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve) {

 float OriginalRange = 0;

 float NewRange = 0;

 float zeroRefCurVal = 0;

 float normalizedCurVal = 0;

 float rangedValue = 0;

 boolean invFlag = 0;

 if (curve > 10) curve = 10;

 if (curve < -10) curve = -10;

 curve = (curve * -.1) ;

 curve = pow(10, curve);

 if (inputValue < originalMin) {

  inputValue = originalMin;

 }

 if (inputValue > originalMax) {

  inputValue = originalMax;

 }

 OriginalRange = originalMax - originalMin;

 if (newEnd > newBegin) {

  NewRange = newEnd - newBegin;

 }

 else

 {

  NewRange = newBegin - newEnd;

  invFlag = 1;

 }

 zeroRefCurVal = inputValue - originalMin;

 normalizedCurVal = zeroRefCurVal / OriginalRange;  // normalize to 0 - 1 float

 Serial.print(OriginalRange, DEC);

 Serial.print("  ");

 Serial.print(NewRange, DEC);

 Serial.print("  ");

 Serial.println(zeroRefCurVal, DEC);

 Serial.println();

 delay(10); 

 if (originalMin > originalMax ) {

  return 0;

 }

 if (invFlag == 0) {

  rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;

 }

 else

 {

  rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);

 }

 return rangedValue;

}

void loop() {

 unsigned long startMillis = millis();

 float peakToPeak = 0;

 unsigned int signalMax = 0;

 unsigned int signalMin = 1023;

 unsigned int c, y;

 while (millis() - startMillis < SAMPLE_WINDOW)

 {

  sample = analogRead(MIC_PIN);

  if (sample < 1024)

  {

   if (sample > signalMax)

   {

    signalMax = sample;

   }

   else if (sample < signalMin)

   {

    signalMin = sample;

   }

  }

 }

 peakToPeak = signalMax - signalMin;

 for (int i = 0; i <= strip.numPixels() - 1; i++) {

  strip.setPixelColor(i, Wheel(map(i, 0, strip.numPixels() - 1, 30, 150)));

 }

 c = fscale(INPUT_FLOOR, INPUT_CEILING, strip.numPixels(), 0, peakToPeak, 2);

 if (c < peak) {

  peak = c;

  HangCount = 0;

 }

 if (c <= strip.numPixels()) {

  drawLine(strip.numPixels(), strip.numPixels() - c, strip.Color(0, 0, 0));

 }

 y = strip.numPixels() - peak;

 strip.setPixelColor(y - 1, Wheel(map(y, 0, strip.numPixels() - 1, 30, 150)));

 strip.show();

 if (HangCount > PEAK_HANG) {

  if (++Count >= PEAK_FALL) {

   peak++;

   Count = 0;

  }

 }

 else {

  HangCount++;

 }

}

void drawLine(uint8_t from, uint8_t to, uint32_t c) {

 uint8_t fromTemp;

 if (from > to) {

  fromTemp = from;

  from = to;

  to = fromTemp;

 }

 for (int i = from; i <= to; i++) {

  strip.setPixelColor(i, c);

 }

}

uint32_t Wheel(byte WheelPos) {

 if (WheelPos < 85) {

  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);

 }

 else if (WheelPos < 170) {

  WheelPos -= 85;

  return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);

 }

 else {

  WheelPos -= 170;

  return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);

 }

}

Arduino实验场景图
 

39.jpg

项目四十三:Adafruit_NeoPixel多彩音乐节奏灯板

 实验视频剪辑

https://v.youku.com/v_show/id_XNTgwODgwMzk5Ng==.html?spm=a2hcb.playlsit.page.1


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

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

 项目四十四:法式流水火花屏

 实验开源代码
 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十四:法式流水火花屏
*/

#include <FastLED.h>
#define LED_PIN     6
#define BRIGHTNESS  30
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
const uint8_t kMatrixWidth  = 8;
const uint8_t kMatrixHeight = 8;
#define WIDTH kMatrixWidth
#define HEIGHT kMatrixHeight
#define ROWS kMatrixWidth
#define COLS kMatrixHeight
#define LED_ROWS kMatrixWidth
#define LED_COLS kMatrixHeight
CRGB leds[kMatrixWidth * kMatrixHeight];
const bool    kMatrixSerpentineLayout = true;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define enlargedOBJECT_MAX_COUNT WIDTH*2
#define SPEED_ADJ (float)NUM_LEDS/512

//speed control
byte speed = 127; // 1-255

//scale control
byte scale = 150; //1-255

void setup() {
  delay(3000);
  LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  LEDS.setBrightness(BRIGHTNESS);
}

static const TProgmemRGBPalette16 MagmaColor_p FL_PROGMEM = {CRGB::Black, 0x240000, 0x480000, 0x660000, 0x9a1100, 0xc32500, 0xd12a00, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400, 0xffffff};
extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};
extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};
extern const TProgmemRGBPalette16 NormalFire2_p FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};
extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink};
extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};
extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen};
extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};
extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue};
extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink};

static double fmap(const double x, const double in_min, const double in_max, const double out_min, const double out_max) {
  return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
}

#define SPARKS_AM WIDTH
float FADE_KOEF = 10;
float SpeedK = .98;
float SpeedDecX = .01;
float SpeedDecY = 0;
#define Board 1
#define GravityX 0
#define GravityY 1

int sparksPos[2][SPARKS_AM];
float sparksSpeed[2][SPARKS_AM];
float sparksFade[SPARKS_AM];
byte sparksColor[SPARKS_AM];

int genPos[2];
int gravityPos[2];
bool run = true;

bool loadingFlag = true;
void reg(byte id) {
  sparksPos[0][id] = genPos[0];
  sparksPos[1][id] = genPos[1];
  sparksSpeed[0][id] = random(-10, 10);
  sparksSpeed[1][id] = random(-10, 10);
  sparksFade[id] = 255;
  sparksColor[id] = random();
}

void physics(byte id) {
  if (SpeedK) {
    if (GravityX) {
      if (gravityPos[0] < sparksPos[0][id])
        sparksSpeed[0][id] -= SpeedK;
      else
        sparksSpeed[0][id] += SpeedK;
    }
    if (GravityY) {
      if (gravityPos[1] < sparksPos[1][id])
        sparksSpeed[1][id] -= SpeedK;
      else
        sparksSpeed[1][id] += SpeedK;
    }
  }
  sparksFade[id] -= (255. / (float)((HEIGHT + WIDTH) * FADE_KOEF));
  if (SpeedDecX || sparksSpeed[0][id]) {
    if (sparksSpeed[0][id] > 0)
      sparksSpeed[0][id] -= SpeedDecX;
    else
      sparksSpeed[0][id] += SpeedDecX;
    if (abs(sparksSpeed[0][id]) <= SpeedDecX)
      sparksSpeed[0][id] = 0;
  }
  if (SpeedDecY || sparksSpeed[1][id]) {
    if (sparksSpeed[1][id] > 0)
      sparksSpeed[1][id] -= SpeedDecY;
    else
      sparksSpeed[1][id] += SpeedDecY;
    if (abs(sparksSpeed[1][id]) <= SpeedDecY)
      sparksSpeed[1][id] = 0;
  }
  if (Board) {
    if (sparksPos[0][id] < 0 || sparksPos[0][id] >= WIDTH * 10) sparksSpeed[0][id] = -sparksSpeed[0][id];
    if (sparksPos[1][id] < 0) sparksSpeed[1][id] = -sparksSpeed[1][id];
  }
  sparksPos[0][id] += constrain(sparksSpeed[0][id], -10, 10);
  sparksPos[1][id] += constrain(sparksSpeed[1][id], -10, 10);
}

void wu_pixel(uint32_t x, uint32_t y, CRGB * col) { //awesome wu_pixel procedure by reddit u/sutaburosu
  // extract the fractional parts and derive their inverses
  uint8_t xx = x & 0xff, yy = y & 0xff, ix = 255 - xx, iy = 255 - yy;
  // calculate the intensities for each affected pixel
#define WU_WEIGHT(a, b)((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
  uint8_t wu[4] = {
    WU_WEIGHT(ix, iy),
    WU_WEIGHT(xx, iy),
    WU_WEIGHT(ix, yy),
    WU_WEIGHT(xx, yy)
  };
  // multiply the intensities by the colour, and saturating-add them to the pixels
  for (uint8_t i = 0; i < 4; i++) {
    uint16_t xy = XY((x >> 8) + (i & 1), (y >> 8) + ((i >> 1) & 1));
    leds[xy].r = qadd8(leds[xy].r, col -> r * wu[i] >> 8);
    leds[xy].g = qadd8(leds[xy].g, col -> g * wu[i] >> 8);
    leds[xy].b = qadd8(leds[xy].b, col -> b * wu[i] >> 8);
  }
}

void render(byte id, CRGB Col) {
  if (loadingFlag) {
    for (byte i = 0; i < SPARKS_AM; i++) {
      reg(i);
      for (byte a = 0; a < i; a++) {
        physics(a);
      }
    }
    loadingFlag = false;
  }
  physics(id);
  if (sparksPos[1][id] < ((HEIGHT - 1) * 10) and sparksPos[1][id] >= 0)
    if (sparksPos[0][id] < ((WIDTH - 1) * 10) and sparksPos[0][id] >= 0) {
      CRGB color = Col;
      wu_pixel(sparksPos[0][id] * 25.6, sparksPos[1][id] * 25.6, & color);
    }
}

void setGenPos(int x, int y) {
  genPos[0] = x;
  genPos[1] = y;
}

void setGravityPos(int x, int y) {
  gravityPos[0] = x;
  gravityPos[1] = y;
}

void setRegenRule(byte id, bool b) {
  if (b) reg(id);
}

void draw() {
  fadeToBlackBy(leds, NUM_LEDS, 20);
  setGenPos(beatsin16(10, 0, WIDTH * 10), beatsin16(10, 0, HEIGHT * 10, 0, 16384));
  setGravityPos(0, 0);
  for (byte i = 0; i < SPARKS_AM; i++) {
    setRegenRule(i, (sparksFade[i] <= 35) ? 1 : 0);
    render(i, CHSV(sparksColor[i], 255, constrain(sparksFade[i], 32, 255)));
  }
  delay(16);
}

void loop() {

  draw();
  LEDS.show();
}

uint16_t XY( uint8_t x, uint8_t y)
{
  uint16_t i;
  if ( kMatrixSerpentineLayout == false) {
    i = (y * kMatrixWidth) + x;
  }
  if ( kMatrixSerpentineLayout == true) {
    if ( y & 0x01) {
      // Odd rows run backwards
      uint8_t reverseX = (kMatrixWidth - 1) - x;
      i = (y * kMatrixWidth) + reverseX;
    } else {
      // Even rows run forwards
      i = (y * kMatrixWidth) + x;
    }
  }
  return i;
}

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

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

 项目四十五:绿色火焰

 实验开源代码
 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十五:绿色火焰
*/

#include <FastLED.h>
#define LED_PIN     6
#define BRIGHTNESS  30
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
const uint8_t kMatrixWidth  = 8;
const uint8_t kMatrixHeight = 8;
#define WIDTH kMatrixWidth
#define HEIGHT kMatrixHeight
#define ROWS kMatrixWidth
#define COLS kMatrixHeight
CRGB leds[kMatrixWidth * kMatrixHeight];
const bool    kMatrixSerpentineLayout = true;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define enlargedOBJECT_MAX_COUNT WIDTH*2
#define SPEED_ADJ (float)NUM_LEDS/512

//speed control
byte speed = 127; // 1-255

//scale control
byte scale = 150; //1-255

//control magma bursts
const byte deltaValue = 6U; 
const byte deltaHue = 8U; 

void setup() 
{
  delay(3000);
  LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  LEDS.setBrightness(BRIGHTNESS);  
 
}
static const TProgmemRGBPalette16 MagmaColor_p FL_PROGMEM = {CRGB::Black, 0x240000, 0x480000, 0x660000, 0x9a1100, 0xc32500, 0xd12a00, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400, 0xffffff};                     
extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};    
extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};                    
extern const TProgmemRGBPalette16 NormalFire2_p FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};             
extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink}; 
extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};    
extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen}; 
extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};
extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue};  
extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink}; 


static double fmap(const double x, const double in_min, const double in_max, const double out_min, const double out_max){
    return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
}

float randomf(float min, float max)
{
  return fmap(random(1024), 0, 1023, min, max);
}

void drawPixelXYF(float x, float y, CRGB color)
{
  if (x < 0 || y < 0 || x > ((float)WIDTH - 1) || y > ((float)HEIGHT - 1)) return;
  uint8_t xx = (x - (int)x) * 255, yy = (y - (int)y) * 255, ix = 255 - xx, iy = 255 - yy;
  // calculate the intensities for each affected pixel
  #define WU_WEIGHT(a,b) ((uint8_t) (((a)*(b)+(a)+(b))>>8))
  uint8_t wu[4] = {WU_WEIGHT(ix, iy), WU_WEIGHT(xx, iy),
                   WU_WEIGHT(ix, yy), WU_WEIGHT(xx, yy)};
  // multiply the intensities by the colour, and saturating-add them to the pixels
  for (uint8_t i = 0; i < 4; i++) {
    int16_t xn = x + (i & 1), yn = y + ((i >> 1) & 1);
    CRGB clr = leds[XY(xn, yn)];
    clr.r = qadd8(clr.r, (color.r * wu[i]) >> 8);
    clr.g = qadd8(clr.g, (color.g * wu[i]) >> 8);
    clr.b = qadd8(clr.b, (color.b * wu[i]) >> 8);
    leds[XY(xn, yn)] = clr;
  }
}


// (c) Сотнег (SottNick) 2021
class EffectMagma {
private:    
    float ff_y, ff_z;        
    uint8_t step, ObjectNUM = WIDTH; 
    uint8_t shiftHue[HEIGHT];
    float trackingObjectPosX[enlargedOBJECT_MAX_COUNT];
    float trackingObjectPosY[enlargedOBJECT_MAX_COUNT];
    uint8_t trackingObjectHue[enlargedOBJECT_MAX_COUNT];
    float trackingObjectSpeedX[enlargedOBJECT_MAX_COUNT];
    float trackingObjectShift[enlargedOBJECT_MAX_COUNT];
    float speedfactor;

    void regen();
    void LeapersMove_leaper(uint8_t l);
    void LeapersRestart_leaper(uint8_t l);

public:
    void load();
    bool run();
};


void EffectMagma::load() {
  speedfactor = fmap(speed, 1, 255, SPEED_ADJ*0.1, SPEED_ADJ);
  ObjectNUM = map(scale, 1, 255, WIDTH, enlargedOBJECT_MAX_COUNT);
  regen();
}


void EffectMagma::regen() {
  for (uint8_t j = 0; j < HEIGHT; j++) {
    shiftHue[j] = map(j, 0, HEIGHT+HEIGHT/4, 255, 0); // init colorfade table
  }


  for (uint8_t i = 0 ; i < enlargedOBJECT_MAX_COUNT ; i++) {
    LeapersRestart_leaper(i);  
    trackingObjectHue[i] = 50U;
  }
}
bool EffectMagma::run() {
  fadeToBlackBy(leds, NUM_LEDS, 25);

  for (uint8_t i = 0; i < ObjectNUM; i++) {
    LeapersMove_leaper(i);
    drawPixelXYF(trackingObjectPosX[i], trackingObjectPosY[i], ColorFromPalette(CopperFireColors_p, trackingObjectHue[i], 255));
  }

  for (uint8_t i = 0; i < WIDTH; i++) {
    for (uint8_t j = 0; j < HEIGHT; j++) {
      leds[XY(i, HEIGHT-1 - j)] += ColorFromPalette(RubidiumFireColors_p, qsub8(inoise8(i * deltaValue, (j + ff_y + random8(2)) * deltaHue, ff_z), shiftHue[j]), 255U);
    }
  }

  ff_y += speedfactor * 2;
  ff_z += speedfactor;
  blur2d(leds, WIDTH, HEIGHT,4 );
  return true;
}

void EffectMagma::LeapersMove_leaper(uint8_t l) {
#define GRAVITY            0.1
  trackingObjectShift[l] -= GRAVITY * speedfactor;

  trackingObjectPosX[l] += trackingObjectSpeedX[l] * speedfactor;
  trackingObjectPosY[l] += trackingObjectShift[l] * speedfactor;

  // bounce off the ceiling?
  if (trackingObjectPosY[l] > HEIGHT + HEIGHT/4) {
    trackingObjectShift[l] = -trackingObjectShift[l];
  }
  
  // settled on the floor?
  if (trackingObjectPosY[l] <= (HEIGHT/8-1)) {
    LeapersRestart_leaper(l);
  }

  // bounce off the sides of the screen?
  if (trackingObjectPosX[l] < 0 || trackingObjectPosX[l] > WIDTH-1) {
    LeapersRestart_leaper(l);
  }

}

void EffectMagma::LeapersRestart_leaper(uint8_t l) {
  randomSeed(millis());
  // leap up and to the side with some random component
  trackingObjectSpeedX[l] = randomf(-0.75, 0.75);
  trackingObjectShift[l] = randomf(0.50, 0.85);
  trackingObjectPosX[l] = randomf(0, WIDTH);
  trackingObjectPosY[l] = randomf(0, (float)HEIGHT/4-1);

  // for variety, sometimes go 100% faster
  if (random8() < 12) {
    trackingObjectShift[l] += trackingObjectShift[l] * 2;
  }

}

bool load = true;
EffectMagma eff;


void draw() {
  if (load) {
    eff.load();
    load = false;
  }
  eff.run();
  //FastLED.delay(16);
}

void loop(){

draw();
LEDS.show();
}
uint16_t XY(uint8_t x, uint8_t y)
{
  uint16_t i;
  if (kMatrixSerpentineLayout == false)
  {
    i = (y * kMatrixWidth) + x;
  }
  if (kMatrixSerpentineLayout == true)
  {
    if (y & 0x01)
    {
      // Odd rows run backwards
      uint8_t reverseX = (kMatrixWidth - 1) - x;
      i = (y * kMatrixWidth) + reverseX;
    }
    else
    {
      // Even rows run forwards
      i = (y * kMatrixWidth) + x;
    }
  }
  return i;
}

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

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

 项目四十六:多彩向日葵

 实验开源代码

 

代码
/*

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

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

 项目四十六:多彩向日葵

*/

#include <FastLED.h>

#define LED_PIN   6

#define BRIGHTNESS 30

#define LED_TYPE  WS2812B

#define COLOR_ORDER GRB

const uint8_t kMatrixWidth = 8;

const uint8_t kMatrixHeight = 8;

#define WIDTH kMatrixWidth

#define HEIGHT kMatrixHeight

#define ROWS kMatrixWidth

#define COLS kMatrixHeight

#define LED_ROWS kMatrixWidth

#define LED_COLS kMatrixHeight

CRGB leds[kMatrixWidth * kMatrixHeight];

const bool  kMatrixSerpentineLayout = true;

#define NUM_LEDS (kMatrixWidth * kMatrixHeight)

#define enlargedOBJECT_MAX_COUNT WIDTH*2

#define SPEED_ADJ (float)NUM_LEDS/512

//speed control

byte speed = 127; // 1-255

//scale control

byte scale = 150; //1-255

const byte deltaValue = 6U; 

const byte deltaHue = 8U; 

void setup() 

{

 delay(3000);

 LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);

 LEDS.setBrightness(BRIGHTNESS);  

 

}

static const TProgmemRGBPalette16 MagmaColor_p FL_PROGMEM = {CRGB::Black, 0x240000, 0x480000, 0x660000, 0x9a1100, 0xc32500, 0xd12a00, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400, 0xffffff};           

extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};   

extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};           

extern const TProgmemRGBPalette16 NormalFire2_p FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};          

extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink};   

extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};    

extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen};  

extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};

extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue}; 

extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink}; 

static double fmap(const double x, const double in_min, const double in_max, const double out_min, const double out_max){

  return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;

}

#define CenterX ((LED_COLS / 2) - 0.5)

#define CenterY ((LED_ROWS / 2) - 0.5)

const byte maxDim = max(LED_COLS, LED_ROWS);

byte effect= 1;

bool change = true;

void drawPixelXYF(float x, float y,

 const CRGB & color) {

 // extract the fractional parts and derive their inverses

 uint8_t xx = (x - (int) x) * 255, yy = (y - (int) y) * 255, ix = 255 - xx, iy = 255 - yy;

 // calculate the intensities for each affected pixel

 #define WU_WEIGHT(a, b)((uint8_t)(((a) * (b) + (a) + (b)) >> 8))

 uint8_t wu[4] = {

  WU_WEIGHT(ix, iy),

  WU_WEIGHT(xx, iy),

  WU_WEIGHT(ix, yy),

  WU_WEIGHT(xx, yy)

 };

 // multiply the intensities by the colour, and saturating-add them to the pixels

 for (uint8_t i = 0; i < 4; i++) {

  int16_t xn = x + (i & 1), yn = y + ((i >> 1) & 1);

  CRGB clr = leds[XY(xn, yn)];

  clr.r = qadd8(clr.r, (color.r * wu[i]) >> 8);

  clr.g = qadd8(clr.g, (color.g * wu[i]) >> 8);

  clr.b = qadd8(clr.b, (color.b * wu[i]) >> 8);

  leds[XY(xn, yn)] = clr;

 }

 #undef WU_WEIGHT

}

void draw() {

 FastLED.clear(); //fadeToBlackBy(leds, NUM_LEDS, 16);

 unsigned long t = millis()/20;

 for(float i = 1; i <maxDim / 2; i+=0.25) {

  double angle = radians(t * (maxDim/2-i));

  drawPixelXYF(CenterX + sin(angle) * i, CenterY + cos(angle) * i, ColorFromPalette(PartyColors_p, (i * 20) + (t / 20)));

  switch(effect){

  case 1: case 3: drawPixelXYF(CenterX + cos(angle) * i, CenterY + sin(angle) * i, ColorFromPalette(PartyColors_p, (i * 20) + (t / 20)));break;

 }}

 switch(effect){

  case 2: case 3: blur2d(leds, LED_COLS, LED_ROWS, 16); break;

 }

 delay(16);

}

void loop(){

draw();

LEDS.show();

}

uint16_t XY( uint8_t x, uint8_t y)

{

 uint16_t i;

 if( kMatrixSerpentineLayout == false) {

  i = (y * kMatrixWidth) + x;

 }

 if( kMatrixSerpentineLayout == true) {

  if( y & 0x01) {

   // Odd rows run backwards

   uint8_t reverseX = (kMatrixWidth - 1) - x;

   i = (y * kMatrixWidth) + reverseX;

  } else {

   // Even rows run forwards

   i = (y * kMatrixWidth) + x;

  }

 }

 return i;

}

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

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

 项目四十七:多彩沙漏

  实验开源代码


 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十七:多彩沙漏
*/

#include <FastLED.h>
#define LED_PIN     6
#define BRIGHTNESS  30
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
const uint8_t kMatrixWidth  = 8;
const uint8_t kMatrixHeight = 8;
#define WIDTH kMatrixWidth
#define HEIGHT kMatrixHeight
#define ROWS kMatrixWidth
#define COLS kMatrixHeight
#define LED_ROWS kMatrixWidth
#define LED_COLS kMatrixHeight
const bool    kMatrixSerpentineLayout = true;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define N_LEDS NUM_LEDS
#define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
CRGB leds[kMatrixWidth * kMatrixHeight];
uint16_t offset = 0;
void setup() 

{
  delay(3000);
  LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  LEDS.setBrightness(BRIGHTNESS);  
}


void randomdot() {
  offset++;
  //byte a= LED_COLS/2; //
  byte a = random8(LED_COLS / 4) + LED_COLS * 3 / 8; //
  if (!random8(4)) leds[XY(a, LED_ROWS - 1)].setHue(offset * 15); // 0 or 1
  if (offset > 250) {
    offset = 10;
  }
      
}

void updatesand() {
  int index, indexXadd1Y, indexXsub1Y, indexXYadd1;
  for (int y = 0; y < LED_ROWS - 1; y++) {
    for (int x = 1; x < LED_COLS - 1; x++) {
      index = XY(x, y);
      indexXadd1Y = XY(x + 1, y);
      indexXsub1Y = XY(x - 1, y);
      indexXYadd1 = XY(x, y + 1);
      if (!leds[index] && !leds[indexXYadd1]) continue;
      if (!leds[index] && leds[indexXYadd1]) {
        leds[index] = leds[indexXYadd1];
        leds[indexXYadd1] = 0;
      }
      if (leds[index] && leds[indexXYadd1] && !leds[indexXsub1Y] && !leds[indexXadd1Y]) {
        if (random8(4)) {
          leds[indexXsub1Y] = leds[indexXYadd1];
          leds[indexXYadd1] = 0;
      } else {
          leds[indexXadd1Y] = leds[indexXYadd1];
          leds[indexXYadd1] = 0;
        }
      }
      if (leds[index] && leds[indexXYadd1] && !leds[indexXsub1Y] && leds[indexXadd1Y]) {
        leds[indexXsub1Y] = leds[indexXYadd1];
        leds[indexXYadd1] = 0;
      }
      if (leds[index] && leds[indexXYadd1] && leds[indexXsub1Y] && !leds[indexXadd1Y]) {
        leds[indexXadd1Y] = leds[indexXYadd1];
        leds[indexXYadd1] = 0;
      }
    }
  }
}

void randomdel() {
  for (int i = 0; i < N_LEDS; i++) {
    if (!random8(20)) leds[i] = 0;
  }
  //leds[XY(0, 0)] = 0;
}

void falldown() {
  for (int y = 0; y < LED_ROWS - 1; y++) {
    for (int x = 0; x < LED_COLS; x++) {
      if (!leds[XY(x, y)] && leds[XY(x, y + 1)]) {
        leds[XY(x, y)] = leds[XY(x, y + 1)];
        leds[XY(x, y + 1)] = 0;
      }
    }
  }
}

void draw() {
  EVERY_N_MILLISECONDS(1) {
    updatesand();
    randomdot();
  }
  // Level controled by LED_ROWS/3   
  if ((uint32_t) leds[XY(0, LED_ROWS / 3)] > 0) {
    EVERY_N_MILLISECONDS(2000) {
      randomdel();
      falldown();
      falldown();
      falldown();
    }
  }
}
void loop() {

  draw();
  LEDS.show();

}
uint16_t XY(uint8_t x, uint8_t y)
{
  uint16_t i;
  if (kMatrixSerpentineLayout == false)
  {
    i = (y * kMatrixWidth) + x;
  }
  if (kMatrixSerpentineLayout == true)
  {
    if (y & 0x01)
    {
      // Odd rows run backwards
      uint8_t reverseX = (kMatrixWidth - 1) - x;
      i = (y * kMatrixWidth) + reverseX;
    }
    else
    {
      // Even rows run forwards
      i = (y * kMatrixWidth) + x;
    }
  }
  return i;
}

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

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

项目四十八:鎏金岁月

  实验开源代码


 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十八:鎏金岁月
*/

#include <FastLED.h>
#define LED_PIN     6
#define BRIGHTNESS  30
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
const uint8_t kMatrixWidth  = 8;
const uint8_t kMatrixHeight = 8;
#define WIDTH kMatrixWidth
#define HEIGHT kMatrixHeight
#define ROWS kMatrixWidth
#define COLS kMatrixHeight
#define LED_ROWS kMatrixWidth
#define LED_COLS kMatrixHeight
CRGB leds[kMatrixWidth * kMatrixHeight];
const bool kMatrixSerpentineLayout = true;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define enlargedOBJECT_MAX_COUNT WIDTH * 2
#define SPEED_ADJ (float)NUM_LEDS / 512

//speed control
byte speed = 127; // 1-255

//scale control
byte scale = 150; //1-255

void setup()
{
  delay(3000);
  LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  LEDS.setBrightness(BRIGHTNESS);
}
static const TProgmemRGBPalette16 MagmaColor_p FL_PROGMEM = {CRGB::Black, 0x240000, 0x480000, 0x660000, 0x9a1100, 0xc32500, 0xd12a00, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400, 0xffffff};
extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};
extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};
extern const TProgmemRGBPalette16 NormalFire2_p FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};
extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink};
extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};
extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen};
extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};
extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue};
extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink};

static double fmap(const double x, const double in_min, const double in_max, const double out_min, const double out_max)
{
  return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
}

const uint8_t exp_gamma[256] PROGMEM = {
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1,
1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,
1,   2,   2,   2,   2,   2,   2,   2,   2,   2,   3,   3,   3,   3,   3,
4,   4,   4,   4,   4,   5,   5,   5,   5,   5,   6,   6,   6,   7,   7,
7,   7,   8,   8,   8,   9,   9,   9,   10,  10,  10,  11,  11,  12,  12,
12,  13,  13,  14,  14,  14,  15,  15,  16,  16,  17,  17,  18,  18,  19,
19,  20,  20,  21,  21,  22,  23,  23,  24,  24,  25,  26,  26,  27,  28,
28,  29,  30,  30,  31,  32,  32,  33,  34,  35,  35,  36,  37,  38,  39,
39,  40,  41,  42,  43,  44,  44,  45,  46,  47,  48,  49,  50,  51,  52,
53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,
68,  70,  71,  72,  73,  74,  75,  77,  78,  79,  80,  82,  83,  84,  85,
87,  89,  91,  92,  93,  95,  96,  98,  99,  100, 101, 102, 105, 106, 108,
109, 111, 112, 114, 115, 117, 118, 120, 121, 123, 125, 126, 128, 130, 131,
133, 135, 136, 138, 140, 142, 143, 145, 147, 149, 151, 152, 154, 156, 158,
160, 162, 164, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187,
190, 192, 194, 196, 198, 200, 202, 204, 207, 209, 211, 213, 216, 218, 220,
222, 225, 227, 229, 232, 234, 236, 239, 241, 244, 246, 249, 251, 253, 254, 255};

const uint8_t cos_wave[256] = {
    0,   0,   0,   0,   1,   1,   1,   2,   2,   3,   4,   5,   6,   6,   8,
    9,   10,  11,  12,  14,  15,  17,  18,  20,  22,  23,  25,  27,  29,  31,
    33,  35,  38,  40,  42,  45,  47,  49,  52,  54,  57,  60,  62,  65,  68,
    71,  73,  76,  79,  82,  85,  88,  91,  94,  97,  100, 103, 106, 109, 113,
    116, 119, 122, 125, 128, 131, 135, 138, 141, 144, 147, 150, 153, 156, 159,
    162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 191, 194, 197, 199, 202,
    204, 207, 209, 212, 214, 216, 218, 221, 223, 225, 227, 229, 231, 232, 234,
    236, 238, 239, 241, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252, 252,
    253, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253,
    253, 252, 252, 251, 250, 249, 248, 247, 246, 245, 243, 242, 241, 239, 238,
    236, 234, 232, 231, 229, 227, 225, 223, 221, 218, 216, 214, 212, 209, 207,
    204, 202, 199, 197, 194, 191, 189, 186, 183, 180, 177, 174, 171, 168, 165,
    162, 159, 156, 153, 150, 147, 144, 141, 138, 135, 131, 128, 125, 122, 119,
    116, 113, 109, 106, 103, 100, 97,  94,  91,  88,  85,  82,  79,  76,  73,
    71,  68,  65,  62,  60,  57,  54,  52,  49,  47,  45,  42,  40,  38,  35,
    33,  31,  29,  27,  25,  23,  22,  20,  18,  17,  15,  14,  12,  11,  10,
    9,   8,   6,   6,   5,   4,   3,   2,   2,   1,   1,   1,   0,   0,   0, 0};

void GammaCorrection(){   //gamma correction function 
byte r,g,b;
for (uint16_t i=0; i<NUM_LEDS; i++){
r=leds[i].r;
g=leds[i].g;
b=leds[i].b;
leds[i].r = pgm_read_byte(exp_gamma + r);
leds[i].g = pgm_read_byte(exp_gamma + g);
leds[i].b = pgm_read_byte(exp_gamma + b);
}
}

void draw() {
byte speed = 5;

  uint8_t w = 2;
  uint8_t scale = 4;

  uint16_t a=millis()/32;
  uint16_t a2=a/2;
  uint16_t a3=a/3;

  uint16_t cx =  beatsin8 (10-speed,0,COLS)*scale;
  uint16_t cy =  beatsin8 (12-speed,0,ROWS)*scale;
  uint16_t cx1 = beatsin8 (13-speed,0,COLS)*scale;
  uint16_t cy1 = beatsin8 (15-speed,0,ROWS)*scale;
  uint16_t cx2 = beatsin8 (17-speed,0,COLS)*scale;
  uint16_t cy2 = beatsin8 (14-speed,0,ROWS)*scale;
  
  uint16_t xoffs=0;

  for (int x = 0; x < COLS; x++) {

     xoffs += scale;
     uint16_t yoffs = 0;

    for (int y = 0; y < ROWS; y++) {

       yoffs += scale;

      // byte rdistort = cos_wave [((x+y)*8+a2)&255]>>1; 
      // byte gdistort = cos_wave [((x+y)*8+a3+32)&255]>>1;
      // byte bdistort = cos_wave [((x+y)*8+a+64)&255]>>1;

      byte rdistort = cos_wave [(cos_wave[((x<<3)+a )&255]+cos_wave[((y<<3)-a2)&255]+a3   )&255]>>1; 
      byte gdistort = cos_wave [(cos_wave[((x<<3)-a2)&255]+cos_wave[((y<<3)+a3)&255]+a+32 )&255]>>1; 
      byte bdistort = cos_wave [(cos_wave[((x<<3)+a3)&255]+cos_wave[((y<<3)-a) &255]+a2+64)&255]>>1; 

      byte valueR = rdistort+ w*  (a- ( ((xoffs - cx) *  (xoffs - cx) +  (yoffs - cy) *   (yoffs - cy))>>7  ));
      byte valueG = gdistort+ w*  (a2-( ((xoffs - cx1) * (xoffs - cx1) + (yoffs - cy1) *  (yoffs - cy1))>>7 ));
      byte valueB = bdistort+ w*  (a3-( ((xoffs - cx2) * (xoffs - cx2) + (yoffs - cy2) *  (yoffs - cy2))>>7 ));

      valueR = cos_wave [(valueR)];
      valueG = cos_wave [(valueG)];
      valueB = cos_wave [(valueB)];

      uint16_t index = XY(x, y);
      leds[index].setRGB (valueR,valueG,valueB); 
    }
  }
  
  GammaCorrection();
}
void loop(){

draw();
LEDS.show();
}

uint16_t XY( uint8_t x, uint8_t y)
{
  uint16_t i;
  if( kMatrixSerpentineLayout == false) {
    i = (y * kMatrixWidth) + x;
  }
  if( kMatrixSerpentineLayout == true) {
    if( y & 0x01) {
      // Odd rows run backwards
      uint8_t reverseX = (kMatrixWidth - 1) - x;
      i = (y * kMatrixWidth) + reverseX;
    } else {
      // Even rows run forwards
      i = (y * kMatrixWidth) + x;
    }
  }
  return i;
}

Arduino实验场景图
 

38.jpg

评论

user-avatar
  • hacker_

    hacker_2023.08.16

    666

    0
    icon 他的勋章
      展开更多