[原创] 【花雕动手做】有趣好玩的音乐可视化系列小项目(03)---RGB律动灯

eagler8   2021-10-6 11:53 楼主

偶然脑子发热心血来潮,想要做一个声音可视化的系列专题。这个专题的难度有点高,涉及面也比较广泛,相关的FFT和FHT等算法也相当复杂,不过还是打算从最简单的开始,实际动手做做试验,耐心尝试一下各种方案,逐步积累些有用的音乐频谱可视化的资料,也会争取成型一些实用好玩的音乐可视器项目。

【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯
项目材料如下:

 

0-.jpg

本帖最后由 eagler8 于 2021-10-7 21:28 编辑

回复评论 (17)

RGB模块参数
1.PCB颜色:黑色
2.使用5mm全彩超高亮LED
3.带限流电阻防止烧坏LED
4.可接各种单片机
5.高电平点亮LED
6.工作电压:3.3V/5V
7.模块重量:4g
8.可直接插在Arduino主板上使用,无需杜邦线连接

 

0.jpg

点赞  2021-10-6 13:08

RGB 5050灯带主要特点:
1、可随意弯曲,可任意固定在凹凸表面;
2、每隔3颗LED灯即可组成一回路;
3、体积小巧,颜色丰富;
4、使用室内低压、户外高压,套管以及滴胶防水。
参数及应用
1、每三个LED可以沿着上面切线任意截断,不损坏其它部分
2、由印制电路板组成,背面带双面胶,用于粘贴
3、体积小。有多种颜色可供客户选择
4、用于透明或非透明材料的四周点亮、局部照明
5、用于用于路径和轮廓标志、广告、电视背景墙、室内吊顶
6、每卷的标准长度:5m,表面采用滴胶或套管处理。
7、每卷灯的数量为:300pcs软光条的宽度:10mm
8、工作电压:直流电压12V、交流220V
9、LED规格:采用超高亮3528/5050贴片LED
10、发光颜色:红/黄/蓝/绿/白

 

00.jpg

点赞  2021-10-6 13:12

前两个帖子我给你删除了把,都在帖子里

没有什么不可以,我就是我,不一样的烟火! 
点赞  2021-10-6 14:03

MAX9814是一款低成本高性能麦克风放大器,具有自动增益控制(AGC)和低噪声麦克风偏置。器件具有低噪声前端放大器、可变增益放大(VGA)、输出放大器、麦克风偏置电压发生器和AGC控制电路。
●自动增益控制(AGC)  
●3种增益设置(40dB、50dB、60dB)  
●可编程动作时间  
●可编程动作和释放时间比  
●电源电压范围2.7V~5.5V   
●低THD:0.04% (典型值)  
●低功耗关断模式  
●内置2V低噪声麦克风偏置

 

000.jpg

点赞  2021-10-6 14:49

 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之一:Arduino 的 HSV 淡入淡出/反弹律动灯

 

  实验开源代码

 

/*
 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯
  项目之一:Arduino 的 HSV 淡入淡出/反弹律动灯
  实验接线: max9814接A0,蓝色LED接D13
  RGB模块    Ardunio Uno
  GND---------GND接地线
  Rpin           D9
  Gpin           D10
  Bpin           D11
*/

#define Indicator_LED 13
#define RED       9 // pin for red LED
#define GREEN    10 // pin for green - never explicitly referenced
#define BLUE     11 // pin for blue - never explicitly referenced
#define MIC      A0//8 // Microphone
#define SIZE     220
#define DELAY    20
#define HUE_MAX  5.0
#define HUE_DELTA 0.01//0.01

/*TEST autogain code*/
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
uint8_t volume = 0;
/*///////////////////*/

//long deltas[3] = { 5, 6, 7 };
long rgb[3];
long rgbval;
//float rand;
boolean flag = false;

int val;
// for reasons unknown, if value !=0, the LED doesn't light. Hmm ...
// and saturation seems to be inverted
float hue = 0.0, last_hue = 0.0, saturation = 1, value = 1;

/*
  chosen LED SparkFun sku: COM-09264
  has Max Luminosity (RGB): (2800, 6500, 1200)mcd
  so we normalize them all to 1200 mcd -
  R  250/600  =  107/256
  G  250/950  =   67/256
  B  250/250  =  256/256
*/
long bright[3] = { 107, 67, 256};
//long bright[3] = { 256, 256, 256};

