【雕爷学编程】Arduino动手做(149)---MAX9814咪头传感器模块6

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

 

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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

 

 

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

  实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目四十六:6mm大尺寸8x8LED方块动态音乐频谱仪

  Arduino实验开源代码

 

/*

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

  实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

  项目四十六:6mm大尺寸8x8LED方块动态音乐频谱仪

 接脚连线:

 MAX7219    UNO

 VCC →→→→→ 5V

 GND →→→→→ GND

 DIN →→→→→ D12(数据,数据接收引脚)

 CS  →→→→→ D11(负载,命令接收引脚)

 CLK →→→→→ D10(时钟,时钟引脚)

*/

#include "LedControl.h"

/* Led matrix - Max7219 Declared */

LedControl lc = LedControl(12, 11, 10, 1);

const int maxScale = 11;

/* Sensor - Max9812 Declared */

const int sensorPin = A4;

const int sampleWindow = 50; // 50ms = 20Hz

unsigned int sample;

unsigned long startMillis;

unsigned long timeCycle;

unsigned int signalMax = 0;

unsigned int signalMin = 1024;

unsigned char index = 0;

unsigned int peakToPeak[8];

unsigned int displayPeak[8];

unsigned int temp[8] = {0, 0, 0, 0, 0, 0, 0, 0};

unsigned int signalMaxBuff[8];

unsigned int signalMinBuff[8];

void setup() {

 // Led matrix

 lc.shutdown(0, false); // bật hiện thị

 lc.setIntensity(0, 1); // chỉnh độ sáng

 lc.clearDisplay(0); // tắt tất cả led

 Serial.begin(9600);

}

void loop() {

 startMillis = millis();

 //peakToPeak = 0;

 signalMax = 0;

 signalMin = 1024;

 // Get data in 50ms

 while (millis() - startMillis < sampleWindow) {

  sample = analogRead(sensorPin);

  if (sample < 1024) {

   if (sample > signalMax) {

    signalMax = sample;

   }

   if (sample < signalMin) {

    signalMin = sample;

   }

  }

  // 20Hz - 64Hz - 125Hz - 250Hz - 500Hz - 1kHz (timeCycle = 1/F)(ms)

  timeCycle = millis() - startMillis;

  if (timeCycle == 1 || timeCycle == 2 || timeCycle == 4 || timeCycle == 8

    || timeCycle == 16 || timeCycle == 32 || timeCycle == 40 || timeCycle == 50) {

   signalMaxBuff[index] = signalMax;

   signalMinBuff[index] = signalMin;

   index = (index + 1) % 8;

   delay(1);

   //Serial.println(timeCycle);

  }

 }

 // Delete pointer to array

 index = 0;

 // Calculation after get samples

 for (int i = 0; i < 8; i++) { // i = row (led matrix)

  // sound level

  peakToPeak[i] = signalMaxBuff[i] - signalMinBuff[i];

  // Map 1v p-p level to the max scale of the display

  displayPeak[i] = map(peakToPeak[i], 0, 1023, 0, maxScale);

  // Show to led matrix

  displayLed(displayPeak[i], i);

  // Led drop down

  if (displayPeak[i] >= temp[i]) {

   temp[i] = displayPeak[i];

  }

  else {

   temp[i]--;

  }

  lc.setLed(0, i, temp[i], true);

  delayMicroseconds(250);

 }

}

void displayLed(int displayPeak, int row) {

 switch (displayPeak) {

  case 0 : lc.setRow(0, row, 0x80); break;

  case 1 : lc.setRow(0, row, 0xC0); break;

  case 2 : lc.setRow(0, row, 0xE0); break;

  case 3 : lc.setRow(0, row, 0xF0); break;

  case 4 : lc.setRow(0, row, 0xF8); break;

  case 5 : lc.setRow(0, row, 0xFC); break;

  case 6 : lc.setRow(0, row, 0xFE); break;

  case 7 : lc.setRow(0, row, 0xFF); break;

 }

}

  

Arduino实验场景图

 

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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目四十六:6mm大尺寸8x8LED方块动态音乐频谱仪

 

实验视频剪辑

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

 

实验场景图 动态图

 


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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目四十七:Arduino 的 HSV 淡入淡出/反弹律动灯

Arduino实验开源代码

/*

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

  实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

 项目四十七: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);

 }

}

  

Arduino实验场景图

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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目四十七:Arduino 的 HSV 淡入淡出/反弹律动灯

实验视频剪辑

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

实验场景动态图

 

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

  实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目四十八:音乐反应RGB律动灯

  Arduino实验开源代码

 

/*
 【花雕动手做】有趣好玩的音乐可视化项目(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);
}

  

实验串口返回情况

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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目四十八:音乐反应RGB律动灯


实验视频剪辑

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


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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目四十九:LED 上的动态变色灯光秀

Arduino实验开源代码

/*
  【花雕动手做】有趣好玩的音乐可视化项目(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
}

  

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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目四十九:LED 上的动态变色灯光秀


实验视频剪辑

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

 

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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目五十:RGB 5050 声音反应灯带

Arduino实验开源代码

/*

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

  实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

 项目五十: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();

 }

}

  

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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目五十:RGB 5050 声音反应灯带

实验视频剪辑

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


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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目五十一:使用MAX9814的音乐反应简单柱跳灯

Arduino实验开源代码

/*

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

  实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

 项目五十一:使用MAX9814的音乐反应简单柱跳灯

*/

#include <Adafruit_NeoPixel.h>

#include <math.h>

#define N_PIXELS 8

#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实验场景图

 

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

实验一百四十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器

项目五十一:使用MAX9814的音乐反应简单柱跳灯

Arduino实验开源代码

 

实验视频剪辑

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


实验场景动态图

 

posted @ 2023-07-14 12:10  行者花雕  阅读(220)  评论(0编辑  收藏  举报