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

news2024/11/23 4:32:10

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

实验场景动态图

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/752924.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

resolvecomreference任务返回了false,但未记录错误

IDE从VS2015升级到了VS2022&#xff0c;然后就报了这个错&#xff0c;百度搜了下&#xff0c;没有啥结果&#xff0c;后来发现&#xff0c;2015可以用的一个dll在2022不能用了&#xff0c;把不能用的dll移除即可

VxLAN学习

目录 什么是VXLAN 为什么需要VXLAN 虚拟机动态迁移&#xff0c;要求提供一个无障碍接入的网络 什么是服务器虚拟化技术&#xff1f; 什么是虚拟机动态迁移&#xff1f; VXLAN如何满足虚拟机动态迁移时对网络的要求&#xff1f; 数据中心租户数量激增&#xff0c;要求提供…

TIOBE 2023年7月编程语言排行榜:C++即将超越C!

一、TIOBE统计数据&#xff08;2023年7月&#xff09; TIOBE Index编程社区指数是编程语言流行度的一个指标。评级基于全球熟练工程师的数量、课程和第三方供应商的数量。Google、Bing、Yahoo!、维基百科、亚马逊、YouTube 和百度等流行搜索引擎用于计算评级。 七月头条&#…

性能测试工具 Jmeter 测试 JMS (Java Message Service)/ActiveMQ 性能

目录 前言 ActiveMQ 介绍 准备工作 编写jndi.properties添加到ApacheJMeter.jar 中 下载 ActiveMQ 配置 Jmeter 进行测试 点对点 (Queues 队列) 配置 Jmeter 进行测试 发布/订阅 (Topic 队列) 配置发布 Publisher 配置订阅 Subscriber 总结 前言 JMeter是一个功能强大…

【Java】如何有效防止API的重放攻击?API接口防止参数篡改?

文章目录 前言一、API接口常见的安全防护要做到主要有以下几点&#xff1a;二、请求参数防篡改三、防止重放攻击3.1、基于timestamp的方案3.2、基于nonce的方案3.2、基于timestamp和nonce的方案3.3、微信公众号如何保证消息不会被重放攻击 前言 API重放攻击(Replay Attacks)又…

uniapp调接口出现跨域问题。

今天在写uniapp项目的时候&#xff0c;使用在线模拟接口的时候&#xff0c;出现跨域问题。 【问题描述】&#xff1a; ①在内嵌浏览器运行&#xff0c;不会出现跨域问题&#xff0c;好像是内嵌浏览器自动去掉了跨域问题。 ②在外部浏览器调用的时候会出现跨域问题。&#xf…

Yalmip工具箱使用教程(2)-决策变量进阶

博客中所有内容均来源于自己学习过程中积累的经验以及对yalmip官方文档的翻译&#xff1a;https://yalmip.github.io/tutorials/ 1.决策变量的定义 1.1 sdpvar 上文简单介绍了sdpvar函数的用法&#xff0c;接下来将对其进行详细介绍。复习一下&#xff0c;sdpvar函数的基本语…

常用化合物谱图数据库查询系统-40个软件免费查!

化学图谱是用于描述化学物质结构和性质的图形化表示方法&#xff0c;在有机化学、材料科学、生物化学、药物化学等领域都被广泛的应用研究分析。不同类型的化学谱图适用于不同的化学分析和研究领域&#xff0c;为此笔者调研了常用化合物谱图数据库及各个类型的主流使用化学谱图…

(RX200)R5F52315ADFP/R5F52318ADFL/R5F52316CDFL微控制器基于RXv2 32位内核,54MHz 闪存 LQFP

RX200 32位微控制器 (MCU) 在功率效率和性能之间实现了平衡。这些MCU在工作模式下的电流消耗为0.12mA/MHz&#xff0c;在待机模式下的电流消耗仅为0.8μA&#xff08;保留RAM内容&#xff09;。RX200 MCU具有54MHz的最高CPU运行速度&#xff0c;可提供4.16 CoreMark/MHz的高性能…

Home Assistant 南方电网 计算电费

目录 1.China Southern Power Grid Statistics集成2.获取当月用电情况3.计算电费然后在UI上显示3.效果 1.China Southern Power Grid Statistics集成 链接 2.获取当月用电情况 因为我的电费是固定的&#xff0c;没有阶梯电价 用电量 * 0.63906875 电费 3.计算电费然后在UI…