long k, temp_value;

void setup () {
  pinMode(Indicator_LED, OUTPUT);
  digitalWrite(Indicator_LED, LOW);

  //Serial.begin(9600);/*Tempo*/

  randomSeed(analogRead(4));
  for (k = 0; k < 3; k++) {
    pinMode(RED + k, OUTPUT);
    rgb[k] = 0;
    analogWrite(RED + k, rgb[k] * bright[k] / 256);
  }
  //pinMode(MIC, INPUT);
}

void loop() {
  /*TEST autogain code*/
  unsigned long startMillis = millis(); // Start of sample window
  unsigned int peakToPeak = 0;   // peak-to-peak level

  unsigned int signalMax = 0;
  unsigned int signalMin = 1024;

  // collect data for 50 mS
  while (millis() - startMillis < sampleWindow)
  {
    sample = analogRead(MIC);
    if (sample < 1024)  // toss out spurious readings
    {
      if (sample > signalMax)
      {
        signalMax = sample;  // save just the max levels
      }
      else if (sample < signalMin)
      {
        signalMin = sample;  // save just the min levels
      }
    }
  }
  peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
  double volts = (peakToPeak * 5.0) / 1024;  // convert to volts
  bool pulse = (sample - signalMin) > (peakToPeak * 0.50) && peakToPeak > 130;
  //Serial.println(peakToPeak);
  /*if(pulse){
    }*/
  /*///////////////////*/

  //flag = false
  //val = digitalRead(MIC);//Board bleu
  //val = !digitalRead(MIC);//Board Rouge

  if ( /*volume > 0*/peakToPeak >= 260/*pulse*/)
  {
    /*if(flag)
      {
      digitalWrite(Indicator_LED, HIGH);
      }
      else
      {
      digitalWrite(Indicator_LED, LOW);
      }
      flag = !flag;*/

    do
    {
      hue = random(601) / 100;
    } while (hue == last_hue);//break only when value is different
    last_hue = hue;

    //hue += HUE_DELTA;
    digitalWrite(Indicator_LED, HIGH);
    delay(50);
    digitalWrite(Indicator_LED, LOW);
  }
  /*hue += HUE_DELTA;
    if (hue > HUE_MAX) {
    hue=0.0;
    }*/
  rgbval = HSV_to_RGB(hue, saturation, value);
  rgb[0] = (rgbval & 0x00FF0000) >> 16; // there must be better ways
  rgb[1] = (rgbval & 0x0000FF00) >> 8;
  rgb[2] = rgbval & 0x000000FF;
  for (k = 0; k < 3; k++) { // for all three colours
    analogWrite(RED + k, rgb[k] * bright[k] / 256);
  }

  /*   //If there is a decent change in volume since the last pass, average it into "avgBump"
    if (volume - last > 10) avgBump = (avgBump + (volume - last)) / 2.0;
    //If there is a notable change in volume, trigger a "bump"
    //  avgbump is lowered just a little for comparing to make the visual slightly more sensitive to a beat.
    bump = (volume - last > avgBump * .9);
    //If a "bump" is triggered, average the time between bumps
    if (bump) {
    avgTime = (((millis() / 1000.0) - timeBump) + avgTime) / 2.0;
    timeBump = millis() / 1000.0;
    }*/

  //delay(DELAY);
}

long HSV_to_RGB( float h, float s, float v ) {
  /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
  // H is given on [0, 6]. S and V are given on [0, 1].
  // RGB is returned as a 24-bit long #rrggbb
  int i;
  float m, n, f;

  // not very elegant way of dealing with out of range: return black
  if ((s < 0.0) || (s > 1.0) || (v < 0.0) || (v > 1.0)) {
    return 0L;
  }

  if ((h < 0.0) || (h > 6.0)) {
    return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
  }
  i = floor(h);
  f = h - i;
  if ( !(i & 1) ) {
    f = 1 - f; // if i is even
  }
  m = v * (1 - s);
  n = v * (1 - s * f);
  switch (i) {
    case 6:
    case 0:
      return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
    case 1:
      return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
    case 2:
      return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
    case 3:
      return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
    case 4:
      return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
    case 5:
      return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
  }
}

 

