【雕爷学编程】Arduino动手做(06)---KY-038声音传感器模块3

发布时间 2023-07-15 15:02:16作者: 行者花雕

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

 

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验六:KY-038高感度声音传感器模块

 

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

  实验之六:KY-038声音传感器模块声控 感应小开关麦克风模块声音控制模块

  项目:使用KY—038声音模块的阙值触发WS2812节奏灯条

Arduino实验开源代码

 

/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(04)---WS2812条灯
  项目之一: 使用KY—038声音模块的阙值触发WS2812节奏灯条
*/

#include<FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 8

CRGB leds[NUM_LEDS];
uint8_t hue = 0;
int soundsensor = 7;

void setup() {
  delay(2000);
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(255);
  pinMode(soundsensor, INPUT);
}

void loop() {
  int sensval = digitalRead(soundsensor);

  if (sensval == 1) {
    Serial.println("ON");
    leds[0] = CRGB :: Red;
    fill_solid(leds, NUM_LEDS, CRGB :: Blue);
    rainbow_moving();
    FastLED.show();
    delay(10);
  }
  else {
    leds[0] = CRGB :: Black;
    fill_solid(leds, NUM_LEDS, CRGB :: Black);
    FastLED.show();
    delay(10);
  }
}

void rainbow_moving() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV(hue + (i * 10), 255, 255);
  }
  EVERY_N_MILLISECONDS(10) {
    hue++;
  }
}

  

Arduino实验场景图

 

 实验视频剪辑

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

实验场景动态图

 

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

  实验六:KY-038声音传感器模块声控 感应小开关麦克风模块声音控制模块

  项目:数字信号驱动的七色节奏灯

Arduino实验开源代码

 

/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(04)---WS2812条灯
  项目之二:数字信号驱动的七色节奏灯
*/

#define FASTLED_INTERRUPT_RETRY_COUNT 0
//#define FASTLED_ESP8266_RAW_PIN_ORDER

#include <FastLED.h>
#define NUM_LEDS 8
CRGB leds[NUM_LEDS];

const int ledPin = 6;
int sensorPin = 7;
boolean val = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  Serial.begin (9600);
  FastLED.addLeds<WS2812B, ledPin, RGB>(leds, NUM_LEDS);
}

void loop () {
  val = digitalRead(sensorPin);
  Serial.println (val);
  if (val == HIGH) {
    leds[0] = CRGB(180, 0, 0);
    FastLED.show();
    delay(3);
    leds[1] = CRGB(0, 180, 0);
    FastLED.show();
    delay(3);
    leds[2] = CRGB(0, 0, 240);
    FastLED.show();
    delay(3);
    leds[3] = CRGB(150, 0, 240);
    FastLED.show();
    delay(5);
    leds[4] = CRGB(220, 200, 20);
    FastLED.show();
    delay(5);
    leds[5] = CRGB(85, 60, 180);
    FastLED.show();
    delay(10);
    leds[6] = CRGB(50, 220, 20);
    FastLED.show();
    delay(10);
    FastLED.show();
    leds[7] = CRGB(220, 220, 250);
    FastLED.show();
    delay(10);
    FastLED.show();
  }
  else {
    leds[8] = CRGB(150, 0, 255);
    FastLED.show();
  }
  FastLED.clear();
}

  

实验视频剪辑

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

实验场景动态图

 

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

  实验六:KY-038声音传感器模块声控 感应小开关麦克风模块声音控制模块

  项目:12位环形音乐反应灯

Arduino实验开源代码

 

/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(04)---WS2812条灯
  项目之三:12位环形音乐反应灯
*/

#define FASTLED_INTERRUPT_RETRY_COUNT 0
//#define FASTLED_ESP8266_RAW_PIN_ORDER

#include <FastLED.h>
#define NUM_LEDS 12
CRGB leds[NUM_LEDS];

const int ledPin = 6;
int sensorPin = 7;
boolean val = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  Serial.begin (9600);
  FastLED.addLeds<WS2812B, ledPin, RGB>(leds, NUM_LEDS);
}

void loop () {
  val = digitalRead(sensorPin);
  Serial.println (val);
  if (val == HIGH) {
    leds[0] = CRGB(180, 0, 0);
    FastLED.show();
    delay(3);
    leds[1] = CRGB(0, 180, 0);
    FastLED.show();
    delay(3);
    leds[2] = CRGB(0, 0, 240);
    FastLED.show();
    delay(3);
    leds[3] = CRGB(150, 0, 240);
    FastLED.show();
    delay(5);
    leds[4] = CRGB(180, 200, 20);
    FastLED.show();
    delay(5);
    leds[5] = CRGB(85, 60, 180);
    FastLED.show();
    delay(10);
    leds[6] = CRGB(50, 220, 20);
    FastLED.show();
    delay(5);
    FastLED.show();
    leds[7] = CRGB(0, 0, 250);
    FastLED.show();
    delay(5);
    FastLED.show();
    leds[8] = CRGB(240, 0, 0);
    FastLED.show();
    delay(10);
    leds[9] = CRGB(0, 250, 0);
    FastLED.show();
    delay(10);
    leds[10] = CRGB(0, 0, 255);
    FastLED.show();
    delay(10);
    leds[11] = CRGB(220, 200, 20);
    FastLED.show();
    delay(10);
  }
  else {
    leds[12] = CRGB(150, 0, 255);
    FastLED.show();
  }
  FastLED.clear();
}

  

Arduino实验场景图

 

实验视频剪辑

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

实验场景动态图