ESXI8.0安装教程,在VMware Workstation安装esxi

文章目录 &#x1f41f;前言&#x1f41f;安装&#x1f41f;在VMware WorkStation中创建虚拟机&#x1f41f;ESXI部署安装&#x1f41f;安装完成后配置 &#x1f41f;前言 ESXI8.0版本开始最低RAM从以前的4GB变为8GB 本文所使用到的镜像以及Key都可以到我的网站中下载 地址&…

Python——— 面向对象

&#xff08;一&#xff09;初识面向对象 Python完全采用了面向对象的思想&#xff0c;是真正面向对象的编程语言&#xff0c; 完全支持面向对象的基本功能&#xff0c;例如&#xff1a;继承、多态、封装等。 Python 支持面向过程、面向对象、函数式编程等多种编程范 式。 Pyth…

数据结构初阶--排序2

目录 前言快速排序思路hoare版本代码实现挖坑法代码实现前后指针法代码实现 快排优化三项取中法代码实现三指针代码实现 快排非递归代码实现 归并排序思路代码实现归并非递归代码实现 计数排序思路代码实现 前言 本篇文章将继续介绍快排&#xff0c;归并等排序算法以及其变式。…

2023年iOS App Store上架流程详解(上)

目录 1.注册开发者账号 2.登录并配置人员 3.申请证书和配置文件 一.证书管理​ 二.新建证书​ 三.使用appuploader服务同步证书​ 1&#xff09;申请证书 2&#xff09;添加Identifiers和配置App ID 3&#xff09;申请配置文件 1.在Xcode项目中配置签名 2.上传应用包…

数据库应用:死锁、悲观锁、乐观锁

目录 一、理论 1.死锁 2.悲观锁 3.乐观锁 二、总结 一、理论 1.死锁 &#xff08;1&#xff09;概念 死锁是指两个或两个以上的进程在执行过程中&#xff0c;因争夺资源而造成的一种互相等待的现象&#xff0c;若无外力作用&#xff0c;它们都将无法推进下去。此时称系…

INVDIA GPU参数列表:P100,V100,A100,A800,H100等性能参数

一、P100与V100 训练深层神经网络的时候&#xff0c;V100比P100快大约2倍 二、A100 三、A800 四、H100 引用 1、 世上最全NVDIA GPU参数列表&#xff1a; V100&#xff0c; A100&#xff0c; A800&#xff0c;H100&#xff0c;3090&#xff0c;4090&#xff0c; A40&#xf…

Unity使用Android Stdio接入SDK制作AAR包

最近需要在Unity2021版本中接入SDK&#xff0c;发现和直接接入SDK有些区别&#xff0c;这里把接入的流程记录一下。 Unity在最新的版本中把UnityPlayActivity.java&#xff0c;从Class.jar中移除&#xff0c;需要在Unity的工程内找到这个文件&#xff0c;并把其加入到Android …

【监控系统】Prometheus监控组件Mysql-Exporter配置实战

Mysql-Exporter主要监控Mysql数据库的稳定性、吞吐量、连接情况、缓冲池使用情况、查询性能等各项指标&#xff0c;是我们压测时常常需要监控的一些指标。 目前&#xff0c;Exporter 支持高于5.6版本的 MySQL 和高于10.1版本的 MariaDB。在 MySQL/MariaDB 低于5.6版本时&#…

MobPush 最佳实践:消息重弹

功能概述 消息重弹功能&#xff0c;可在安卓在线通道送达的消息被其他消息挤下去时&#xff0c;重新置顶&#xff0c;增加消息的曝光度和点击率。 适用场景 仅支持Mob通过自有TCP通道下发的通知消息&#xff0c;不支持自定义推送消息支持开发者后台和推送API配置使用 功能使…

安天逆向教程——常用汇编语句

一.汇编基础 二.条件分支 反汇编时更多关注这些条件分支。如果看懂这些条件分支&#xff0c;会对程序的大体逻辑有一个整体的了解。 至于程序里面的细节&#xff0c;有时会省略掉。往往关键的跳转理解了甚至进行一点点的改动&#xff0c;就会使得程序发生翻天覆地的变化。 三…