点赞  2021-10-6 16:23

  实验场景图

 

01.jpg

点赞  2021-10-6 16:50

 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之一:Arduino 的 HSV 淡入淡出/反弹律动灯

 

   实验视频剪辑

 

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

 


 

点赞  2021-10-6 16:57

  实验场景动态图

 

02.gif

点赞  2021-10-6 17:03

 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之二:音乐反应RGB律动灯

 

  实验开源代码

 

/*
 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯
  项目之二:音乐反应RGB律动灯
  实验接线: max9814接A0
  RGB模块    Ardunio Uno
  GND---------GND接地线
  Rpin           D9
  Gpin           D10
  Bpin           D11
*/

int voltagePin = A0;
int voltageVal = 0;

long newVal = 0;

String binStr;

String rBin;
String gBin;
String bBin;

int rBinVal, gBinVal, bBinVal;

int r = 9;
int g = 10;
int b = 11;

int rv, gv, bv;

int c = 0;

void setup() {

  pinMode(r, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  voltageVal = analogRead(voltagePin);
  //c=c+1;
  //voltageVal=c;
  Serial.print("Voltage Value = ");
  Serial.println(voltageVal);
  voltageToRGB(voltageVal);
  delay(300);
}

void setrgb(int x, int y, int z)
{
  analogWrite(r, x);
  analogWrite(g, y);
  analogWrite(b, z);
}

String decToBin(int decValue, byte desiredStringLength) {

  String hexString = String(decValue, BIN);
  while (hexString.length() < desiredStringLength) hexString = "0" + hexString;

  return hexString;
}

int binToDecRGB(int binVal)
{
  switch (binVal)
  {
    case 0: return 0;
      break;
    case 1: return 35;
      break;
    case 10: return 71;
      break;
    case 11: return 107;
      break;
    case 100: return 143;
      break;
    case 101: return 179;
      break;
    case 110: return 215;
      break;
    case 111: return 251;
      break;
    default: return 255;
  }
}
void voltageToRGB(int voltage)
{

  binStr = decToBin(voltage, 10);

  rBin = binStr.substring(0, 3);
  gBin = binStr.substring(3, 6);
  bBin = binStr.substring(6, 9);

  rBinVal = rBin.toInt();
  gBinVal = gBin.toInt();
  bBinVal = bBin.toInt();

  rv = binToDecRGB(rBinVal);
  gv = binToDecRGB(gBinVal);
  bv = binToDecRGB(bBinVal);

  setrgb(rv, gv, bv);

  Serial.print("Bin Value = ");
  Serial.println(binStr);
  Serial.print("R Bin Value = ");
  Serial.print(rBinVal);
  Serial.print("    R Int Value = ");
  Serial.println(r);
  Serial.print("G Bin Value = ");
  Serial.print(gBinVal);
  Serial.print("    G Int Value = ");
  Serial.println(g);
  Serial.print("B Bin Value = ");
  Serial.print(bBinVal);
  Serial.print("    B Int Value = ");
  Serial.println(b);
}

 

点赞  2021-10-6 18:43

实验串口返回情况

 

03.jpg

点赞  2021-10-6 18:51

 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之二:音乐反应RGB律动灯

 

  实验视频剪辑

 

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

 


 

点赞  2021-10-6 20:13

 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之三: LED 上的动态变色灯光秀

 

  实验开源代码

 

/*
  【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯
  项目之三: LED 上的动态变色灯光秀
  实验接线: max9814接A0
  RGB模块    Ardunio Uno
  GND---------GND接地线
  Rpin           D9
  Gpin           D10
  Bpin           D11
*/

#define REDPIN 11                                            //r control output pin
#define GREENPIN 10                                          //g control output pin
#define BLUEPIN 9                                          //b control output pin
#define SOUNDPIN A0                                         //pin for reading voltage level in from F2V circuit

void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  pinMode(SOUNDPIN, INPUT);
  Serial.begin (9600);                                      //serial console for dev use
}

double maxVal = 0;                                          //used to determine each song's unique max

double m1 = 0, m2 = 0, m3 = 0, m4 = 0;                      //used to split input range into 5 levels
int d = 4;                                                  //(delta) value to make colors more reactive
int previous = 0;                                           //level previously read in

int r, g, b;                                                //red, green, and blue values to be output
int wait = 1;                                               //delay between each loop

double power = 1.8;                                         //affects linearity of sensor input values

void loop() {
  double sensorValue = pow(analogRead (SOUNDPIN), power);   //reads in signal and spreads values according to power
  Serial.println(analogRead(SOUNDPIN), DEC);                //prints input to consol for dev use
  maxVal -= 1;                                              //decrements local max to keep range updated

  if (sensorValue > maxVal) {                               //resets max and interval values
    maxVal = sensorValue;
    m1 = 0.1 * maxVal;
    m2 = 0.25 * maxVal;
    m3 = 0.5 * maxVal;
    m4 = 0.75 * maxVal;
  }

  if (previous <= m1) {                                     //compares previous range to current range
    if (sensorValue <= m1) {                                //and changes rgb values accordingly
      r += d;
      g -= d;
      b -= d;
    }
    else if (sensorValue <= m2) {
      r -= d;
      g += 2 * d;
      b -= 3 * d;
    }
    else if (sensorValue <= m3) {
      r -= 3 * d;
      g += d;
      b += 2 * d;
    }
    else if (sensorValue <= m4) {
      r -= 3 * d;
      g -= 2 * d;
      b += 3 * d;
    }
    else {
      r += d;
      g -= 3 * d;
      b += 4 * d;
    }
  }
  else if (previous <= m2) {
    if (sensorValue <= m1) {
      r += d;
      g -= d;
      b -= d;
    }
    else if (sensorValue <= m2) {
      r -= d;
      g += d;
      b -= d;
    }
    else if (sensorValue <= m3) {
      r -= d;
      g += d;
      b += d;
    }
    else if (sensorValue <= m4) {
      r -= d;
      g -= 2 * d;
      b += 2 * d;
    }
    else {
      r += 3 * d;
      g -= 3 * d;
      b += 3 * d;
    }
  }
  else if (previous <= m3) {
    if (sensorValue <= m1) {
      r += 2 * d;
      g -= 2 * d;
      b -= 3 * d;
    }
    else if (sensorValue <= m2) {
      r += d;
      g -= d;
      b -= d;
    }
    else if (sensorValue <= m3) {
      r -= d;
      g += d;
      b += d;
    }
    else if (sensorValue <= m4) {
      r -= d;
      g -= d;
      b += d;
    }
    else {
      r += 2 * d;
      g -= 3 * d;
      b += 3 * d;
    }
  }
  else if (previous <= m4) {
    if (sensorValue <= m1) {
      r += 3 * d;
      g -= d;
      b -= 3 * d;
    }
    else if (sensorValue <= m2) {
      r += d;
      g += d;
      b -= 2 * d;
    }
    else if (sensorValue <= m3) {
      r -= d;
      g += d;
      b -= d;
    }
    else if (sensorValue <= m4) {
      r -= d;
      g -= d;
      b += d;
    }
    else {
      r += d;
      g -= d;
      b += d;
    }
  }
  else {
    if (sensorValue <= m1) {
      r += d;
      g += d;
      b -= 3 * d;
    }
    else if (sensorValue <= m2) {
      r -= d;
      g += d;
      b -= 3 * d;
    }
    else if (sensorValue <= m3) {
      r -= 3 * d;
      g += 3 * d;
      b -= d;
    }
    else if (sensorValue <= m4) {
      r -= 2 * d;
      g += d;
      b += d;
    }
    else {
      r += d;
      g -= 3 * d;
      b += d;
    }
  }

  if (r > 255) {
    r = 255; //resets r, g, and b values if they become too small or too large
  }
  if (r < 0) {
    r = 0;
  }
  if (g > 255) {
    g = 255;
  }
  if (g < 0) {
    g = 0;
  }
  if (b > 255) {
    b = 255;
  }
  if (b < 0) {
    b = 0;
  }


  analogWrite(REDPIN, r);                                   //writes r, g, and b values to red, green, and blue, pins
  analogWrite(GREENPIN, g);
  analogWrite(BLUEPIN, b);
  previous = sensorValue;                                   //updates the previous value
  delay(wait);                                              //delays the wait amount specified
}

 

点赞  2021-10-7 13:25

 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之三: LED 上的动态变色灯光秀

 

  实验视频剪辑

 

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

 


 

点赞  2021-10-7 13:32

  【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之四: RGB 5050 声音反应灯带

  实验接线: max9814接A0

  RGB灯带    Ardunio Uno

  5V --------- 5V

  Rpin           D9

  Gpin           D10

  Bpin           D11

 

06.jpg

点赞  2021-10-7 21:11

  【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之四: RGB 5050 声音反应灯带

 

  实验开源代码

 

/*
  【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯
  项目之四: RGB 5050 声音反应灯带
  实验接线: max9814接A0
  RGB灯带    Ardunio Uno
  5V   --------- 5V
  Rpin           D9
  Gpin           D10
  Bpin           D11
*/

#define red 9
#define green 10
#define blue 11
#define microphone A0
#define L 13

int change = 0;
int minNum = 4000; // Gerar uma função entre 4000 e 15000 para mudar as cores dos leds.
int maxNum = 15000;

int x = 0;
int y = 0; // Serve para não repetir as cores mais que 3 vezes seguidas.
int z = 0;

int initFirstRGB = 500;
int initRGB = 150; // Inicia o RGB com 3 pausas de luz vermelha e 1 verde.
int initLastRGB = 1000;

bool soundstate; // Deteta o som do microfone.


void setup() {
  //Serial.begin(9600);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(L, OUTPUT);
  pinMode(microphone, INPUT);

  digitalWrite(L, LOW); // Desliga os leds.
  initStrip();
}

void loop() {  //Define os padrões e a ordem pelos quais apareçe.
  
  chooseRandomColor();
  changeRandomTime();
  
  for (int i = 0; i < change; i++) {
  detectSound();

  if (soundstate == 1) {
    colorShow();
  }
  else {
    colorOff();
    }
  }
  
}

void initStrip() {  //Consoante o som detetado altera as cores dos leds.
    colorRed();
    delay(initFirstRGB);
    colorOff();
    delay(initRGB);
    colorRed();
    delay(initRGB);
    colorOff();
    delay(initRGB);
    colorRed();
    delay(initRGB);
    colorOff();
    delay(initRGB);
    colorGreen();
    delay(initFirstRGB);
    colorOff();
    delay(initLastRGB);
}

//Definir as cores no padrão RGB.

void colorRed() {  
  analogWrite(red, 255);
  analogWrite(green, 0);
  analogWrite(blue, 0);
}

void colorGreen() {
  analogWrite(red, 50);
  analogWrite(green, 250);
  analogWrite(blue, 50);
}

void colorBlue() {
  analogWrite(red, 0);
  analogWrite(green, 0);
  analogWrite(blue, 255);
}

void colorYellow() {
  analogWrite(red, 255);
  analogWrite(green, 255);
  analogWrite(blue, 0);
}

void colorCyan() {
  analogWrite(red, 0);
  analogWrite(green, 255);
  analogWrite(blue, 255);
}

void colorPurple() {
  analogWrite(red, 255);
  analogWrite(green, 0);
  analogWrite(blue, 255);
}

void colorOff() {
  analogWrite(red, 0);
  analogWrite(green, 0);
  analogWrite(blue, 0);
}

int chooseRandomColor() {  //Escolhe uma cor ao randomicamente.
  while(y == x || z == x){
    x = random(1, 7);
  }

  z = y;
  y = x;
}

int changeRandomTime() {  //Mudar as cores aleatoriamente.
  change = random(minNum, maxNum);
}

bool detectSound() {  //Detetar o microfone.
  soundstate = digitalRead(microphone);
}

void colorShow() {  //Mostrar as cores defenidas anteriormente.
  if (x == 1) {
    colorRed();
  }

   if (x == 2) {
    colorGreen();
  }

   if (x == 3) {
    colorBlue();
  }

   if (x == 4) {
    colorYellow();
  }

   if (x == 5) {
    colorCyan();
  }

   if (x == 6) {
    colorPurple();
  }
}

 

点赞  2021-10-7 21:15

 【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯

  项目之四: RGB 5050 声音反应灯带

 

  实验视频剪辑

 

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

 


 

点赞  2021-10-7 21:26
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复